Pass value from one JtextField to another? - java

//New to java swing and need help getting the text in the first Jtextfield to display in the //second second jtextfield???? Im young and just starting out in java and need some help. below is the code i have done already thanks
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class lab4 extends JFrame implements ActionListener {
int numClicks = 0;
String text = null;
public lab4() {
setSize(1200, 700);
setVisible(true);
JButton button = new JButton("Hello i am a button");
button.addActionListener(this);
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2));
panel.add(button);
this.add(panel);
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem open = new JMenuItem("Open File");
file.add(open);
final JTextField myField = new JTextField(10);
myField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = myField.getText();
//System.out.println("Hello");
}
});
final JTextField myField2 = new JTextField(10);
yField2.setText(myField.getText());
panel.add(myField);
panel.add(myField2);
setVisible(true);
}
}
public static void main(String[] args) {
new lab4();
}
public void actionPerformed(ActionEvent e) {
numClicks++;
System.out.println("The button has been clicked " + numClicks + " times");
}
}

Yes, you are doing ok. If more then one work going to happen in sequence on one action event, then you need to put the sequence inside the corresponding actionPerformed function. So:
myField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = myField.getText();
myField2.setText(text);
}
});
JComponents listeners mean that they will listen and respond only when an action event occur. They will be notified by the instance of ActionListener registered to event source(JCompnent) with addActionListener() function as you have done.
One more thing to note: you can't access a field in any statement before even declaring it. Compiler's need to know the information about the field before doing anything with it. So you must declare myField2 before the accessing-code of it, such as, myField1's anonymous class ActionListener actionPerformed function.
Tutorial Resources:
Writing Event Listeners
Anonymous Class

You can share the model:
JTextField textField1 = new JTextField(...);
JTextField textField2 = new JTextField(...);
textField2.setDocument( textField1.getDocument() ):
Now whenever you type text in either text field the other will also be updated.

Related

How to add 2 elements from 2 different JTextField to one JList

How can I add elements from different JTextFields to one List. I've tried to put the elements in Strings and add them to the list but thats not working.
You have to add the strings to the list model that backs up the JList. Here is short example code that appends the current JTextField's value to the list whenever you hit ENTER in the text field:
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.BorderLayout;
public final class Example extends JFrame {
public Example() {
setLayout(new BorderLayout());
// Create a list model and populate it with two initial items.
final DefaultListModel<String> model = new DefaultListModel<String>();
model.addElement("Initial 1");
model.addElement("Initial 2");
// Create a JList (wrapped into a JScrollPane) from the model
add(new JScrollPane(new JList<String>(model)), BorderLayout.CENTER);
// Create a JTextField at the top of the frame.
// Whenever you click ENTER in that field, the current string gets
// appended to the list model and will thus show up in the JList.
final JTextField field1 = new JTextField("Field 1");
add(field1, BorderLayout.NORTH);
field1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Append " + field1.getText());
model.addElement(field1.getText());
}
});
// Create a JTextField at the bottom of the frame.
// Whenever you click ENTER in that field, the current string gets
// appended to the list model and will thus show up in the JList.
final JTextField field2 = new JTextField("Field 2");
add(field2, BorderLayout.SOUTH);
field2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Append " + field2.getText());
model.addElement(field2.getText());
}
});
}
public static void main(String[] args) {
final JFrame frame = new Example();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(1); }
});
frame.pack();
frame.setVisible(true);
}
}

Why is my JPanel empty after button is clicked?

I'm learning how to create event listeners in Java for a button click. I want a panel to popup with form items. I've built the panel in the action listener's contructor, but it's empty when it opens. I thought it makes sense to only build this one, then just show it when the button is clicked (actionPerformed). Obviously not :)
Below is my ActionListener class:
package biz.martyn.budget;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class NewTransaction implements ActionListener {
protected JPanel panel = new JPanel(new GridLayout(0, 1));
public void NewTransaction() {
String [] category = {"Internet", "Clothes", "Rent", "Salary", "Groceries"};
JComboBox combo = new JComboBox(category);
panel.add(combo);
panel.add(new JLabel("Description:"));
JTextField desc = new JTextField();
panel.add(desc);
panel.add(new JLabel("Date:"));
JTextField date = new JTextField();
panel.add(date);
panel.add(new JLabel("Amount:"));
JTextField amount = new JTextField();
panel.add(amount);
}
#Override
public void actionPerformed(ActionEvent arg0) {
int result = JOptionPane.showConfirmDialog(null, panel, "New transaction",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
}
}
Here is how I'm attaching the event:
JButton newTransactionButton = new JButton("New transaction");
newTransactionButton.addActionListener(new NewTransaction());
toolbar.add(newTransactionButton);
I'd appreciate any additional advice on conventions when doing this sort of thing coz I'm quite the beginner, thanks.
I've built the panel in the action listener's contructor, but it's empty when it opens
The following...
public void NewTransaction() {
...is a method, not a Constructor. You need to explicitly call it, or change it to a constructor
public NewTransaction() {

How do i print a line of text when typing a certain number/word with a JTextField and JTextArea (JAVA)

MY CODE
package pack.TAgame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class AL_Test {
public static void main(String[] args){
//JFrame
JFrame Frame = new JFrame("AL Test");
Frame.setSize(720,480);
Frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Frame.setVisible(true);
Frame.setResizable(false);
//Text Area
JTextArea textArea = new JTextArea();
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.GRAY);
JLabel jl = new JLabel();
Frame.add(textArea);
textArea.setEditable(false);
//Text Field
JTextField jt = new JTextField(50);
JPanel jp = new JPanel();
jp.add(jt);
Frame.add(jt, BorderLayout.SOUTH);
class UserInput extends JFrame implements ActionListener{
public UserInput() {
}
public void actionPerformed(ActionEvent e) {
JTextField jt = new JTextField();
JTextArea textArea = new JTextArea();
jt.addActionListener(this);
String text = jt.getText();
textArea.append("\n>What does the scouter say about his power level?");
}
}
}
}
This maybe a little like my other post...(In my code for the actionlistener part I gave up and I might have some useless code in it)
so basically I want a JTextField that when I type a certain command to print something into my JTextArea. (Without the use of a button (i.e. Press enter))
Start with the TextDemo.java code from the Swing tutorial on How to Use Text Fields. This example does exactly what you want:
it adds an ActionListener to the text field. The ActionListener is invoked when Enter is pressed and focus is on the text field.
The ActionListener will take the text from the text field and append it to a text area.
As a bonus you will also learn how to better structure your code by adding the components to a panel and then add the panel to the frame. The variables will be defined in the class as well.

Input from JOptionPane, output in JTextArea in JFrame?

How do you make a JTextArea in the JFrame that accepts multiple input from JOptionPane? Is that even possible? Thanks to whoever helps!
Create a new class and extend JFrame
add a JTextArea to it. Make it a member variable
add a button to the frame. In the action method call open the input dialog
when the dialog returns, append the text to the JTextArea using its append method (don't forget to check for empty/null string)
Here is a sample program:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class InputTest extends JFrame {
private final JTextArea textarea;
private final JButton button;
public InputTest(String title) {
super(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textarea = new JTextArea(5,30);
button = new JButton("new input");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String input = JOptionPane.showInputDialog(InputTest.this, "Please enter some text");
if((input != null) && !input.isEmpty()) {
textarea.append(input);
textarea.append(System.getProperty("line.separator"));
}
}
});
JPanel p = new JPanel(new BorderLayout());
p.add(textarea, BorderLayout.NORTH);
p.add(button, BorderLayout.SOUTH);
this.getContentPane().add(p);
}
public static void main(String[] args) {
InputTest it = new InputTest("Input Test");
it.setSize(200, 200);
it.setVisible(true);
}
}

Swing/Java: How to use the getText and setText string properly

I'm trying to make input nameField appear in a Label called label1 after a Button called button1 is clicked.
Right now it says: 'txt' and I understand why. But I don't know how I can use the string!
Can anyone explain me what I'm doing wrong and how to use this string properly?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class thisismytest2 {
public static void main(String[] args) {
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JTextField nameField = new JTextField("...", 2);
JButton button1 = new JButton();
final JLabel label1 = new JLabel();
label1.setText("txt");
label1.setVisible(false);
String txt = nameField.getText();
frame.add(panel);
panel.add(button1);
panel.add(label1);
frame.setSize(200,200);
frame.setVisible(true);
panel.add(nameField);
frame.setSize(600,400);
nameField.setBounds(400, 40, 400, 30);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label1.setVisible(true);
}
});
}
}
You are setting the label text before the button is clicked to "txt". Instead when the button is clicked call setText() on the label and pass it the text from the text field.
Example:
label1.setText(nameField.getText());
in your action performed method, call:
label1.setText(nameField.getText());
This way, when the button is clicked, label will be updated to the nameField text.
the getText method returns a String, while the setText receives a String, so you can write it like label1.setText(nameField.getText()); in your listener.
Setup a DocumentListener on nameField. When nameField is updated, update your label.
http://download.oracle.com/javase/1.5.0/docs/api/javax/swing/JTextField.html

Categories

Resources