M
Marcello
Which is the better way or any other advice?
I know a fifth way with actionPerformed in an anonymous class, but I
don't like it in this app.
MyAppFrame.java is the GUI implementation of MyApp.java.
A and B are two items in a menu and two buttons in the toolbar:
public class MyAppFrame extends JFrame {
//Constructors
public MyAppFrame(String title) {
setTitle(title);
... // add A, B to menu and to toolbar
/* First Way
A = new MenuItemAction(...);
B = new MenuItemAction(...);
// the class sets keystroke, icon, etc and performs actions
class MenuItemAction extends AbstractAction {
//Constructors
MenuItemAction(...) {
...
}
public void actionPerformed(ActionEvent e) {
String name = (String)getValue(NAME);
if (name.equals(A.getValue(NAME))) {
// do something ...
} else if (name.equals(B.getValue(NAME))) {
// do something else ...
}
}
}
// First Way end */
/* Second Way
A = new AActionClass(...);
B = new BActionClass(...);
abstract class MenuItemAction extends AbstractAction {
//Constructors
MenuItemAction(...) {
...
}
}
public class AActionClass extends MenuItemAction {
//Constructors
public AActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something ...
}
}
public class BActionClass extends MenuItemAction {
//Constructors
public BActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something else ...
}
}
// Second Way end */
/* Third Way
// like Second Way with AActionClass and BActionClass implemented in
// separate files (i.e. AActionClass.java and BActionClass.java). But
// I have to change some local vars from private to global, because
// these classes use them in their actionPerformed method.
// Third Way end */
/* Fourth Way
A = new AActionClass(...);
B = new BActionClass(...);
class AActionClass extends AbstractAction {
//Constructors
public AActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something ...
}
}
class BActionClass extends AbstractAction {
//Constructors
public BActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something else ...
}
}
// Fourth Way end */
// local vars
private ...
}
Thanks in advance,
Regargs,
Marcello.
P.S. Sorry for the indentation, but Forte Agent news reader ...
I know a fifth way with actionPerformed in an anonymous class, but I
don't like it in this app.
MyAppFrame.java is the GUI implementation of MyApp.java.
A and B are two items in a menu and two buttons in the toolbar:
public class MyAppFrame extends JFrame {
//Constructors
public MyAppFrame(String title) {
setTitle(title);
... // add A, B to menu and to toolbar
/* First Way
A = new MenuItemAction(...);
B = new MenuItemAction(...);
// the class sets keystroke, icon, etc and performs actions
class MenuItemAction extends AbstractAction {
//Constructors
MenuItemAction(...) {
...
}
public void actionPerformed(ActionEvent e) {
String name = (String)getValue(NAME);
if (name.equals(A.getValue(NAME))) {
// do something ...
} else if (name.equals(B.getValue(NAME))) {
// do something else ...
}
}
}
// First Way end */
/* Second Way
A = new AActionClass(...);
B = new BActionClass(...);
abstract class MenuItemAction extends AbstractAction {
//Constructors
MenuItemAction(...) {
...
}
}
public class AActionClass extends MenuItemAction {
//Constructors
public AActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something ...
}
}
public class BActionClass extends MenuItemAction {
//Constructors
public BActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something else ...
}
}
// Second Way end */
/* Third Way
// like Second Way with AActionClass and BActionClass implemented in
// separate files (i.e. AActionClass.java and BActionClass.java). But
// I have to change some local vars from private to global, because
// these classes use them in their actionPerformed method.
// Third Way end */
/* Fourth Way
A = new AActionClass(...);
B = new BActionClass(...);
class AActionClass extends AbstractAction {
//Constructors
public AActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something ...
}
}
class BActionClass extends AbstractAction {
//Constructors
public BActionClass(...) {
super(...);
}
public void actionPerformed(ActionEvent e) {
// do something else ...
}
}
// Fourth Way end */
// local vars
private ...
}
Thanks in advance,
Regargs,
Marcello.
P.S. Sorry for the indentation, but Forte Agent news reader ...