• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to copy from 4 JTextFields to 1 JTextArea

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm a newby with Java.

I want to Copy text from 4 JTextFields, to 1 JTextArea, if i push the >Kopieer> button.
The problem is that only 1 JTextField is copieed to the JTextArea.
Does someone know how to resolve this problem.


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import javax.swing.*;

public class Invulscherm extends JFrame{

public Invulscherm(){

JFrame venster = new JFrame();
venster.setSize(700,240);
venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
venster.setTitle("Voorbeeld 1 van Telling");
venster.setLocation(100 ,100);
JPanel hoofdpaneel = new InvulschermPaneel();
venster.add(hoofdpaneel);
venster.setVisible(true);
}

public static void main( String[] args){
new Invulscherm();

}
}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

import java.awt.event.*;
import javax.swing.*;

public class InvulschermPaneel extends JPanel implements ActionListener{

private JTextField veld1;
private JTextField veld2;
private JTextField veld3;
private JTextField veld4;
private JTextArea veld5;
private JButton actieknop;


public InvulschermPaneel(){
actieknop = new JButton(">SEND>");
actieknop.addActionListener(this);
veld1 = new JTextField(13);
veld2 = new JTextField(13);
veld3 = new JTextField(13);
veld4 = new JTextField(13);
veld5 = new JTextArea(3,40);

add(veld1);
add(veld2);
add(veld3);
add(veld4);
add(actieknop);
add(veld5);
}

public void actionPerformed(ActionEvent e){

String veldinhoud1 = veld1.getText();
String veldinhoud2 = veld2.getText();
String veldinhoud3 = veld3.getText();
String veldinhoud4 = veld4.getText();
veld5.setText(veldinhoud1);
veld5.setText(veldinhoud2);
veld5.setText(veldinhoud3);
veld5.setText(veldinhoud4);

}

}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

javaTelling.gif
[Thumbnail for javaTelling.gif]
picture Java application
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're setting the text of the JTextArea. Thus overriding the previous text. Concatenate the Strings and then set the text will solve your problem.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the JavaRanch
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
veld5.setText() does not add text - it just sets it, so you are setting the same text field 4 times. You need to take all your text fields, concatenate them together and call veld5.setText() on the concatenated text. Please respond if you need more information.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend using JTextArea#append(String text) instead of concatenating and then setting the text.
Moving thread to the UI forums.
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help

But can you give me an example how i should concatenate the 4JTextFields.
I'm not sure where i should make the change in the java code.

 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've solved the problem by concatenate: veld5.setText(veldinhoud1 + veldinhoud2 + veldinhoud3 + veldinhoud4);.
Now it's working good.


I have another question??

How can i make sure the "previous" text in the JTextArea is not erased everytime i push the >Send> button.
My goal is to make a list in the "JTextArea".

I hope you understand my question, because my english is not so good!


 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alberto Aparicio wrote:I have another question??

How can i make sure the "previous" text in the JTextArea is not erased everytime i push the >Send> button.



You already have the answer to that

Maneesh Godbole wrote:I would recommend using JTextArea#append(String text) instead of concatenating and then setting the text.

 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:I would recommend using JTextArea#append(String text) instead of concatenating and then setting the text.



I'v tried to use JTextArea#append(String text) in my Java code. But where exactly should i put JTextArea#append(String text)?
Can you give me an example, i only get a lot of ERRORS.

thanks

 
Saloon Keeper
Posts: 15490
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's simple. With veld5, just append, instead of setText.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alberto Aparicio wrote:I'v tried to use JTextArea#append(String text) in my Java code. But where exactly should i put JTextArea#append(String text)? Can you give me an example, i only get a lot of ERRORS.



To get better help, you'll want to post the offending code and the error messages. Otherwise we can't guess how you're doing it wrong.
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for al the help!

But now my program is advancing further, and getting more complex.

now i have a JTable 1button and 1 JTextArea. and the same problem as before.

My question is how can i get the button to put the text from the JTable to the JTextArea.

here is the new javacode!


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


java-Telling-2.gif
[Thumbnail for java-Telling-2.gif]
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you have a question regarding a compiler error or run-time exception, you will want to show us the error text. The more pertinent information you can give us, the better we can understand your problem and help you.

In your current code, you seem to be calling getText() on an array, and looking through the API, I don't see how array could recognize this method. Better would be for you to get the selected row from the JTable, and to learn how to do that, you should read the JTable tutorial including the section on getting the user's selection: How to Use Tables.

Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right? (I've taken the liberty of adding code tags to your post above)

To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:



Best of luck!
Pete
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Will append the text for the data in cell 0, 0.

If you want more data appended then you will need to write a loop to access the data of every cell you want added to the text area.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking through your code, I see several other problems, the main one being data shadowing. In short, you're doing a lot of this:



What happens here is that the veld5 JTextArea added to your GUI will not be the one declared in the class, but rather the one declared and initialized in the createAndShowGUI method. Since the veld5 variable declared in the class is never constructed, it will be null, and this will cause a lot of unwanted null pointer exceptions since your critical class fields will be null. Instead, don't re-declare the key fields but rather use the class fields.

So instead of this:
do this:


Likewise instead of this:
do this:


Likewise instead of this:
do this:


Good luck and hope this helps!
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help! i have learnt a lot. I have made the changes like Pete said.

but you must understand that i'm really not good at Java, but i have to do this project for my school.
So if you use a lot of theorie of Java, it's really dificult to understand for me. So i hope you have a little pattience with me.

But still nothing happens if i push the button. I want to copy the text from the Jtable to the JTextArea.

here is the NEW code mayby someone nows what i'm doing wrong.

if i push the button i get this error:

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's not an exception. That's just part of the stack trace, and probably the most useless part of it. Please post the entire stack trace.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alberto Aparicio wrote:Thanks for all the help! i have learnt a lot. I have made the changes like Pete said.

but you must understand that i'm really not good at Java, but i have to do this project for my school.
So if you use a lot of theorie of Java, it's really dificult to understand for me. So i hope you have a little pattience with me.


Don't lose heart, since with practice and effort, you can and will learn this stuff, and it will get easier.

The exception is actually "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String"

And this line causes it:


So what the exception is telling you is that you are trying to cast an Integer object as a String which is not allowed. So it seems that the object contained here:

is an Integer, and if you look at your Table model's data array:

you'll see that all rows contain an Object array that holds String, String, String, Integer objects, so it makes sense that calling getValue(3, 3) will return an Integer.

For completeness, here is your cast:



A solution is to get your String by using Integer's to String method. So in other words, rather than doing this:



do this:


Much luck!

Pete
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello i'm really happy with Javaranch because without you i could never have come so far with my Java project for school.

Now i have another problem. I am making a registration form for "Bird Wathchers", they must fill in in the JTable how many birds they have seen on wich date.
After they push the send button the data must be transfered to the JTextArea. (Everytime you put new data on the JTextArea it must start on a new line.)
But it does not start on a new line.

Ill make a picture of how i want it to be, and how it is now. Maybe someone can help me with the code.



howitis.gif
[Thumbnail for howitis.gif]
HOW IT IS NOW AFTER PUSHING SEND BUTTON
How-I-want-it.gif
[Thumbnail for How-I-want-it.gif]
HOW I WANT IT TO BE AFTER PUSHING SEND BUTTON
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this will probably fix your immediate problem (with simplification)



you'll still need to address:
if there are any conditions prior to printing the row's data e'g' if no date, ignore row
cell still in edit mode when 'send' pressed will print cell's pre-edit value
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Michael, it worked well!

you'll still need to address:
if there are any conditions prior to printing the row's data e'g' if no date, ignore row
cell still in edit mode when 'send' pressed will print cell's pre-edit value



How can i make sure that only the data that is edited in a ROW, is printed (in the JTextArea).
I want the hole row to be printed. For example "Valk 10-10-2010 Gelderland 3 23".
The rows that are not used must not be printed.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you're talking about also editing existing data (previously 'sent'),
one way might be a TableModelListener to keep a list of rows edited.

iterate (sort first?) the list when printing, at the end clear the list.
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have lookt at TableModelListener and found a method called tablechanged!

public void tableChanged(TableModelEvent e) {
}



And some parameters from TableModelEvent

TableModelEvent(source); // The data, ie. all rows changed
TableModelEvent(source, HEADER_ROW); // Structure change, reallocate TableColumns
TableModelEvent(source, 1); // Row 1 changed
TableModelEvent(source, 3, 6); // Rows 3 to 6 inclusive changed
TableModelEvent(source, 2, 2, 6); // Cell at (2, 6) changed
TableModelEvent(source, 3, 6, ALL_COLUMNS, INSERT); // Rows (3, 6) were inserted
TableModelEvent(source, 3, 6, ALL_COLUMNS, DELETE); // Rows (3, 6) were deleted



Until so far i understand this theorie, but i don't know how i must use this method for my problem i described before.
my problem is this!


How can i make sure that only the data that is edited in a ROW, is printed (in the JTextArea).
I want the hole row to be printed. For example "Valk 10-10-2010 Gelderland 3 23".
The rows that are not edited must not be printed.



Mayby someone can give me some hints, because i'm really stuck here.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alberto Aparicio wrote:I have lookt at TableModelListener and found a method called tablechanged!

How can i make sure that only the data that is edited in a ROW, is printed (in the JTextArea).
I want the hole row to be printed. For example "Valk 10-10-2010 Gelderland 3 23".
The rows that are not edited must not be printed.



Do you have your latest update for your SSCCE? I'm also a little confused as in your previous examples, the event that triggered updating the JTextArea's text was a JButton press, not a change the JTable's model. Which one will it be? Or in other words, can you explain your event model more clearly?

Best of luck
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the new code










Here are pictures from how the situation is now if i push the >SEND> button.
And a picture from how i want it to be.
How-the-result-is-now.gif
[Thumbnail for How-the-result-is-now.gif]
This happens if i push the >send> button now.
How-the-result-should-be.gif
[Thumbnail for How-the-result-should-be.gif]
This should happen. (only one line in the JTextArea, and the edited cells are emty again).
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again:

I'm also a little confused as in your previous examples, the event that triggered updating the JTextArea's text was a JButton press, not a change the JTable's model. Which one will it be? Or in other words, can you explain your event model more clearly?



On looking at your images, it still looks to me as if you want to use the selected row when the SEND JButton has been pressed. If this is indeed correct, then again, don't use a TableModelListener but rather simply use the JButton's ActionListener and in the actionPerformed method, use the JTable's model to get the selected row and then to get the row data. If my assumption of your requirements is incorrect, please correct me, but also please provide as much detail about your requirement as possible.
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your fast reply!
You should know i'm from Holland, and my englisch is not so good, that's why i find it hard to explain what i mean.

On looking at your images, it still looks to me as if you want to use the selected row when the SEND JButton has been pressed. If this is indeed correct,

this is indeed correct! I only want the data in the row that has been changed, to be "send" to the TextArea.

don't use a TableModelListener but rather simply use the JButton's ActionListener and in the actionPerformed method, use the JTable's model to get the selected row and then to get the row data.


So i should not use TableModel listener. But i should only use the ActionPerformed method.
But how can i get the SELECTED row, and the row data in Actionperformed?
mayby you could give me an example in java code.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that you'd get the selected row by getting the JTable's ListSelectionModel and then call getAnchorSelectionIndex() on it if you want the anchor selection or getLeadSelectionIndex() if you want the lead selection (if the table allows multiple selections), or there are times when you get both if you want to allow the users to select multiple rows. Then you get the table's model and extract the data with that:

e.g.,
 
Alberto Aparicio
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried to use this code in my javacode, but i'm not sure where i should put these code.
I put it in the method public TableDemo() {} but it does not work.
Can you tell me where exatly i should put these code and is these code you gave me complete or should i change it in a way?
I'm really a beginner with java,


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic