i can get the content of a textfield when pressing enter in the text field, but how do u get the content of the textfield by pressing a button?
Printable View
i can get the content of a textfield when pressing enter in the text field, but how do u get the content of the textfield by pressing a button?
Hello chopficaro.
Welcome to the Java Programming Forums.
I wrote you this example code to look at. I hope it helps :)
Code :import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JTextField; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JOptionPane; public class Chopficaro extends JPanel { public Chopficaro(){ final JTextField txt = new JTextField("Java Programming Forums"); txt.setBounds(1, 1, 300, 30); JButton button = new JButton(" >> CLICK ME <<"); button.setBounds(40, 40, 200, 40); setLayout(null); add(txt); add(button); [B]// Add action listener to button[/B] [B] button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // Execute when button is pressed //System.out.println("You clicked the button"); String getTxt = txt.getText(); int messageType = JOptionPane.PLAIN_MESSAGE; JOptionPane.showMessageDialog(null, getTxt, "Java Programming Forums!!", messageType); } });[/B] } public static void main(String[] args) { JFrame frame1 = new JFrame("JavaProgrammingForums.com"); frame1.getContentPane().add(new Chopficaro()); frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame1.setSize(310, 130); frame1.setVisible(true); } }
Output:
http://javaprogrammingforums.com/jav...a_example1.jpg
As you can see, the text in the JTextField is displayed in the popup when you click the button.