Hi, would appreciate the advise and help in JTextArea

ArrayList studentList = new ArrayList();

//Create Center Layount TextField Area

txtArea = new JTextArea ();

txtArea.setText("");

txtArea.setEditable(false);

public void actionPerformed (ActionEvent e) {

if (e.getSource() == btnAdd)

addRecord (studentList);

if (e.getSource () == btnList)

listRecord (studentList);

//Add Record Function

public void addRecord (ArrayList studentList) {

String sNumber = (txtSNumber.getText());

studentList.add(sNumber);

txtArea.setText("Student Record Added to Base Data :)");

}

//List Record Function

public void listRecord (ArrayList studentList)

{

if(studentList.size() > 0)

{

for(int i = 0; i < studentList.size(); i++)

{

txtArea.setText("StudentNo" + "\n" + studentList.get(i));

}

}

else

txtArea.setText("No Students Registered.");

}

Those are the codes of mine, I've been trying and trying to print on the JTextArea the array values, but it only prints the last added value only. Example sNumber i add in value of s10014213 , it'll display s10014213 , but after i add in a 2nd value of 8582186, the textArea will only display 8582186 and not s10014213 and 8582186.

I would really appreciate any kind helpings kind people out there may offer, I'm new at java. thanks and may you people have a great weekend ahead :)

Message was edited by:

victorxu

[1494 byte] By [victorxua] at [2007-11-27 3:29:05]
# 1
setText() replaces the existing content with the new text. I think append() is what you want to use.
uncle_alicea at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 2
Instead og using setText("") use append("My text")Carsten
Carsten_Fa at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanks alot for the help rendered, I really appreciate the kind gesture :)once again, my most sincere thanks!
victorxua at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 4

//Add Record Function

public void addRecord (ArrayList studentList) {

String sNumber = (txtSNumber.getText());

studentList.add(sNumber);

txtArea.setText("Data Entered");

}

//List Record Function

public void listRecord (ArrayList studentList)

{

if(studentList.size() > 0)

{

for(int i = 0; i < studentList.size(); i++)

{

//txtArea.setText("Student Number");

txtArea.append("\n" +studentList.get(i));

}

}

else

txtArea.setText("No Students Registered.");

}

Hi, so sorry to be bothersome again, but after I appended it shows like S10014213 thn on S10014214 etc but there's always the line "Data Entered" above, is there any way I could remove it?

victorxua at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 5
You could clear the JTexArea using setText("") before calling addRecord()
Carsten_Fa at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 6
The output will be this :Data Entereds100142138425080
victorxua at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 7
And what should output be ?
Carsten_Fa at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 8

Carsten_F

Posts:19

Registered: 04/02/06 Re: Hi, would appreciate the advise and help in JTextArea

May 6, 2007 7:51 AM (reply 5 of 6)

You could clear the JTexArea using setText("") before calling addRecord()

Sorry Carsten but where should I clear the textarea and how and where should i put the addRecord?

I'm real sorry, I'm just ended my week3 of java, I'm really lost, thanks for your help mate, do hope you wouldnt mind me being so bothersome :)

victorxua at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 9
Sorry - I got it wrong I didn't see you actually added the text "Data entered"in method addRecord() So remove it if you don't want it.If you showed me how you wanted the output to be I might of better assistance.Carsten
Carsten_Fa at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 10
I would just like to have s100142138425080 instead ofData Entereds100142138425080
victorxua at 2007-7-12 8:31:59 > top of Java-index,Java Essentials,Java Programming...
# 11

public void addRecord (ArrayList studentList) {

String sNumber = (txtSNumber.getText());

studentList.add(sNumber);

txtArea.setText("Data Entered");

}

The reason i added the Data Entered into the method is because the problem asks that when i click the Add Record button the txtArea should display "Data Entered"

public void listRecord (ArrayList studentList)

{

if(studentList.size() > 0)

{

for(int i = 0; i < studentList.size(); i++)

{

txtArea.append("\n" +studentList.get(i));

}

}

else

txtArea.setText("No Students Registered.");

}

Above are the codes I used for the program to display the added numbers to the text area.

But when I call the listRecord()

the following will appear

Data Entered

s10014213

8425080

is it possible for me to just have

s10014213

8425080

appear instead of the additional Data Entered infront?

Thanks alot for your help. I'm really grateful for the time of yours

victorxua at 2007-7-12 8:32:00 > top of Java-index,Java Essentials,Java Programming...
# 12

Add the code in listRecord()

public void listRecord (ArrayList studentList)

if(studentList.size() > 0) {

txtArea.setText("");// Removes all text

for(int i = 0; i < studentList.size(); i++) {

txtArea.append("\n" +studentList.get(i));

}

}

else

txtArea.setText("No Students Registered.");

}

}

Carsten_Fa at 2007-7-12 8:32:00 > top of Java-index,Java Essentials,Java Programming...
# 13
Thanks alot for your time mate! Finally I can continue on my assignment, thanks mate for your kind help and time spent! may the week ahead be bright and blessed for u :)
victorxua at 2007-7-12 8:32:00 > top of Java-index,Java Essentials,Java Programming...
# 14
The same to You :)
Carsten_Fa at 2007-7-12 8:32:00 > top of Java-index,Java Essentials,Java Programming...
# 15

//List Record Function

public void listRecord (ArrayList studentList){

if(studentList.size() > 0) {

txtArea.setText("");}

{

for(int i = 0; i < studentList.size(); i++)

{

txtArea.append("\n" +studentList.get(i));

}

}

else if

txtArea.setText("No Students Registered.");

}

wow...I'm really burnt, I have like 12 errors in that coding alone...

victorxua at 2007-7-21 20:48:01 > top of Java-index,Java Essentials,Java Programming...
# 16

//List Record Function

public void listRecord (ArrayList studentList){

txtArea.setText("StudentNo");

if(studentList.size() > 0)

{

for(int i = 0; i < studentList.size(); i++)

{

txtArea.append("\n" +studentList.get(i));

}

}

else

txtArea.setText("No Students Registered.");

}

OH MY! I'm so stupid, I just have to add a txtArea.setText in the front of the codes lol. MY BAD! Gosh I'm just so stupid lol.

Thanks for all your help! I'm just so glad that you were around :)

victorxua at 2007-7-21 20:48:01 > top of Java-index,Java Essentials,Java Programming...