Solved

Same result as selecting menu option in code?

Posted on 2007-11-23
19
286 Views
Last Modified: 2010-04-21
I have a JMenuItem with an ActionListener (a separate class), the result is that if you select the menuitem then a filechooser opens to select a file (action A).

Now, when another menu item is chosen and a file value is empty then I want action A to occur as well. How do I do this'? I can add an ActionListener and check if the value is empty but at that point I cannot then add the other actionlistner code....

Any thoughts?
0
Comment
Question by:tomcorcoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 9
19 Comments
 
LVL 24

Expert Comment

by:sciuriware
ID: 20339966
You can add an ActionListener to multiple controls.
May be they are to be different Listeners and to call a common method,
but you don't have to.

Make the ActionListener a class with static (global) state variables.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20340002
the 2nd menu option needs to check a string class member of the frame (not the ActionListener class) is empty before  "executing the first ActionListener"...
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20341859
Well, make that String static ...................................

;JOOP!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tomcorcoran
ID: 20348934
When the 2nd menu item is chosen and a file value is empty then I want action A to occur, if it is not empty i want Action B to occur. Not sure how this second ActionListener could resuse the first one re a static String property?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20349386
Of course such a static member belongs in the top of your GUI class, common to all controls.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20349422
When the 2nd menu item is chosen and a file value is empty then I want action A to occur, if it is not empty i want Action B to occur. Not sure how this second ActionListener could resuse the first one re a static String property in my main JFrame descendant class?
0
 

Author Comment

by:tomcorcoran
ID: 20349436
Your one liners are not giving me enough to go on...how can I call the 1st actionlistener from within a 2ns actionlistenmer
JMenuItem 2ndMenuItem = new JMenuItem("2nd");
2ndMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	 			if (fileName.getText().isEmpty()) {
	 				// call actionlister from 1stMenuItem?
				} else {
	 				Do2nd();	
	 			}
			}
		});        

Open in new window

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20349476
That's not the way to go.
Your ActionListeners should be:
- or separate classes,
- or classes that only implement ActionListener and thus 'actionPerformed()'

The construct your code has I call "just-in-time-classes".
Advantage: simple coding,
Disadvantage: severe access problems.

My style: if I need only 1 actionlistener, I make the GUI class implement ActionListener,
If I need several I first try to split off the action in separate methods you can call at will,
or otherwise I make a new class.
The latter is clumsy and is rare with me.
Remember always that the actionPerformed() will find out what control fired,
so I can (I suppose) always chose what action(s) to call at any event.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20349830
@scuriware... the code example was to try and get a response other than a one liner. The actionListener I want to call is a separate class..I am now looking at passing the parent (JFrame) as a variable to a static method in it (so I can call the do2nd()) which first checks the string value...

...how can I find out the calling component from the actionEvent (in actionPerformed) as you describe?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20350200
// Example for 2 buttons, just the same for menu items:

public class MyGui implements ActionListener
{
.................................................................... intervening code............
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
.................................................................... intervening code............
    button1.addActionListener(this);
    button2.addActionListener(this);
.................................................................... intervening code............
   /** (non-Javadoc)
    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
    * @param a action event.
    */
   public void actionPerformed(ActionEvent a)
   {
      Object o = a.getSource();

      if(o == button1)
      {
         doCallAction1();
      }
      else if(o == button2)
      {
         doCallAction2();
         doCallAction1();
      }

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20350293
as ok, so a.getSource(); will not point to calling frame button/menuitem....does nto seem to be a solution other than including JFrame ref in the ActionListener class :-(
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20350755
>>> will not point to calling frame button/menuitem

It WILL point!

>>> does nto seem to be a solution

I wonder why you persist your construction? This is a much more open solution.

;JOOP!
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 80 total points
ID: 20380262
I object. The questioner is a bit stubborn.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20380580
so i will have to give points for free?
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20380805
You got a sincere advice: that's an answer.
You can redraw your question if there was no solution.

But, it's your decision .........................................................

;JOOP!
0
 

Author Closing Comment

by:tomcorcoran
ID: 31410685
forced by expert to give points
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 20383401
I can live with that.
Please re-consider my advice.

;JOOP!
0
 

Author Comment

by:tomcorcoran
ID: 20383418
will be requesting expert not to add comments to my questions in future
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iterator/ListIterator approach 17 68
null output 3 59
Crystal Reports Licensing Questions 4 86
Given a github repo find email address of all collaborators in that repo 1 56
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question