jaap wrote:
I'm a real noob with java. I worked for 4 years with php but now I have
to use java.
I'm looking for a clean way to get an action behind a button/menuitem. I
already found the class actionListener. but the method actionpreformed
is not very clean if you want to give 20 buttons an action. The way I
know is 20 times else if. There must be an better method.
I will try to use my GUI like an interface. It does'nt have to include
much code in my opinion. I think it have to be only some field and
button delcarations.
I personally prefer a design-pattern
roughly alongside the article "How to use Action" at
<http://java.sun.com/docs/books/tutorial/uiswing/misc/action.html>.
My code typically looks like this:
//--begin example-------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainFrame extends JFrame {
public static void main(String[] args) {
JFrame frame = new MainFrame();
frame.pack();
frame.setVisible(true);
}
// Actions used by menu items and buttons:
private Action openAction = new OpenAction();
private Action saveAction = new SaveAction();
// ...more actions
public MainFrame() {
jbInit();
}
private void jbInit() {
getContentPane().setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
fileMenu.add(openAction);
fileMenu.add(saveAction);
menuBar.add(fileMenu);
// ... more menus and menu-items
setJMenuBar(menuBar);
JToolBar toolBar = new JToolBar();
toolBar.add(openAction);
toolBar.add(saveAction);
// ... more tool-bar buttons
getContentPane().add(toolBar, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JButton(openAction));
panel.add(new JButton(saveAction));
// ... more buttons
getContentPane().add(panel, BorderLayout.CENTER);
}
class OpenAction extends AbstractAction {
public OpenAction() {
putValue(Action.NAME, "Open");
putValue(Action.SMALL_ICON, new ImageIcon("open16.gif"));
putValue(Action.MNEMONIC_KEY, new Integer('P'));
putValue(Action.SHORT_DESCRIPTION, "open file");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
KeyEvent.VK_O, InputEvent.CTRL_MASK));
}
public void actionPerformed(ActionEvent e) {
System.out.println(this);
// ... do something
}
}
class SaveAction extends AbstractAction {
public SaveAction() {
putValue(Action.NAME, "Save");
putValue(Action.SMALL_ICON, new ImageIcon("save16.gif"));
putValue(Action.MNEMONIC_KEY, new Integer('A'));
putValue(Action.SHORT_DESCRIPTION, "save file");
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
KeyEvent.VK_S, InputEvent.CTRL_MASK));
}
public void actionPerformed(ActionEvent e) {
System.out.println(this);
// ... do something
}
}
// ...more Action classes
}
//--end example------------------------------------------------
I hadn't seen that example before, and I like it quite a lot.
(as of 1.3). Instead, it suggests creating the component and using
setAction. Of course, the API also shows that common Swing components
now accept an Action in their constructors (also since 1.3). So it's
something like "new JMenuItem(openAction)". In either case, I'm going
to keep this idea in mind for future use.
Steve W. Jackson