I tried differend things on how to make Actions resuable in my Swing applications. I am not a friend on heavy-weight, 1000+ line classes (with lots of inner/anon classes) and try to split my code up into multiple classes. Thus make them resuseable and exchangeable easily.
For reusing same Actions in an application I made for every Action its own class to use it in JMenuBar and JToolBar. Please have a look at the minimal example below.
Is this a good choosen practice (esp. using static inner classes)?
public class SomeGUI extends JFrame {
public static void main(String[] args)
{
new SomeGUI();
}
public SomeGUI()
{
setJMenuBar(new MyMenuBar());
add(new MyToolBar());
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class MyMenuBar extends JMenuBar {
JMenu menu = new JMenu("File");
public MyMenuBar()
{
add(menu);
menu.add(new JMenuItem(new Actions.NewAction("New", null, "New File", KeyEvent.VK_N)));
}
}
class MyToolBar extends JToolBar {
public MyToolBar()
{
add(new Actions.NewAction("New", null, "New File", KeyEvent.VK_N));
}
}
class Actions {
static class NewAction extends AbstractAction {
public NewAction(String name, ImageIcon icon, String desc, Integer mnemonic)
{
super(name, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent paramActionEvent)
{
System.out.println("do the new action...");
}
}
}
Looking forward for your advises. Thanks in advance.
Is this a good choosen practice (esp. using static inner classes)?
This is the way much of the base Swing code is implemented.
For example take a look at the source code of DefaultEditorKit where all the Actions are defined.
One suggestion would be not to use an aggregate class which holds all actions. Just use a separate file for each class and make it public.
This way for using the class in another project you would just need to copy the file assuming it has no specific dependencies in current project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With