Results 1 to 5 of 5
  1. #1
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

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

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Using anonimous class on JTextField

    Do you mean adapter classes?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Mapisto is offline Senior Member
    Join Date
    Dec 2011
    Posts
    102
    Rep Power
    0

    Default Re: Using anonimous class on JTextField

    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
    				
    			}
    		});
    Here's the example:
    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!!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. JTextField on JPanel-class within JFrame-class
    By floris in forum AWT / Swing
    Replies: 5
    Last Post: 06-25-2012, 01:54 PM
  2. Calling a JTextField from another class
    By Mike. in forum New To Java
    Replies: 16
    Last Post: 05-15-2012, 03:36 AM
  3. Replies: 5
    Last Post: 02-25-2012, 04:56 AM
  4. Getting class attribute values from JTextField
    By raverz2 in forum AWT / Swing
    Replies: 8
    Last Post: 08-05-2011, 09:05 PM
  5. Replies: 1
    Last Post: 01-30-2009, 07:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •