JButton b = new JButton("Button");
Note that the label could have been "George" instead of "Button"
and it would all be the same. There is a big difference between
a JFrame and a JButton, however. A JFrame is a window that sits
on the screen all by itself. But a JButton is a component that
must be placed inside some other GUI container - like a JFrame
or some other component that can act as a container for other
GUI elements. So, for starters we'd like to add the JButton to
a JFrame. However, now things get a bit complicated. Where in
the JFrame window do we want the JButton to go? This question
is outrageously subtle because there might be lots of components
sitting in a container like a JFrame, and because windows can
get resized or, perhaps, were never given a set size at all, and
the programmer wants it to be "big enough".
To deal with this complication of where to place components
that get added to containers like JFrames, the designers of
Java's Swing library turned to OOP principles: let's make the
positioner of components within the container be ... an
object. Specifically, there is an interface
named LayoutManager, and objects implementing
this interface are used to position componenets.
By default, JFrame's have LayoutManagers of type
BorderLayout, which thinks of the screen being divied up into
regions: North, East, South, West and Center. When you add a
component to a JFrame, you specify which BorderLayout region
you want it in. So if f is a JFrame you might
say:
f.add(new JButton("Button"), BorderLayout.CENTER);
if you want a new button with label "Button" added to the
Center region, and
f.add(new JButton("Button"), BorderLayout.CENTER);
if you want it added to the East region. These regions
automatically expand or shrink to fit the various componenets
into their various regions.
![]() |
![]() |
![]() |
| BorderLayout's regions | add button BorderLayout.CENTER | add button BorderLayout.EAST |
In our simple GUI, we have a button that the user can click and a text field the user can type something in. A click is considered "the" action for a button, and a new text value (user presses enter while the box has the focus) is considered "the" action for a button. So both components use a the simple ActionListener interface, which looks like this basically:
public interface ActionListener
{
public void actionPerformed(ActionEvent e);
}
So a class that implements ActionListener is what you want,
whether you want to react to a button being clicked or text
being entered.
In this example, we'll simply react to the button being
clicked. When that happens, we'll take any text in the text
field, and make it the new text in the label, erasing the text
field in the process.
Now, if a user hits "enter" while in the text field, that should change the label just like clicking the button would. So let's make that happen. The way
JTextField works, hitting
enter is an event that results in the JTextField's
ActionListeners being called. Since we want the
same thing happening regardless of whether the button is clicked
or enter hit, we can simply add the same action listener to both
components, i.e.
b.addActionListener(new ButtonClickListener(t,l)); |
becomes | ActionListener al = new ButtonClickListener(t,l); b.addActionListener(al); t.addActionListener(al); |
Cute, eh?

JComboBox<T>, for drop-down list. Notice
that this class uses generics, so that different kinds of
objects can appear in the drop-down.
JFrame f = new JFrame(); JPanel p = new JPanel(new FlowLayout()); f.add(p,BorderLayout.CENTER);
![]() |
![]() |
![]() |
| BorderLayout's regions | add JPanel BorderLayout.CENTER | after the add (assuming the JPanel is the only component added) |
public double getFromValue(); // get the "from" value public double getFromCF(); // get the conversion factor for the "from" units public double getToCF(); // get the conversion factor for the "to" units public void setToValue(double x); // set the "to" value to xWhy? Because this is precisely the functionality required by the Listener object that will be called upon to react to our various events - change in "from" value, change in "from" units, change in "to" units. Following this approach, our Listener object will need to 1) remember the ConversionWindow it belongs to, and 2) call onthe above methods to make the coversion happen.
| Ex3.java | ConverterWindow.javax |
| ConverterActionListener.java | |
| Ex5.java | UCFrame.javax |
| Responder.java | |