Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Easy Java Lecture 16: Swing II. Part I

JavaFAQ Home » Java Lectures by Anatoliy Malyarenko Go to all tips in Java Lectures by Anatoliy Malyarenko

Bookmark and Share

Swing II

by: Anatoliy Malyarenko


  • Contents of the lecture.
  • Example four: LunarPhases.
  • Example five: VoteDialog.
  • Layout management.

Example four: LunarPhases

This next example, LunarPhases, is a more complicated example of how to use images in your application. As an added bonus, you'll also see how to implement combo boxes. Here are two pictures of the LunarPhases application:

In this program, the user chooses the lunar phase from the combo box, and the selected phase of the moon is shown in the lower panel. This is the first example we've seen that uses multiple panels to group components. Let's take a quick look at how we set up these panels.

LunarPhases has three panels, as shown in the following figure.

In the following code in the LunarPhases constructor, we construct all three panels and add the two subpanels (selectPanel and displayPanel) to mainPanel.


// Create the phase selection and display panels.
selectPanel = new JPanel();
displayPanel = new JPanel();

// Add various widgets to the subpanels.

// Create the main panel to contain the two subpanels.
mainPanel = new JPanel();
mainPanel.setLayout(new GridLayout(2,1,5,5));

// Add the select and display panels to the main panel.

When we add the subpanels to the main panel, how can we make sure that they're added in the right place? By default, each container has a layout manager -- an object that performs layout management for the components within the container. Layout management is the process of determining the size and the position of components. The default layout manager for the containers we've looked at is FlowLayout. With FlowLayout, the components are simply positioned in the container from left to right in the order they are added.

In the previous code snippet, we use a layout manager called GridLayout to position the components within.

Compound borders

In previous examples, we've added a simple border to add a buffer of space around components. In this example, both subpanels, selectPanel and displayPanel, have a compound border. This compound border consists of a titled border (an outlined border with a title) and an empty border (to add extra space), as shown in the following figure:

The code for the selectPanel border follows.


// Add border around the select panel
    BorderFactory.createTitledBorder("Select Phase"),

The displayPanel sets its own border in the same way.

Combo boxes

A combo box enables user choice. A combo box can be either editable or uneditable, but by default it is uneditable. An uneditable combo box looks like a button until the user interacts with it. When the user clicks it, the combo box displays a menu of items.

Use an uneditable combo box to display one-of-many choices when space is limited, when the number of choices is large, or when the menu items are computed at runtime. The following code in LunarPhases.java creates an uneditable combo box, phaseChoices, and sets it up:

JComboBox phaseChoices = null;
// Create combo box with lunar phase choices
String[] phases = {
"New", "Waxing Crescent", "First Quarter",
"Waxing Gibbous", "Full", "Waning Gibbous",
"Third Quarter", "Waning Crescent" };
phaseChoices = new JComboBox(phases);

The code initialises the combo box with an array of strings, phases. You can also put icons in a combo box or initialise the combo box with a vector. In the last line of code, the setSelectedIndex method specifies which phase of the moon should be shown when the program starts.

Part II continues ...

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy