user1951269
user1951269

Reputation: 11

Printing a 2d char array to a Swing Gui (Java)

I am currently trying to convert a Java application (also made by me) from a console based one to one that uses a Swing based GUI. All well and good, but I don't understand Swing as well as I'd like to, so this is somewhat difficult.

My current issue is printing an array of chars to the output. Originally, I was able to print to the console directly through judicious use of for loops and System.out.print(theworld[j][i]); (where i and j iterate through the loops, and theworld is the array in question), but I don't know how to get something similar to work for a Swing GUI.

Specifically, it was printed to the console like this:

for (int i = 0; i < ydimension; i++){
  for (int j = 0; j < xdimension; j++){
    System.out.print(theworld [j][i]);
  }
System.out.println();
}

Currently, I have a JFrame with a JMenuBar attached, which all works as intended. What I'd like is that the array appears printed on the frame (or on an attached panel) in response to a stimulus, either a button press or a timer tick.

Another thing is, the array will be getting updated every tick, and this should be reflected in the final printed array, so would I go about clearing the screen before printing the array or something similar?

Thanks.

EDIT: Just having looked at my code again, I've realised in the question I omitted the println in the outer for loop. It's supposed to print line by line, rather than all as one line. The above code has been corrected to reflect this.

Upvotes: 1

Views: 2323

Answers (3)

David Kroukamp
David Kroukamp

Reputation: 36423

Well there are many ways to display text data each with advantages and disadvantges for different situations:

1) Text Controls:

Also known simply as text fields, text controls can display only one line of editable text. Like buttons, they generate action events. Use them to get a small amount of textual information from the user and perform an action after the text entry is complete.

2) Plain Text Editors:

JTextArea can display multiple lines of editable text. Although a text area can display text in any font, all of the text is in the same font. Use a text area to allow the user to enter unformatted text of any length or to display unformatted help information.

3) Styled Text Areas:

A styled text component can display editable text using more than one font. Some styled text components allow embedded images and even embedded components. Styled text components are powerful and multi-faceted components suitable for high-end needs, and offer more avenues for customization than the other text components.

3) JLabel

With the JLabel class, you can display unselectable text and images. If you need to create a component that displays a string, an image, or both, you can do so by using or extending JLabel. If the component is interactive and has a certain state, use a button instead of a label.

4) JTable (+1 to @AndrewThompsons comment)

With the JTable class you can display tables of data, optionally allowing the user to edit the data. JTable does not contain or cache data; it is simply a view of your data.

All above are linked with relevant examples/information from oracle.

The next problem is position your component where you want it:

For this Swing has various inbuilt LayoutManagers.

See Laying Out Components Within a Container for more on that.

As for:

Another thing is, the array will be getting updated every tick, and this should be reflected in the final printed array, so would I go about clearing the screen before printing the array or something similar

Use a Swing Timer as to make sure your Gui is updated from EDT.

Most of above components besides JTable has a simple setText(String s) and getText() methods.

Calling setText on one of their instances will make the old text be overwritten with new text. So no need for you having to clear it, simply call the same method which added the text with new text. To append text you would use a combination of getText() and setText(String s) or append(String s) when using JTextArea.

If this is not what you want, try be more specific on the expected output maybe post an SSCCE showing what you have and are trying to achieve.

Upvotes: 1

Andrew Mao
Andrew Mao

Reputation: 36940

Perhaps it would make more sense (and be easier to read) if you used a JTable instead of trying to output the text directly. Here's an example of what it might look like. You would update the TableModel on every tick and not have to worry about clearing the screen.

The other thing that is very important is that when you update the TableModel, it has to be done according to Swing's threading policy. That means you will probably have to do something like the following, from whatever thread is doing the updates:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        // Update row/rows of table model here
    }
});

Upvotes: 2

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9317

If you just want the text to appear in the gui, first you need to construct the string to be displayed. So you would concatenate the strings you were earlier printing to the console to one single string.

Then you could use JTextArea to display it. You can even use JLabel or directly draw over the JPanel.

To update the display you would again set the new text in the TextArea.

Upvotes: 1

Related Questions