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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: press a button, get textfield content

  1. #1
    Join Date
    May 2010
    Thanked 0 Times in 0 Posts

    Default press a button, get textfield content

    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?

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 294 Times in 227 Posts
    Blog Entries

    Default Re: press a button, get textfield content

    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);
    		[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);
    	public static void main(String[] args) {
    		JFrame frame1 = new JFrame("JavaProgrammingForums.com");
    		frame1.getContentPane().add(new Chopficaro());
    		frame1.setSize(310, 130);


    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.

Similar Threads

  1. Auto Fill the TextField
    By malladiG in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2010, 07:32 AM
  2. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  3. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 12:57 PM
  4. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM