Using Swing for Graphical User Interfaces
Quitting the application on window close
It's easy to forget to quit the application when the window is closed. Remember to add the following line.
Creating an Empty Window (JFrame)
Creating the JFrame
Creating a window is easy. You just have to create a
Titling the Window
You may wish to give your window a title. You can so do by passing a string when creating the
JFrame, or by calling
Setting the Window Size
The window will be as small as possible when it has been created. To make it bigger, you can set its size explicitly:
Or you can have the frame size itself based on the size of its contents with the
pack() methods are mutually exclusive, so use one or the other.
What to do on Window Close
Note that the application will not quit when the window has been closed. You can quit the application after the window has been closed by telling the
JFrame to do that.
Alternatively, you can tell the window to do something else when it is closed.
Creating a Content Pane
An optional step is to create a content pane for your window. This is not needed, but if you want to do so, create a
JPanel and call
Showing the Window
After creating it, you will want to create your components, then show the window. Showing the window is done as such.
For those of you who like to copy and paste, here's some example code.
A component is some sort of user interface element, such as a button or a text field.
Creating a Component
Creating components is near identical to creating a window. Instead of creating a
JFrame however, you create that component. For example, to create a
JButton, you do the following.
Many components can have parameters passed to them when created. For example, a button can be given some text to display.
If you don't want to create a button, a list of common components can be found in another example on this page.
The parameters that can be passed to them vary from component to component. A good way of checking what they can accept is by looking at the paramters within your IDE (If you use one). The default shortcuts are listed below.
- IntelliJ IDEA - Windows / Linux:
CTRL + P
- IntelliJ IDEA - OS X / macOS:
CMD + P
CTRL + SHIFT + Space
CTRL + P
Showing the Component
After a component has been created, you would typically set its parameters. After than, you need to put it somewhere, such as on your
JFrame, or on your content pane if you created one.
Here's an example of creating a window, setting a content pane, and adding a button to it.
|Drop down menu / Combo box|
|Menu in a menu bar|
|Item in a menu|
|Spinner / Number picker|
|Text area / Multiline text field|
Making Interactive User Interfaces
Having a button there is all well and good, but what's the point if clicking it does nothing?
ActionListeners are used to tell your button, or other component to do something when it is activated.
ActionListeners is done as such.
Or, if you're using Java 8 or above...
Example (Java 8 and above)
Organizing Component Layout
Adding components one after another results in a UI that's hard to use, because the components all are somewhere. The components are ordered from top to bottom, each component in a separate "row".
To remedy this and provide you as developer with a possibility to layout components easily Swing has
These LayoutManagers are covered more extensively in Introduction to Layout Managers as well as the separate Layout Manager topics:
Setting Parameters for Components
Components have various parameters that can be set for them. They vary from component to component, so a good way to see what parameters can be set for components is to start typing
componentName.set, and let your IDE's autocomplete (If you use an IDE) suggest methods. The default shortcut in many IDEs, if it doesn't show up automatically, is
CTRL + Space.
Common parameters that are shared between all components
|Sets the smallest size that the component can be (only if the layout manager honors the minimumSize property)|
|Sets the biggest size that the component can be (only if the layout manager honors the maximumSize property)|
|Sets the perferred size of the component (only if the layout manager honors the preferredSize property)|
|Shows or hides the component|
|Sets whether the component should respond to user input|
|Sets the font of text|
|Sets the text of the tooltip|
|Sets the Backgroundcolor of the component|
|Sets the Foregroundcolor (font color) of the component|
Common parameters in other components
|Sets the text displayed|
|Set's a numerical value between the component's min and max values|
|Set's the smallest possible value that the |
|Set's the biggest possible value that the |
|Set's whether the value is true or false (Eg: Should a checkbox be checked?)|