Miscellaneous GUI Code etc.

(Note that some of this might be a bit different for later versions of Netbeans.)


1. In the Inspector, open up disclosure triangles until you get inside the “menuBar” or a particular menu, and right-click on the “menuBar” or menu component.  Choose “Add JMenu” or Add, JMenuItem.

2. Then right-click on the new menu/menu item, and “Change Variable Name…

3. Select the menu/menu item, and in the Properties window, scroll down to the “text” property, and name the text what you want.

4. For a menu item action, right-click on the menu item, and go Event, Action, actionPerformed, and add the code.


Making Dialog Boxes

Drag in a JOptionPane, and reduce its size so that it is not visible.

For any event, add the following code:
jOptionPane1.showMessageDialog(null, "The Message", "The Window Name",
For the final parameter, let the whole list of methods and attributes come up when you press the period key.  Then choose an icon constant that is appropriate (they’ll be all CAPS and bold).


Having A Key Press Trigger an Action

Rather than having a menu item selection or a button click trigger code, another way is to have the press of a key trigger code.  Usually this will be within the context of a TextField. 

Right-click a textField, and then choose Events, Key, keyPressed.

In the code, use the getKeyChar() method of the evt object to trigger the action.
For example:  
            if(evt.getKeyChar() == 'A'){
If it is the Enter key that you want to trigger something, then you have to put the “escape character” for Enter, which is actually two characters, the back slash followed by n.  Note that it’s a back slash, not the more common forward slash.  And note that characters (the data type char) are put in single quotes. 
So another example:
if(evt.getKeyChar() == '\n'){


You’ll use JTextAreas to paste in your code when you pass in fully functional applications that you’ve done as labs.  Note that there needs to be a return at the end of each full line; that is done automatically in the Netbeans code.


Saving to and Opening from Specific Paths

By default, FileWriter objects will be placed in your project folder.  It would be more convenient to have the user choose where they save their files, and also be able to open them from anywhere in the computer.

One java class enables all of this: JFileChooser.

There are three methods you’ll need to use with objects that you make of the JFileChooser class: showSaveDialog(), showOpenDialog(), and getSelectedFile().

The first two display the respective dialogue boxes.  And getSelectedFile() returns a file object (either created via the Save dialogue box, or selected in the Open dialogue box), which you will then use in the construction of a FileWriter or FileReader object.


- JFileChooser comes from the package called javax, which you can “import”.

- Another object that you should make more simply accessible via the import line is the HeadlessException, found in the java.awt package.

- Don’t worry so much about the try and catch blocks, since Netbeans will place them correctly for you automatically.

- To automatically have Netbeans set up the proper method heading, double-click on the menu item in the Inspector window of the Design View.

- If you are not using this GUI way of saving and opening files, or whenever you want to communicate a certain path to a certain file, you use the conventional path syntax.  On a Mac, it would be something like:
but within Netbeans, and in this lab, always starting with /users/theNameOfYourComputer


import java.awt.HeadlessException;
import javax.swing.*;
import java.io.*;

    private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                               
        try {
            JFileChooser jfc = new JFileChooser();
            FileWriter fw = new FileWriter(jfc.getSelectedFile());
            BufferedWriter bw = new BufferedWriter(fw);
        } catch (HeadlessException ex) {
        } catch (IOException ex) {

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                            
        try {
            JFileChooser jfc = new JFileChooser();
            FileReader fr = new FileReader(jfc.getSelectedFile());
            BufferedReader br = new BufferedReader(fr);
        } catch (HeadlessException ex) {
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {