Reputation: 175
How do I display the data from a JTextField
into a JTextArea
?
I'm trying to take input from four JTextField
and show them in a JTextArea
when a button is clicked. I have the ActionListener
working. I am just unsure how to get the input to the JTextArea
.
My JTextArea
is called 'ShowActions'. And I am getting some lines of code appearing in the JTextArea
. But they are not the inputs I desired so bad.....
Can someone please point me in the right direction please?
private JPanel JPanel1 (){
JP1 = new JPanel();
//Style JP1
JP1.setLayout(new GridLayout(8, 2));
JP1.setBackground(Color.RED);
JP1.setBorder(new EmptyBorder(20, 20, 20, 20));
//Make bits and name them
heading1 = new JLabel ("Add Landlord");
Font font = new Font("Serif", Font.ITALIC + Font.BOLD, 28);
heading1.setFont(font);
spacer1 = new JLabel (" ");
LLNameT = new JLabel ("Enter Landlord name");
LLName = new JTextField(30);
LLAddressT = new JLabel ("Enter Landlord Address ");
LLAddress = new JTextField(40);
LLPhoneT = new JLabel ("Enter Landlod Phone No.");
LLPhone = new JTextField(10);
LLbankDeetsT = new JLabel ("Enter Landlod Bank details");
LLbankDeets = new JTextField(10);
sub1 = new JButton("Submit");
//Add bits to panel
JP1.add(heading1);
JP1.add(spacer1);
JP1.add(LLNameT);
JP1.add(LLName);
JP1.add(LLAddressT);
JP1.add(LLAddress);
JP1.add(LLPhoneT);
JP1.add(LLPhone);
JP1.add(LLbankDeetsT );
JP1.add(LLbankDeets);
JP1.add(sub1);
//Set Action Listener
event1 JP1sub1 = new event1();
sub1.addActionListener(JP1sub1);
return(JP1);
}
//Activate ActionListener
public class event1 implements ActionListener{
public void actionPerformed(ActionEvent JP1sub1){
ShowActions.setText(LLName + "\n" + LLAddress + "\n" + LLPhone + "\n" + LLbankDeets);
}
}
Upvotes: 0
Views: 876
Reputation: 1206
You are very close you need to do one more thing and that is getting the tekst from the labels and textfield by calling getText()
.
public class event1 implements ActionListener{
public void actionPerformed(ActionEvent JP1sub1){
ShowActions.setText(LLName.getText() + "\n" + LLAddress.getText() + "\n" + LLPhone.getText() + "\n" + LLbankDeets.getText());
}
}
Hope this helps :)
Upvotes: 1