473,800 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JMenuItem ActionListener help

Stubert
20 New Member
Hi there just a quick question, if I have multiple JMenuItems do I need to create an inner class ActionListener for each and every JMenuItem I create? Or is there a more efficient way of doing it?

I know I could just use event.getSource and check it all in one big ActionListener, but I've been informed that this is bad pracitce and should be done using inner classes.

Just wondering if there were any other ways of achieving this?

Thanks,

Stuart
Apr 21 '08 #1
4 2874
sukatoa
539 Contributor
do I need to create an inner class ActionListener for each and every JMenuItem I create?
Yep... you can..... and it is a good practice... but it consumes more lines of code.....

See the Sun's Java tutorials for more details about it.....

regards,
sukatoa
Apr 21 '08 #2
JosAH
11,448 Recognized Expert MVP
Hi there just a quick question, if I have multiple JMenuItems do I need to create an inner class ActionListener for each and every JMenuItem I create? Or is there a more efficient way of doing it?

I know I could just use event.getSource and check it all in one big ActionListener, but I've been informed that this is bad pracitce and should be done using inner classes.

Just wondering if there were any other ways of achieving this?

Thanks,

Stuart
Or, alternatively create an Action object for a JMenuItem. It fully populates and
decorates a JMenuItem and handles whatever needs to be done when the JMenuItem
is pressed.

Going the getSource() route ends up in a silly sequence of if-statements and you'll
end up with much more code at the end. Better create a little class for your item:
one class per item; it's easier to maintain than just one big method with a bunch
of if-statements in it. The little Action classes don't even need to be inner classes.

kind regards,

Jos
Apr 21 '08 #3
r035198x
13,262 MVP
... also ask aunt Google about the Command pattern.
Apr 21 '08 #4
Stubert
20 New Member
Thanks for the replies, at first I was using a stupid amount of if statements in one actionPerformed method and it was just too messy to understand, hence the first post. I'm going to look up the Command pattern now and I'll get back to you guys.

Thanks again,

Stuart
Apr 21 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
6564
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help Workshop program: hcw.exe that's included with Visual Basic. This exact same file compiled perfectly with no notes, warnings or errors prior to reformatting my system. Prior to the reformatting, I copied the help.rtf file onto a CD and checked the box to...
2
2385
by: Hal Vaughan | last post by:
I have a "wizard" type program with a series of panels that are placed in a JPanel. When the "Next >" button is pressed, I put in a new panel (in other words, the ActionListener for "Next >" selects the proper panel from a CardLayout). Some of these panels, when selected, perform actions immediately. When that happens, the panel is not displayed until the action is complete. I've tried invalidating the layout and repainting, but it...
1
5982
by: Patrick Vanhoof | last post by:
Hi, I have a small program to test the ActionListener, but it gives errors. Here's part of the code: class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { ....
0
1807
by: linux newbie | last post by:
is there any java expert could tell me what the problem below and explain the code to me, your explanation is greatly appreciated :) static Class class$java$awt$event$ActionListener; static Class _mthclass$(String x$0) { try
1
3109
by: cnixuser | last post by:
Hello, I am currently attempting to implement a simple actionlistener for a button in a JFrame that was created via "drag and drop" with the Netbeans 5.0 IDE, the code that I am using to implement the actionlistener should work, and is taken from other functional examples ;however, for some reason the compiler kicks out the following error :/home/vontux/cis435lab1/src/UserInterface.java:103: <identifier> expected btn_roll.addActionListener(...
1
3917
by: Phil Latio | last post by:
Quite a while back I used Java Swing (for Uni project) which allowed the developer to a create form and on the "Submit" button I recall adding something called an ActionListener. I was thinking of trying to do the something similar in PHP but a bit puzzled on how I would achieve it. My thoughts are to have a class for a Textbox, class for a SubmitButton and a class called ActionListener which is invoked (is that the correct term??) when...
3
2426
by: shailajaanand | last post by:
can we add Actionlistener to textarea?
3
2796
by: stmfc | last post by:
KeyListener listens keyboard strokes, MouseListener listens mouse events, etc. My question is about ActionListener. what kind of events does ActionListener listen? its name does not give much information, or does it listen all possible events? can you give an example?
2
3768
by: yeshello54 | last post by:
so here is my problem...in a contact manager i am trying to complete i have ran into an error..we have lots of code because we have some from class which we can use...anyways i keep getting an error when i do the following. if you add a contact with up to 13 fields it will be stored in a data structure. i have a tabbed pane that will show six of the 13 fields for that contact. when you double click the contact i want it to pop up and show all 13...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10279
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.