• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with clear text button

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(");}
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Chris Lavery
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic