aspose file tools*
The moose likes Beginning Java and the fly likes help with clear text button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with clear text button" Watch "help with clear text button" New topic
Author

help with clear text button

Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
im trying to add a button to my program that will clear the text from the text area but i cannot get rid of one error. the program is made up of anumber of panels. the part of the code concerned here is as follows:

JPanel message = new JPanel();
message.add(new JTextArea(20,40), BorderLayout.NORTH);
message.setLayout(new BoxLayout(message, BoxLayout.Y_AXIS));

JButton clearButton = new JButton("Clear");
clearListener cb = new clearListener();
clearButton.addActionListener(cb);

JButton quitButton = new JButton("Quit");
quitListener qt = new quitListener();
quitButton.addActionListener(qt);

JPanel Holder = new JPanel();
Holder.add(message, BorderLayout.NORTH);
Holder.add(style, BorderLayout.SOUTH);
Holder.add(size, BorderLayout.EAST);
Holder.add(quitButton, BorderLayout.SOUTH);
Holder.add(clearButton, BorderLayout.SOUTH);
Holder.setBorder (new TitledBorder(new EtchedBorder(), "Message"));

class quitListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{System.exit(0);}
}

class clearListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{message.setText("");}
}



the quit listener works fine but the clearlistener reveals the error cannot find symbol on the last line:
{message.setText(");}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

That's because the listener does not have a reference to the TextField.

You need some reference to something that can then get access to the TextField.

So you can do this many ways.

In your Listener class you only have the ActionEvent which will only return the Button. But you should be able to get a hold of its container the JPanel Holder. Change the name to lowercase to meet the Coding Standard set by Java.

So the question then becomes, can you get to the TextField from there. Does JPanel have a method to get all the Components that are in the Panel. And then loop through those till you find the TextField.

Personally, that is the long way around. and with OO techniques, it would be easier, but that would take a long time to describe. But basically you would have more classes, like the JPanel Holder being a class with a method called setMessage() and that class has an instance variable holding the JTextfield, that that method setMessage() can simple call the TextFields setText() method.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
thanks mark,
i wasnt completely sure of what you meant but i thought that it may be because "message" is the name of the panel that there was an error. as a result i changed "message" to just a text area but the same error exists:

JTextArea message = new JTextArea(20,40);
message.setLayout(new BoxLayout(message, BoxLayout.Y_AXIS));

JButton clearButton = new JButton("Clear");
clearListener cb = new clearListener();
clearButton.addActionListener(cb);

class clearListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{message.setText("");}
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

It is still a reference issue. Basically in your ActionListener code you do not have access to the reference variable, you need to be able to reference something to call a method on that reference. A JPanel does not have a setText() method in its class.

Mark
Chris Lavery
Ranch Hand

Joined: Nov 30, 2004
Posts: 54
thanks mark, iv got it now.
i was creating the textarea in my buildGUI method so i took it out and put it before the method as a field and it works fine now.
Thanks for your help
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with clear text button
 
Similar Threads
Gui conpiled but no display.
adding a picture
help on reading textfield on 1 class from another class and using it to create a file
Buttons on Java...
adding a picture