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?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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
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:
As you can see, the text in the JTextField is displayed in the popup when you click the button.
Please use [highlight=Java] code [/highlight] tags when posting your code.
Forum Tip: Add to peoples reputation by clicking the button on their useful posts.