Java Swings/Large Examples

Calculator
The example below builds a user interface using multiple nested layout managers, applies listeners and client properties to components, and demonstrates how to create components from the Event Dispatch Thread (EDT).

Main Method
The main method is pretty straight forward.

It creates a new calculator, sets it to exit the program when its closed and then makes it visible. The interesting bit about this code is that it doesn't happen in the main thread. It happens in the event dispatch thread. While in a simple application like this it's enough to simply wrap the whole component creation in an  to push it into the EDT, larger applications need to be careful that they only access UI components from the EDT. Not doing so can cause mysterious deadlocks in applications.

Constructor
The constructor is where the calculator is laid out.

This approach uses inheritance for defining the calculator. Another way of doing it would have been to have a  method which would return a panel laid out as desired. The calculator would then be a controller dictating the behavior of the panels and own the panel all the components were on. While more complex, that method also scales more easily to larger, more complicated UIs. Inheritance has the problem of child classes having the ability to override methods that are currently being called in the constructor of the parent class. If the overridden methods required a field in the child class to be already initialized, you're in trouble. At the time that the method would be called in the parent, the child hasn't been fully initialized yet.

The constructor uses two BorderLayouts to do the layout. The first is for the main panel, the second is to compose the main panel with the error message panel. Also, each of the subpanels in the main panel are built with their own layout managers. This is a good example of building a more complicated UI as a composite of multiple simpler sub-panels.

Listeners
Here we start getting into the fun bits. For each of the buttons we want something to listen for the pushed button. The  class does just that.

We setup a separate listener for each type of button. Numbers, Decimals, Operators and Clear buttons. Each one processes the message associated with that class of button and only that class of button. While that's fine, how do you determine which button in that class was pressed? Well, Swing provides a way of setting arbitrary properties on objects. This is accomplished by the  pair. Below, we tell the difference between the number buttons by which number is associated with the  of the button. Similarly the  does the same, but pulling out its own property.

Building the buttons
Here we can see how the listeners get assigned to the buttons.

First we set the label for the buttons by passing a  to the constructor of the JButton, then we set the property value that we want using   then we add the appropriate action listener to the button. Action listeners are activated when the component takes its intended action. For a  that means being pressed.

Building the panels
Here we show building the number panel.

We want an even grid for the number keys and want them all to be sized the same so we use a  as our LayoutManager. This grid is 4 rows deep and 3 columns wide. Building the operator panel and the clear panel are both done similarly.

Full code for the Calculator
In this "calculator": 4 * 6 = 36, 4 * 9 = 81, 4 * 3 = 9. Error1 in strings: After correcting Error1 in this "calculator": 1 / 3 = 3, 5 / 2 = 0.4, 8 / 4 = 0.5. Error2 in strings: After correcting Error2 in this "calculator": 9 - 6 = -3, 6 - 3 = -3, 8 - 4 = -4. Error3 in strings: Error4 in strings: After removing this strings calculator work correctly. Here you can find the similar calculator running on a web like Java applet, with source code and code review available.