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.

Results 1 to 2 of 2

Thread: press a button, get textfield content

  1. #1
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    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
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    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);
     
    		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.

    Looking for a Java job? Visit - Java Programming Careers

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, 11:15 AM
  3. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 11:57 AM
  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