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 9 of 9

Thread: JTextField getting text values

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default JTextField getting text values

    Hey guys, I'm trying to make a simple JFrame where the user will input information and the information will then be printed out. The problem is, I have no idea how to acquire the string which the user input. I've tried many different things and the main problem seems to be that I can't add an ActionListener to my JTextField for some reason...

    Here is the full code:

    package layout;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.SpringLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
     
    public class testing2 extends JPanel implements ActionListener {
    	JTextField textField = new JTextField("", 15);
    	protected static JButton b1;
    	protected JButton b2, b3;
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("SpringDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Set up the content pane.
            Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
     
            //Create and add the components.
            JLabel label = new JLabel("Username: ");
            JTextField textField = new JTextField("", 15);
            JLabel label2 = new JLabel("Password: ");
            JPasswordField textField2 = new JPasswordField("", 15);
            JLabel label3 = new JLabel("Re-enter Password: ");
            JPasswordField textField3 = new JPasswordField("", 15);
            JLabel label4 = new JLabel("Email Address: ");
            JTextField textField4 = new JTextField("", 15);
            b1 = new JButton("Finish");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.setMnemonic(KeyEvent.VK_F);
            b1.setActionCommand("Finish");
            contentPane.add(label);
            contentPane.add(textField);
            contentPane.add(label2);
            contentPane.add(textField2);
            contentPane.add(label3);
            contentPane.add(textField3);
            contentPane.add(label4);
            contentPane.add(textField4);
            contentPane.add(b1);
     
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, label,
                                 5,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, label,
                                 5,
                                 SpringLayout.NORTH, contentPane);
     
     
            layout.putConstraint(SpringLayout.WEST, textField,
                                 56,
                                 SpringLayout.EAST, label);
            layout.putConstraint(SpringLayout.NORTH, textField,
                                 5,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
     
           //SET HEIGHT/WIDTH
     
            layout.putConstraint(SpringLayout.WEST, label2,
                                 5,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, label2,
                                 33,
                                 SpringLayout.NORTH, contentPane);
     
     
     
            layout.putConstraint(SpringLayout.WEST, textField2,
                                 57,
                                 SpringLayout.EAST, label2);
            layout.putConstraint(SpringLayout.NORTH, textField2,
                                 33,
                                 SpringLayout.NORTH, contentPane);
     
            //END SET HEIGHT/WIDTH
     
          //SET HEIGHT/WIDTH
     
              layout.putConstraint(SpringLayout.WEST, label3,
                                   5,
                                   SpringLayout.WEST, contentPane);
              layout.putConstraint(SpringLayout.NORTH, label3,
                                   61,
                                   SpringLayout.NORTH, contentPane);
     
     
              layout.putConstraint(SpringLayout.WEST, textField3,
                                   5,
                                   SpringLayout.EAST, label3);
              layout.putConstraint(SpringLayout.NORTH, textField3,
                                   61,
                                   SpringLayout.NORTH, contentPane);
     
              //END SET HEIGHT/WIDTH
     
            //SET HEIGHT/WIDTH
     
                layout.putConstraint(SpringLayout.WEST, label4,
                                     5,
                                     SpringLayout.WEST, contentPane);
                layout.putConstraint(SpringLayout.NORTH, label4,
                                     89,
                                     SpringLayout.NORTH, contentPane);
     
     
     
                layout.putConstraint(SpringLayout.WEST, textField4,
                                     32,
                                     SpringLayout.EAST, label4);
                layout.putConstraint(SpringLayout.NORTH, textField4,
                                     89,
                                     SpringLayout.NORTH, contentPane);
     
                //END SET HEIGHT/WIDTH
     
              //SET HEIGHT/WIDTH
     
                  layout.putConstraint(SpringLayout.WEST, b1,
                                       125,
                                       SpringLayout.WEST, contentPane);
                  layout.putConstraint(SpringLayout.NORTH, b1,
                                       120,
                                       SpringLayout.NORTH, contentPane);
     
                  //END SET HEIGHT/WIDTH
     
            //Display the window.
            frame.pack();
            frame.setSize(325, 200);
            frame.setVisible(true);
        }
     
        [B]
        public void actionPerformed(ActionEvent e) {
        	if ("Finish".equals(e.getActionCommand())) {
        		String x = textField.getText();                       This piece of code here does nothing. :(
        		System.out.println(x);
        	}
    	}
        [/B]
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    Any help would be greatly appreciated


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTextField getting text values

    I don't see where any actionListeners are added to the components. In addition, if you wish to add an ActionListener to a JTextField and filter those events via the action command, you should set the ActionCommand of the JTextField.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: JTextField getting text values

    Well, that's just the problem, I can't add the actionlistener anywhere. For example, I try to add it here:

    private static void createAndShowGUI() {
            ...
            JTextField textField = new JTextField("", 15);
            [B]textField.addActionListener(this);[/B]

    And it comes up with the error "Cannot use this in a static context" I understand what the means but there's just no other way to do it.

  4. #4
    Member
    Join Date
    Oct 2010
    Posts
    40
    Thanks
    0
    Thanked 2 Times in 1 Post

    Default Re: JTextField getting text values

    okay here's what i found in your code

     
    public class Test extends JPanel implements ActionListener {
    	 // I MADE IT STATIC SO THAT WE CAN USE IT ON createAndShowGUI
          static JTextField textField = new JTextField("", 15);
    	protected static JButton b1;
    	protected JButton b2, b3;
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("SpringDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Set up the content pane.
            Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
     
            //Create and add the components.
            JLabel label = new JLabel("Username: ");
     
            // THIS ONE IS THE PROBLEM, THIS OBJECT IS HIDING UR GLOBAL FIELD DELETE THIS LINE
            JTextField textField = new JTextField("", 15);
     
     
            JLabel label2 = new JLabel("Password: ");
            JPasswordField textField2 = new JPasswordField("", 15);
            JLabel label3 = new JLabel("Re-enter Password: ");



    As for your action listener: its better to do this

        public void actionPerformed(ActionEvent e) {
        	if (e.getSource()==b1) {
        		String x = textField.getText();
        		System.out.println(x);
        	}
    }

    so this code works for me:

    package util;
    import javax.swing.AbstractButton;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.SpringLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
     
    public class Test extends JPanel implements ActionListener {
    	static JTextField textField = new JTextField("", 15);
    	protected static JButton b1;
    	protected JButton b2, b3;
     
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("SpringDemo2");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Set up the content pane.
            Container contentPane = frame.getContentPane();
            SpringLayout layout = new SpringLayout();
            contentPane.setLayout(layout);
     
            //Create and add the components.
            JLabel label = new JLabel("Username: ");
      //      JTextField textField = new JTextField("", 15);
            JLabel label2 = new JLabel("Password: ");
            JPasswordField textField2 = new JPasswordField("", 15);
            JLabel label3 = new JLabel("Re-enter Password: ");
            JPasswordField textField3 = new JPasswordField("", 15);
            JLabel label4 = new JLabel("Email Address: ");
            JTextField textField4 = new JTextField("", 15);
            b1 = new JButton("Finish");
            b1.setVerticalTextPosition(AbstractButton.CENTER);
            b1.setHorizontalTextPosition(AbstractButton.LEADING);
            b1.setMnemonic(KeyEvent.VK_F);
            b1.addActionListener(new Test());
            b1.setActionCommand("Finish");
            contentPane.add(label);
            contentPane.add(textField);
            contentPane.add(label2);
            contentPane.add(textField2);
            contentPane.add(label3);
            contentPane.add(textField3);
            contentPane.add(label4);
            contentPane.add(textField4);
            contentPane.add(b1);
     
            //SET HEIGHT/WIDTH
            layout.putConstraint(SpringLayout.WEST, label,
                                 5,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, label,
                                 5,
                                 SpringLayout.NORTH, contentPane);
     
     
            layout.putConstraint(SpringLayout.WEST, textField,
                                 56,
                                 SpringLayout.EAST, label);
            layout.putConstraint(SpringLayout.NORTH, textField,
                                 5,
                                 SpringLayout.NORTH, contentPane);
            //END SET HEIGHT/WIDTH
     
           //SET HEIGHT/WIDTH
     
            layout.putConstraint(SpringLayout.WEST, label2,
                                 5,
                                 SpringLayout.WEST, contentPane);
            layout.putConstraint(SpringLayout.NORTH, label2,
                                 33,
                                 SpringLayout.NORTH, contentPane);
     
     
     
            layout.putConstraint(SpringLayout.WEST, textField2,
                                 57,
                                 SpringLayout.EAST, label2);
            layout.putConstraint(SpringLayout.NORTH, textField2,
                                 33,
                                 SpringLayout.NORTH, contentPane);
     
            //END SET HEIGHT/WIDTH
     
          //SET HEIGHT/WIDTH
     
              layout.putConstraint(SpringLayout.WEST, label3,
                                   5,
                                   SpringLayout.WEST, contentPane);
              layout.putConstraint(SpringLayout.NORTH, label3,
                                   61,
                                   SpringLayout.NORTH, contentPane);
     
     
              layout.putConstraint(SpringLayout.WEST, textField3,
                                   5,
                                   SpringLayout.EAST, label3);
              layout.putConstraint(SpringLayout.NORTH, textField3,
                                   61,
                                   SpringLayout.NORTH, contentPane);
     
              //END SET HEIGHT/WIDTH
     
            //SET HEIGHT/WIDTH
     
                layout.putConstraint(SpringLayout.WEST, label4,
                                     5,
                                     SpringLayout.WEST, contentPane);
                layout.putConstraint(SpringLayout.NORTH, label4,
                                     89,
                                     SpringLayout.NORTH, contentPane);
     
     
     
                layout.putConstraint(SpringLayout.WEST, textField4,
                                     32,
                                     SpringLayout.EAST, label4);
                layout.putConstraint(SpringLayout.NORTH, textField4,
                                     89,
                                     SpringLayout.NORTH, contentPane);
     
                //END SET HEIGHT/WIDTH
     
              //SET HEIGHT/WIDTH
     
                  layout.putConstraint(SpringLayout.WEST, b1,
                                       125,
                                       SpringLayout.WEST, contentPane);
                  layout.putConstraint(SpringLayout.NORTH, b1,
                                       120,
                                       SpringLayout.NORTH, contentPane);
     
                  //END SET HEIGHT/WIDTH
     
            //Display the window.
            frame.pack();
            frame.setSize(325, 200);
            frame.setVisible(true);
        }
     
        //HERE
        public void actionPerformed(ActionEvent e) {
        	if (e.getSource()==b1) {
        		String x = textField.getText();
        		System.out.println(x);
        	}
    	}
     
     
        public static void main(String[] args) {
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

    PS: JUST RENAME THE PACKAGE AND CLASS NAME,

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JTextField getting text values

    Quote Originally Posted by The_Mexican View Post
    And it comes up with the error "Cannot use this in a static context" I understand what the means but there's just no other way to do it.
    That information would have been helpful in your original post. this in a static function is out of context because there is not this. Create an instance of the class, then add that as the action listener.
    testing2 t2 = new testing2();
    textField.addActionListener(t2);

  6. #6
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: JTextField getting text values

    Thanks relixus.

    Copeg, why is there no this in a static function? I dont get it, sorry I'm a beginner xD.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JTextField getting text values

    You need to have that class that you have the text field in having the method

    actionPerformed(ActionEvent e)

    if you use this

  8. #8
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: JTextField getting text values

    Quote Originally Posted by The_Mexican View Post
    Thanks relixus.

    Copeg, why is there no this in a static function? I dont get it, sorry I'm a beginner xD.
    Because this refers to a specific instance of an object.

    Static methods don't have a this because they are defined on the class level, and there is no instance to refer to.

    Read this for more info: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: JTextField getting text values

    You mean you call them like this:

    ClassName.method();

    ?

    Thought so.

Similar Threads

  1. Make some text bold, some NOT bold. JTextField
    By danny_felipe in forum Java Theory & Questions
    Replies: 2
    Last Post: May 27th, 2010, 06:44 PM
  2. Get a certain line in a JTextField
    By FlamingDrake in forum Java Theory & Questions
    Replies: 2
    Last Post: May 14th, 2010, 03:21 PM
  3. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM
  4. Issue with JTextField Locking
    By PekinSOFT.Systems in forum AWT / Java Swing
    Replies: 0
    Last Post: October 1st, 2009, 11:12 AM
  5. [SOLVED] JTextField not visible in swing
    By Sterzerkmode in forum AWT / Java Swing
    Replies: 4
    Last Post: May 21st, 2009, 07:37 AM