wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to copy from 4 JTextFields to 1 JTextArea Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to copy from 4 JTextFields to 1 JTextArea" Watch "How to copy from 4 JTextFields to 1 JTextArea" New topic
Author

How to copy from 4 JTextFields to 1 JTextArea

Alberto Aparicio
Greenhorn

Joined: Oct 01, 2010
Posts: 12
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);

}

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




[Thumbnail for javaTelling.gif]

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And welcome to the JavaRanch
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

I would recommend using JTextArea#append(String text) instead of concatenating and then setting the text.
Moving thread to the UI forums.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Alberto Aparicio
Greenhorn

Joined: Oct 01, 2010
Posts: 12
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

Joined: Oct 01, 2010
Posts: 12
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!


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Alberto Aparicio
Greenhorn

Joined: Oct 01, 2010
Posts: 12
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

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3595
    
  14

It's simple. With veld5, just append, instead of setText.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12
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!


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




[Thumbnail for java Telling 2.gif]

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7


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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12
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)


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12
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.






[Thumbnail for howitis.gif]


[Thumbnail for How I want it.gif]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 01, 2010
Posts: 12
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Oct 01, 2010
Posts: 12
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12
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.



[Thumbnail for How the result is now.gif]


[Thumbnail for How the result should be.gif]

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Oct 01, 2010
Posts: 12

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,


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to copy from 4 JTextFields to 1 JTextArea
 
Similar Threads
Can't Update Record ?
ui manager getColor and link JOptionpane OK button to code
Writing to file problems
Can't Update Record ?
Event problems