Results 1 to 7 of 7
- 07-15-2012, 08:26 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
Nifty method at adding an actionListener, but is it better or worse?
I'm a code organization freak and I've been dwelling over a way to clean up 23 lines of code that repeat "object.addActionListener(this)" with object being a JMenuItem or something. I felt like that 23 lines of code can be shortened a few lines and I've done it! It's nothing special, but would the below code be faster or slower when processing? I assume slower.
Assume menuBar is a JMenuBar. The first loop gets the Menu's of the Menubar, and then that first if statement tests if it is a JMenu or not. If true, it heads on over to the next for loop which get's a menu at I, the control variable of the first loop. The JMenu at i, returns the component count of that menu. If the component found in the menubar is an instanceof Separator it hits continue; . If false, It gets the MenuItem and set's the listener through a method. The instanceof Separator is there because I have separators in my Menu's.Java Code:for (int i = 0; i < menuBar.getComponentCount(); i++) { if (menuBar.getComponent(i) instanceof JMenu) { for (int x = 0; x < menuBar.getMenu(i).getMenuComponentCount(); x++) { if (menuBar.getMenu(i).getMenuComponent(x) instanceof Separator) { continue; } else { JMenuItem get = (JMenuItem) menuBar.getMenu(i).getMenuComponent(x); setListener(get); } } } }
Any thoughts? I hate having lines of code that repeat the same thing, so I cleaned it up with this. Is this faster or slower? Better or for the worse?
-
Re: Nifty method at adding an actionListener, but is it better or worse?
Speed of this code is irrelevant since it is a trivially small piece of code that isn't even called when the GUI is running but rather runs only once when the GUI is being set up. Once the the GUI is running, how you add listeners to menus or buttons doesn't matter at all.
I think though that there are other potential problems here. The speed that matters most in this situation is the time it takes you to debug, maintain and upgrade your application, and having all of your menu items use the same listener is a potential problem since you may be forcing that listener object to do too many unrelated things, to make it disobey the Single Responsibility Principle of object oriented programming.
- 07-15-2012, 07:22 PM #3
Re: Nifty method at adding an actionListener, but is it better or worse?
addActionListener(this) is evil.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 07-15-2012, 09:42 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
- 07-15-2012, 09:55 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
Re: Nifty method at adding an actionListener, but is it better or worse?
So say, If I create a class for every GUI component of a TextEditor (JMenubar, JTabbedPane, JToolBar, etc) then had a main class construct the text editor from every class, would that be bad? Or should I just create the whole GUI in one class.
- 07-15-2012, 11:49 PM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: Nifty method at adding an actionListener, but is it better or worse?
Well its better for you to use something like this:
or maybe like this:Java Code:addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // your code goes here } });
and then inside of your existing class make new private class that implements action listener. It should be something like this:Java Code:addActionListener(new ActionListenerThatIWantToWrite());
Java Code:private class ActionListenerThatIWantToWrite implements ActionListener { @Override public void actionPerformed(ActionEvent e) { // your code goes here } }Last edited by cselic; 07-15-2012 at 11:52 PM.
- 07-16-2012, 07:39 AM #7
Member
- Join Date
- Feb 2012
- Posts
- 28
- Rep Power
- 0
Re: Nifty method at adding an actionListener, but is it better or worse?
I have used your first method many times, but it creates Anonymous classes. When it comes to a menubar that has 20+ items, that creates 20+ anonymous classes in the compiled jar file, which to me is excessive and annoying. I'm starting to use your second method, with the exception I didn't know you could make classes private.
Similar Threads
-
Adding an ActionListener to a drawn Circle
By aortell24 in forum New To JavaReplies: 13Last Post: 06-04-2012, 09:41 PM -
error when adding actionlistener
By nananya in forum New To JavaReplies: 19Last Post: 12-14-2011, 05:44 AM -
Is this the equivalent of wearing both suspenders and a belt, or something worse?
By blackbird in forum New To JavaReplies: 2Last Post: 07-22-2011, 02:09 AM -
Need help adding an actionListener
By HbJgd in forum New To JavaReplies: 1Last Post: 06-12-2011, 05:33 PM -
adding a actionListener but not using inner class
By hariza in forum AWT / SwingReplies: 2Last Post: 10-08-2010, 07:24 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks