|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 4 of 6
The main() method instantiates JPad and passes to its constructor the array of command-line arguments on the event-dispatch thread. After creating JPad's UI,
the constructor opens the file identified by the first command-line argument when at least one argument is specified.
In the JavaWorld article "Swing threading and the event-dispatch thread" author John Zukowski explains why it's important to create Swing UIs on the event-dispatch thread.
JPad's constructor creates the JPad UI by leveraging the javax.swing package's JCheckBoxMenuItem, JLabel, JMenu, JMenuBar, JMenuItem, JScrollPane, and JTextArea classes, as shown in Listing 2.
// ...
private JTextArea ta;
// ...
private JLabel lbl;
// ...
public JPad(String[] args)
{
// ...
JMenuBar mb = new JMenuBar();
JMenu mFile = new JMenu("File");
JMenuItem miNew = new JMenuItem("New");
miNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
KeyEvent.CTRL_MASK));
// ...
mFile.add(miNew);
// ...
mb.add(mFile);
// ...
JMenu mFormat = new JMenu("Format");
final JCheckBoxMenuItem cbmiWordWrap = new JCheckBoxMenuItem("Word Wrap");
// ...
mFormat.add(cbmiWordWrap);
// ...
mb.add(mFormat);
// ...
setJMenuBar(mb);
getContentPane().add(new JScrollPane (ta = new JTextArea()));
// ...
getContentPane().add(lbl = new JLabel("JPad 1.0"), BorderLayout.SOUTH);
setSize(400, 400);
setTitle(DEFAULT_TITLE);
// ...
setVisible(true);
// ...
}
The constructor first initializes the JFrame superclass and creates/installs the menu system (although I've omitted most of this code for brevity). Next, it creates the
scrollable text area and status bar, and adds them to the frame window's content pane. Lastly, it initializes and displays
the window.
Experienced Swing developers might note that I've used setTitle(DEFAULT_TITLE); instead of super(DEFAULT_TITLE); to initialize the frame window's default titlebar text in Listing 2. I wrote the code this way because the JavaFX 2 application
will also require setTitle(). For the sake of example, I wanted JPad to be as consistent with JPadFX as possible.
Event handling is one of the more involved aspects of Swing development, which makes refactoring a Swing app's event-handling infrastructure to JavaFX both challenging and rewarding. Here, we'll look at how JPad handles typical notepad events triggered by the user, namely: action, document, menu, caret, and window events.
An action event is fired when the user selects a menu item. For example, when the user selects New from the File menu, an action event is sent to New's registered action listener, as shown in Listing 3.
ActionListener al;
al = new ActionListener()
{
@Override
public void actionPerformed(ActionEvent ae)
{
if (fDirty)
switch (JOptionPane.showConfirmDialog(JPad.this, SAVE_CHNGS,
TITLE_AYS,
JOptionPane.YES_NO_OPTION))
{
case JOptionPane.YES_OPTION: if (doSave()) doNew(); break;
case JOptionPane.NO_OPTION : doNew();
}
else
doNew();
}
};
miNew.addActionListener(al);
The New menu item's action listener first examines fDirty to learn whether or not the current document has changed. If this Boolean field variable is true (meaning that the document has changed), the user is prompted to save changes before a new document replaces the current
one.
More from JavaWorld