boardtc
asked on
Same result as selecting menu option in code?
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?
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?
ASKER
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"...
Well, make that String static .......................... .........
;JOOP!
;JOOP!
ASKER
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?
Of course such a static member belongs in the top of your GUI class, common to all controls.
;JOOP!
;JOOP!
ASKER
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?
ASKER
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();
}
}
});
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!
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!
ASKER
@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?
...how can I find out the calling component from the actionEvent (in actionPerformed) as you describe?
// 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.ActionListe ner#action Performed( java.awt.e vent.Actio nEvent)
* @param a action event.
*/
public void actionPerformed(ActionEven t a)
{
Object o = a.getSource();
if(o == button1)
{
doCallAction1();
}
else if(o == button2)
{
doCallAction2();
doCallAction1();
}
;JOOP!
public class MyGui implements ActionListener
{
..........................
JButton button1 = new JButton("One");
JButton button2 = new JButton("Two");
..........................
button1.addActionListener(
button2.addActionListener(
..........................
/** (non-Javadoc)
* @see java.awt.event.ActionListe
* @param a action event.
*/
public void actionPerformed(ActionEven
{
Object o = a.getSource();
if(o == button1)
{
doCallAction1();
}
else if(o == button2)
{
doCallAction2();
doCallAction1();
}
;JOOP!
ASKER
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 :-(
>>> 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!
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!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
so i will have to give points for free?
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!
You can redraw your question if there was no solution.
But, it's your decision ..........................
;JOOP!
ASKER
forced by expert to give points
I can live with that.
Please re-consider my advice.
;JOOP!
Please re-consider my advice.
;JOOP!
ASKER
will be requesting expert not to add comments to my questions in future
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!