School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,029 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,090 people online right now. Registration is fast and FREE... Join Now!



Issues getting text from textfield on button press

Issues getting text from textfield on button press Rate Topic: -----

#1 PeRFeCK  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 5
  • Joined: 14-April 09


Dream Kudos: 0

Post icon  Posted 14 April 2009 - 08:04 AM

Hello,

I have been stuck writing this program for 3 days now and every example I have come across seems to do it exactly the same way I am trying. I do not know what I am missing, its obviously something easy, someone please point it out to me :)

Basically I need to get text from a textfield on button press. My error is the actionPerformed can't see the text field. I have tried in an abstract method, a seperate class, and in the same class, all failed. I need to do more stuff to the text it after this happens, but I figure after I get over this hump, the rest will be easy.

Current Compiling Issues:

cannot find symbol method addActionListener(TheFrame)
I got this recently since i changed to 'implimenting ActionListener' in my TheFrame class, was not having trouble with this prior when I had a seperate class to handle it and made an object of it in TheFrame.

cannot find symbol variable textField
This is my main problem! The actionPerformed can't find textField??? What do I need to do to get it to see textField.

The Code:

public class TheFrame extends JFrame implements ActionListener
{
	public TheFrame()
	{
		JPanel center = new JPanel();
		JPanel top = new JPanel();
		JPanel bottom = new JPanel();
		
		JLabel label1 = new JLabel("Text Field");
		JLabel label2 = new JLabel("Text Area");
		JLabel label3 = new JLabel("Combo Box");
		
		JButton button = new JButton("Move");
		JComboBox comboBox = new JComboBox();
		JTextArea textArea = new JTextArea(10,20);
		JTextField textField = new JTextField(20);
		
		button.addActionListener(this);
		comboBox.addActionListener(this);
		textArea.addActionListener(this);
		textField.addActionListener(this);
		
		center.setLayout(new FlowLayout());
		center.add(label1);
		center.add(textField);
		center.add(label2);
		center.add(textArea);
		center.add(label3);
		center.add(comboBox);
	
		top.setBackground(Color.WHITE);
		top.add(new JLabel("By ..."));
		
		bottom.setBackground(Color.WHITE);
		bottom.add(button);
		
		add(top, BorderLayout.NORTH);
		add(center, BorderLayout.CENTER);
		add(bottom, BorderLayout.SOUTH);
	}

	public void actionPerformed(ActionEvent e)
	{
		String text = textField.getText();
		System.out.println(text);
	}
	
	public static void main(String[] arrrrggggggghhhhhs)
	{
		TheFrame frame = new TheFrame();
		frame.setTitle("Action Event Demo");
		frame.setSize(300, 350);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}



Thanks in advance!!
Was This Post Helpful? 0
  • +
  • -


#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • Icon
  • View blog
  • Group: Greeters
  • Posts: 1,659
  • Joined: 12-July 08


Dream Kudos: 25

Posted 14 April 2009 - 08:35 AM

Instantiate text field at the class level. In other words do this...

public class TheFrame extends JFrame implements ActionListener
{
private JTextField textField;
	public TheFrame()
	{
//rest of your code




Hope this helps... Cheers
Was This Post Helpful? 1
  • +
  • -

#3 PeRFeCK  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: New Members
  • Posts: 5
  • Joined: 14-April 09


Dream Kudos: 0

Posted 14 April 2009 - 01:30 PM

thanks! i had another issue posted here but i figured it out so editing it out...appreciate the help.

This post has been edited by PeRFeCK: 14 April 2009 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month