Hi all,
While I'm trying to understand the concepts of OO programming (in
Java),
I need to disable some MenuItems and enable them when some action is
performed (in this case clicking on the MenuItem "Lees Breuken").
The disabling of MenuItems is succeeded, but Enabling them is not easy
though.
How can I, for one, enable the button (miSortUp)?
Oh.. and I keep getting this message
"local variable miSortUp is accessed from within inner class; needs to
be
declared final"
but when I declare this variable final (final
miSortUp.setEnabled(true);) I get a message "<identifier> excepted";
why?
Here follows my code..
Thanks!
------------------------------------------------------------------------------
public class RationaalCalculator extends Panel
{
private RationaalView op1rv, op2rv, resrv;
private RationaalInvoer ri;
private Container toetsenpaneel;
... (IRRELEVANT CODE) ...
public void addMenuBar() {
// trucje voor menubar
Object f = getParent ();
while (! (f instanceof Frame))
f = ((Component) f).getParent ();
Frame frame = (Frame) f;
MenuItem miOpen, miSortUp, miSortDown, miSom, miProduct, miExit;
MenuBar mbMain = new MenuBar();
Menu mnuFile = new Menu("Bestand");
miOpen = new MenuItem("Lees breuken");
miOpen.addActionListener(new ActionListener()
{
/**
* @TODO
* @param evt ActionEvent
*/
public void actionPerformed(ActionEvent evt)
{
RationaalLijst rlijst = new RationaalLijst();
try {
RationaalFile ft = new RationaalFile("breuken.txt", rlijst);
String dumpResult = rlijst.dump();
JOptionPane.showMessageDialog(null, dumpResult, "Lees
Breuken", JOptionPane.OK_OPTION);
} catch ( IOException e ) {
System.out.println("File not found, or cannot be opened.");
}
//Enable MenuItems
//Probablt best to set this in the try-catch statement...
//use: mi.enable(); - mi.disable();
// miSortUp.setEnabled(true);
}
});
miSortUp = new MenuItem("Sorteer <");
miSortUp.addActionListener(new ActionListener()
{
/**
* @TODO
* @param evt ActionEvent
*/
public void actionPerformed(ActionEvent evt)
{
}
});
// miSortUp.setEnabled(false);
... (IRRELEVANT CODE) ...
miExit = new MenuItem("Afsluiten");
miExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
System.exit(0);
}
});
mnuFile.add(miOpen);
mnuFile.add(miSortDown);
mnuFile.add(miSortUp);
mnuFile.add(miSom);
mnuFile.add(miProduct);
mnuFile.add(miExit);
mbMain.add(mnuFile);
frame.setMenuBar(mbMain);
}
}
Tor Iver Wilhelmsen - 04 Jan 2004 19:50 GMT
Change the "predeclarations" to be assignments to final variables e.g.
final MenuItem miSortUp = new MenuItem("Sorteer <");
etc.
Then it should work.
(It complains about "must be final" because method variables are
passed by value to the nested class; if it hadn't had that requirement
you would instead wonder why you always got NullPointerException in
the listener that used the (at the time uniniated) variable.
registratie909 - 05 Jan 2004 11:25 GMT
Hey thanks Tor! Your solution really helped!
Now I have another question ;)
As you might have seen I must first run miOpen wherein a file is
loaded into
a ArrayList. Hereafter some other MenuItems get enabled, but when I
run for example miSortUp I need to load the list again. Is there any
other way I can achieve this without loading the complete list from
file again?
see underlying listing
-----------------------------------------------------------------------------
miOpen.addActionListener(new ActionListener()
{
/**
* @TODO
* @param evt ActionEvent
*/
public void actionPerformed(ActionEvent evt)
{
RationaalLijst rlijst = new RationaalLijst();
try {
RationaalFile ft = new RationaalFile("breuken.txt", rlijst);
String dumpResult = rlijst.dump();
JOptionPane.showMessageDialog(null, dumpResult, "Lees
Breuken", JOptionPane.OK_OPTION);
//Enable MenuItems
miSortUp.setEnabled(true);
miSortDown.setEnabled(true);
miSom.setEnabled(true);
miProduct.setEnabled(true);
} catch ( IOException e ) {
System.out.println("File not found, or cannot be opened.");
}
}
});
miSortUp.addActionListener(new ActionListener()
{
/**
* @TODO
* @param evt ActionEvent
*/
public void actionPerformed(ActionEvent evt)
{
RationaalLijst rlijst = new RationaalLijst();
//Sorteren - CBA
try {
RationaalFile ft = new RationaalFile("breuken.txt", rlijst);
rlijst.sortUp();
String dumpResult = rlijst.dump();
JOptionPane.showMessageDialog(null, dumpResult, "Sorteer <",
JOptionPane.OK_OPTION);
}catch ( IOException e ) {
System.out.println("File not found, or cannot be opened.");
}
}
});
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
> Change the "predeclarations" to be assignments to final variables e.g.
>
[quoted text clipped - 8 lines]
> you would instead wonder why you always got NullPointerException in
> the listener that used the (at the time uniniated) variable.
registratie909 - 07 Jan 2004 13:16 GMT
Don't mind it anymore.
The solution was fairly simple (I'm ashamed).
I just needed to create a 'global' variabele of type RationaalLijst
-- RationaalLijst rlijst = new RationaalLijst();
Bye..
> Hey thanks Tor! Your solution really helped!
>
[quoted text clipped - 77 lines]
> > you would instead wonder why you always got NullPointerException in
> > the listener that used the (at the time uniniated) variable.