Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,811 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,695 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

PeRFeCK

14 Apr, 2009 - 08:04 AM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 5

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 smile.gif

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:

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!!


User is offlineProfile CardPM
+Quote Post


markhazlett9

RE: Issues Getting Text From Textfield On Button Press

14 Apr, 2009 - 08:35 AM
Post #2

Coding is a lifestyle
Group Icon

Joined: 12 Jul, 2008
Posts: 1,463



Thanked: 48 times
Dream Kudos: 25
My Contributions
Instantiate text field at the class level. In other words do this...

CODE

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



Hope this helps... Cheers
User is offlineProfile CardPM
+Quote Post

PeRFeCK

RE: Issues Getting Text From Textfield On Button Press

14 Apr, 2009 - 01:30 PM
Post #3

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 5

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 Apr, 2009 - 01:40 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:54PM

Live Java Help!

Be Social

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

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month