User Interface Components



In java swing applications, windows are instances of the JFrame class. In this lab you will write a program that creates a JFrame and adds user interface components including menus, buttons, text areas, check boxes, and radio buttons. When you have completed all the exercises, your program should open a window that looks like the sample solution shown below:


  1. Use the visual guide to swing components to identify the component classes displayed in the window shown above. Label each user interface component with its java class.
  2. Create a new java project in Eclipse.
  3. Add a class to your project (use any name you like) that includes a main method.
  4. Add code to the main method to create and display an empty Order Placement window. It should have "Order Placement" in the title bar, and the rest of the window should be empty. You will need to use the JFrame constructor method that takes a String (specified in quotation marks) as a parameter.

    You will need to set the frame's size to something reasonable and set its visibility to true so it will show up on the screen. This is accomplished by sending setSize and setVisible messages.

    To make the close box work, send a setDefaultCloseOperation message, passing a parameter to indicate what should happen when the window closes. You should supply the value JFrame.EXIT_ON_CLOSE.

    You should now be able to test your code and see a window open on the screen.

  5. Now add code to your main method to put a menu bar and menus in your window. Your menus should show up, but they don't have to actually do anything. Test your code after each step.
    1. Create a JMenuBar object using the default (no parameter) constructor for JMenuBars. Create a JMenu object for a "File" menu using the JMenu constructor that takes a String parameter for the menu name. Use the JMenuBar add method to add the file menu to the menu bar. Finally, use the JFrame setJMenuBar method to set your JFrame object's menu bar to the one you created. Now test your code.
    2. Create a JMenuItem for a "Quit" item using the JMenuItem constructor that takes a String parameter for the menu item. Use the add method of the JMenu class to add the Quit item to your File menu. Test your code.
    3. Add a "Save" item to your "File" menu. Also, add an "Edit" menu with "Cut", "Copy", and "Paste" items. Test your code again.
  6. Now we will experiment with arranging some user interface components in the window. For this part, tell the JFrame not to use a layout manager (you will arrange the components by hand) by sending it the message setLayout(null).
  7. One at a time, create each of the components in the window that you identified in Exercise 1.
    1. Use an appropriate constructor method to create the component. You may need to check the documentation to find a suitable constructor. (Click "javax.swing" in the upper left pane to see just the user interface classes in the swing package.)
    2. Set the size and location of the component using the setSize and setLocation methods that all user interface components inherit from the Component class.
    3. Send the JFrame object an add message to add the component to the frame.
  8. When the window first opens, one of the two radio buttons (eat-in or take-out) should be selected, but when a radio button is created it is not selected by default. To fix this, send one of the radio buttons a setSelected message, passing the parameter value true.
  9. To make the radio buttons work properly (so that when one is selected the other is automatically deselected) they need to be added to a ButtonGroup. Create a ButtonGroup object and send it add messages to add the two radio buttons to the group.
  10. To have the initial text "Write any special requests here" show up in the special requests text area, send a setText message.
  11. Check the documentation to see how to get the text area to automatically wrap the text at word boundaries when it doesn't fit on a single line.
  12. To give a scrollbar to the "special requests" text area, it needs to be placed in a JScrollPane. Surprisingly, there is no add method in the JScrollPane class. To put the text area in the scroll pane, you pass it to the JScrollPane constructor. Then add only the scroll pane (not the text area) to the frame.
  13. By default, scroll panes only display scroll bars when they are needed. Check the documentation to see how to always display the vertical scroll bar.