Displaying Arrays in Java

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
Hey Guys - can anyone give me a hand here?

I need to display an array in Java. I can display it in DOS easily - getting it to display in a JFrame GUI has me baffled.

I assume I need to use something like a JTextArea?
How do I get the Array into the TextArea?
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Try:
Code:
//In the top bit where you declare variables
private JTextArea textArea = new JTextArea(5,10);
private String[] strings = new String[3];

//Inside the constructor/wherever you add things to the panel:
//Add some crap to the array
string[0] = "foo";
string[1] = "bar";
string[3] = "foobar";

//Populate the textArea
textArea.setText("");
for(int i=0; i<string.length; i++){
  textArea.append(strings[i]+"\r\n");
}

This should put:
foo
bar
foobar

in your text area.

BTW - http://java.sun.com/j2se/1.5.0/docs/api/ is the most valuable site for java stuff.
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
cheers, but I need a lil more help... lecturers set us an assignment without teaching us how to use arrays in GUIs. So far, 2 people out of 80 have managed to hack away until they got it working.

I'm reading in strings from a .txt file to an Array - I have the method written for doing this already - what I really want to know, is it possible to call a method inside a TextArea?

so I have my text area:
Code:
Table = new JTextArea();
Table.setBounds(180, 150, 400, 330);
panel.add(Table);

Can I add a line to that, so it just calls my dipslay method in a different object, and then diplays it in the TextArea?

The way the other 2 guys have done it, is by creating tempoary a String " ", and then passing a copy of the read in array to the GUI object. Then simply sticking the array elements after the tempoary string one after another, and then sticking the temp string into the TextArea.
It works, but it seems awfully complicated if you can just call the displayArray method that I've already created.

Help very appreciated :)
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
The JTextArea can only contain Strings, not String Arrays so you need to concatenate all the elements in the array into one string, then set the text of the textarea to that string. As arrays do not have a toString() method you have to set a temp String variable to contain its contents.
Code:
//call the method that returns an array
String[] tempArray = otherClass.getArrayFromATextFile();

String tempText = "";
for(int i=0; i<tempArray.length; i++){
  tempText  += tempArray[i];
}
Table.setText(tempText);
would be about the only way to do this.
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
For a minor performance improvement, you could use a StringBuffer to assemble the bits.. seems a bit odd that you are onto GUI work before completing all the language and useful run-time library stuff?

Phil.
 

SilverHood

FH is my second home
Joined
Dec 23, 2003
Messages
2,284
Cheers jingle.

Gonna give that a go tomorrow morning when I go into Uni...

Otherwise I'll just use the GUI to display things in a DOS window, which is also acceptable for the assignment, but not if I want the top mark.

phlash, it's not a programming course - most of our programming focuses on user interfaces for databases. In the 4th year, there's an option to do advanced programming, which will probably make you a Java programmer, rather than "know java" that we're doing now.

:)
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
phlash said:
For a minor performance improvement, you could use a StringBuffer to assemble the bits
I was going to say that, but I didn't even know that a StringBuffer should be used until on placement this year, string concatenation was used throughout our course textbook.

I am surprised that you haven't looked more into run-time functions/basic language constructs, on my course (Comp Sci at UMIST) we spent most of the first semester learning about the basic constructs of java in our programming module, then moving on to basic AWT stuff. We didn't even look at Swing until the second semester, so I'm surprised you're using JTextArea.
 

phlash

Fledgling Freddie
Joined
Dec 24, 2003
Messages
195
SilverHood said:
phlash, it's not a programming course - most of our programming focuses on user interfaces for databases. In the 4th year, there's an option to do advanced programming, which will probably make you a Java programmer, rather than "know java" that we're doing now. :)

Ah OK: DB-oriented course with GUIs for extra pain :mad: - if you haven't done it already, the online Java tutorial at http://java.sun.com/docs/books/tutorial/index.html provides a good grounding in Java and the standard run-time libs - worth reading through if you are going to use Java in anger (or indeed want to show off and get top marks!).
 

Users who are viewing this thread

Top Bottom