Results 1 to 4 of 4
  1. #1
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Exclamation How to check a property of a textfield located in a constructor to an Action Listener

    I have a Constructor:
    Java Code:
    Main() {
    		JLabel label = new JLabel();
    		JTextField textField = new JTextField("HahAHah",0);
    		JButton mainButton = new JButton("Do stuff");
    		FlowLayout layout = new FlowLayout();
    		mainButton.addActionListener(this);
    		int X;
    		int Y;
    		X = 300; Y = 100;
    		setSize(new Dimension(X,Y));
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setLocation(100,100);
    		setLayout(layout);
    		add(mainButton);
    		add(textField);
    	}
    And a Action Listener:

    Java Code:
    @Override
    public void actionPerformed(ActionEvent e) {
    	int randNum = new Random().nextInt(10) + 1;
    	printwithnewline(randNum);
    }
    How can I check text from the constructor in mu Action Listener? When I try to textField.getText() in the action listener, it says textField is not defined.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: How to check a property of a textfield located in a constructor to an Action List

    So declare it as an instance field

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: How to check a property of a textfield located in a constructor to an Action List

    Nevermind I figured it out...

  4. #4
    rokit boy is offline Member
    Join Date
    Feb 2012
    Posts
    35
    Rep Power
    0

    Default Re: How to check a property of a textfield located in a constructor to an Action List

    Quote Originally Posted by DarrylBurke View Post
    So declare it as an instance field

    db

    That's exactly what I've done :D

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  2. mouse click listener from textfield
    By ryanmk54 in forum AWT / Swing
    Replies: 2
    Last Post: 05-11-2011, 02:22 AM
  3. Replies: 5
    Last Post: 11-14-2010, 04:28 PM
  4. How to set the property of Bean by constructor injection
    By Java Tip in forum Spring Framework
    Replies: 1
    Last Post: 05-05-2008, 09:01 AM
  5. Replies: 0
    Last Post: 10-04-2007, 09:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •