Results 1 to 4 of 4
  1. #1
    patman is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default GUI TextField issues

    Hey, Im working through a project right now and im struggling with getting the text from the text field. Here is part of my code
    Java Code:
    	public Input(){
    		Fun = new JTextField("Function", 12);
    		Var = new JTextField("Variable", 12);
    		Start = new JTextField("Start", 12);
    		End = new JTextField("End", 12);
    		ActionListener buttonListener = new ButtonListener();
    		JButton button = new JButton("Graph");
    		button.addActionListener(buttonListener);
    		TPanel = new JPanel(null);
    		TPanel.setLayout(new GridLayout(1, 5));
    		TPanel.add(Fun);
    		TPanel.add(Var);
    		TPanel.add(Start);
    		TPanel.add(End);
    		TPanel.add(button);
                    add(TPanel);
    	}
    	
    }
    class ButtonListener implements ActionListener{
    	
    	public void actionPerformed(ActionEvent e) {
    		Poly p = new Poly(Fun.getText());
    	}
    	
    }
    Poly is an object I created in another class. But i am getting an error with the Fun.getText() line. Im sure Im just doing this the wrong way so Im just looking for the right way to do it.
    Thanks

  2. #2
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    106
    Rep Power
    0

    Default Re: GUI TextField issues

    Try putting your ButtonListener class into the same class as the Input() constructor. I think that this will do the trick.

  3. #3
    patman is offline Member
    Join Date
    Dec 2012
    Posts
    3
    Rep Power
    0

    Default Re: GUI TextField issues

    Mint dude! I think that did it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: GUI TextField issues

    You can have the listener in a separate class, and often you'll want to do that, but it will need some way to call the methods of the GUI class, and so will need a reference to the GUI passed in to it, possibly via a constructor parameter.

Similar Threads

  1. Won't set to textfield
    By Steffers in forum New To Java
    Replies: 7
    Last Post: 11-04-2010, 03:32 PM
  2. Draw TextField
    By PhQ in forum New To Java
    Replies: 6
    Last Post: 07-17-2010, 02:30 AM
  3. TextField Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:21 PM
  4. JSP - getting value from a textfield
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 09:58 PM

Posting Permissions

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