Results 1 to 5 of 5
- 07-28-2012, 04:45 PM #1
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Using anonimous class on JTextField
Hi,
I've been trying to use an anonimous class to declare some actions in a textfield.
The problem is that when using anonimous class, it implements and overrides all methods, and I want to use only 1 of them.
I want to use only 1 method of Action interface.
Is there anyway to use only one of the methods without having to override all the methods?
Thanks!
- 07-28-2012, 05:09 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Using anonimous class on JTextField
Do you mean adapter classes?
- 07-28-2012, 05:09 PM #3
Re: Using anonimous class on JTextField
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
If you implement an interface, you have to provide implementations of all its methods. but you already know that.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-28-2012, 06:02 PM #4
Senior Member
- Join Date
- Dec 2011
- Posts
- 102
- Rep Power
- 0
Re: Using anonimous class on JTextField
Here's the example:Java Code:finalAmount.setAction(new Action() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } @Override public void setEnabled(boolean b) { b=false; } @Override public void removePropertyChangeListener(PropertyChangeListener arg0) { // TODO Auto-generated method stub } @Override public void putValue(String arg0, Object arg1) { // TODO Auto-generated method stub } @Override public boolean isEnabled() { // TODO Auto-generated method stub return false; } @Override public Object getValue(String arg0) { // TODO Auto-generated method stub return null; } @Override public void addPropertyChangeListener(PropertyChangeListener arg0) { // TODO Auto-generated method stub } });
I've been trying to make the JTextField (finalAmount object) unenabled for changes by the user. although it worked, I wanted to save the default properties of all the other fields without declaring every single one of them by myself.
It's like using the paintComponent method of JLabel. If I want to draw something but also wants to keep all the default properties I use super.paintComponent(g) .
So I was trying to find a way of doing the same thing with the problem above.
eRaaaa,
I've just read about the adapter classes (first time I've heard about them so thanks). I've been looking for a simpler way, shorter way of doing it- that's why I've been using the anonymous class option in the first place. If I've got the idea right, the adapter class also needs a long declaration.
If there's a good and short way to do it, I'd realy appreciate it if you could write me.
Thanks again!!
- 07-28-2012, 06:44 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
Re: Using anonimous class on JTextField
There isn't a shorter way; if you want to define a concrete class that implements an interface, you have to implement all the methods defined in the interface; the Action interface defines six methods and its super interface defines one method so you have to implement seven method. There's also an abstract class AbstractAction that implements most of those methods (and then some); maybe it can be of help to you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
JTextField on JPanel-class within JFrame-class
By floris in forum AWT / SwingReplies: 5Last Post: 06-25-2012, 12:54 PM -
Calling a JTextField from another class
By Mike. in forum New To JavaReplies: 16Last Post: 05-15-2012, 02:36 AM -
GUI Passing JTextField value to Logic class upon Button Click
By Redefine12 in forum New To JavaReplies: 5Last Post: 02-25-2012, 03:56 AM -
Getting class attribute values from JTextField
By raverz2 in forum AWT / SwingReplies: 8Last Post: 08-05-2011, 08:05 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks