Data Entry Fields & Menus

Using the graphical components to create fully fledged Java applications is a complex task. I have several large books that specialize explicitly in the Swing libraries only. However to give a flavour of what can be done the following code creates a form that contains two labels with associated text fields and a Submit button. To keep things simple I have omitted any event handling code.

import javax.swing.*;
import java.awt.*;
/**
 *@author Marcus Green
 *Demonsration of use of Java Swing Compontnnts
 **/
public class Components extends javax.swing.JFrame{
    public static void main(String argv[]){
        new Components();
    }
    Components(){
        /* GridLayout with 3 rows and 1 column */
        GridLayout gl = new GridLayout(3,1);
        setLayout(gl);
        /**
         *JPanel defaults to a Flow Layout and
         *so components added will have their
         *preferred size respected
         **/
        JPanel jpRow1 = new JPanel();
        JPanel jpRow2 = new JPanel();
        JPanel jpRow3 = new JPanel();
        
        JLabel jlFirstName = new JLabel("First Name");
        
        /** JTextField takes the number of 
        *columns (width) as a constructor paramter 
        **/
        JTextField jtFirstName = new JTextField(20);
        
        JLabel jlLastName = new JLabel("Last Name");
        JTextField jtLastName = new JTextField(20);
        /* add the components to the panel */
        jpRow1.add(jlFirstName);
        jpRow1.add(jtFirstName);
        /* add the panel to the row within the GridLayout */
        add(jpRow1);
        
        jpRow2.add(jlLastName);
        jpRow2.add(jtLastName);
        add(jpRow2);
        JButton jbSubmit = new JButton("Submit");
        jpRow3.add(jbSubmit);
        add(jpRow3);
        /* Set the overall Frame size */
        setSize(300,300);
        /* Without this the application will be invisible */
        setVisible(true);
        
    }
    
}

The following screen shot shows how the application appears at runtime.

Note that if you resize the application and make the Form narrower, the labels will wrap so they appear above the associated text fields.

Menus

Creating menus is Java applications is relatively easy using a combination of the JmenuBar, Jmenu and JmenuItem classes. Note that in a real system you would not have the names of menus embedded within the code, but separated out as a resource to allow easy internationalisation. I have included sample code to allow this application to respond when the exit option is selected.

import javax.swing.*;
import java.awt.*;
/**
 *@author Marcus Green
 *Demonsration of use of Java Swing Menu Compontnnts
 **/
public class Menu extends javax.swing.JFrame{
    public static void main(String argv[]){
        new Menu();
    }
    Menu(){
        /* Create the bar that runs accross the top of the frame */
        JMenuBar jmBar = new JMenuBar();
        /*The File menu that all other items will be attached to */
        JMenu jm = new JMenu("File");
        JMenuItem jmiFile = new JMenuItem("File");
        JMenuItem jmiOpen = new JMenuItem("Open");
        /* The short cut key that will have an underscore */
        jmiOpen.setMnemonic('O');
        JMenuItem jmiExit = new JMenuItem("Exit");
        jmiExit.setMnemonic('x');
        /* Exit back to the system when this menu option is selected */
        jmiExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit(0);
            }
        });
        
        /* Add each item to the menu */
        jm.add(jmiOpen);
        jm.add(jmiExit);
        
        /* Add the menu to the menu bar */
        jmBar.add(jm);
        /* attch this menubar to the frame */
        setJMenuBar(jmBar);
        /* Set the overall Frame size */
        setSize(350,350);
        /* Without this the application will be invisible */
        setVisible(true);
    }
}

The following screen shot shows how the application appears at runtime.

Last modified: Thursday, 24 July 2014, 2:54 PM