
GUIs in Java Swing
The basic window
Containers and basic components
Add Remove
Visibility
Opacity
Size
Enable / Disable
Border
Tooltip
Cursor
Get an icon
Use html and unicode characters
The buttons
The text fields
Placement of components and LayoutManager
FlowLayout
GridLayout
BorderLayout
The basic window
Containers and basic components
Add Remove
Visibility
Opacity
Size
Enable / Disable
Border
Tooltip
Cursor
Get an icon
Use html and unicode characters
The buttons
The text fields
Placement of components and LayoutManager
FlowLayout
GridLayout
BorderLayout
GUIs in Java Swing
The purpose of this article is to remember which are the main swing components and how to use it.The basic window
What I call basic windows are windows, dialog boxes...JFrame | conventional window |
JWindow | window without bar |
JDialog | dialog box |
JApplet | web applet |
DO_NOTHING_ON_CLOSE | do nothing |
HIDE_ON_CLOSE | makes it invisible (default) |
DISPOSE_ON_CLOSE | makes it invisible and frees resources |
EXIT_ON_CLOSE | terminates the program |
public class hello { public static void main(String[] args) { JFrame f=new JFrame("hello window"); f.setSize(new Dimension(200,100)); //center the window on the screen f.setLocationRelativeTo(null); f.setVisible(true); } }
Containers and basic components
Here are some useful methods common to all components.Add Remove
Most objects extend swing container. So they can almost all contain each other. To add/remove a component contained in a container component, we use the add/remove methods of the container with the content as parameter like this:container.add(content);
container.add(content1).add(content2);
Visibility
To determine whether a component is visible or make it visible, you can use its methods setVisible/isVisible.Opacity
A component may be opaque or not, for this its setOpaque method is used, but be careful! The component is not refreshed.Size
The different size properties of the components are as follows.set/getSize | the current size |
set/getPreferredSize | the preferred size |
set/getMinimumSize | the minimum size |
set/getMaximumSize | the maximum size |
Enable / Disable
It is possible to enable and disable components. Eg a disabled button becomes grayed out. The method used here is setEnabled. To determine whether a component is enabled, the method called is isEnabled.Border
To add a border to the components, they have the setBorder method. The border itself can be created using the class BorderFactory.Tooltip
To add a tooltip when mousing over a component, the method to use on it is setToolTipText.Cursor
At the overflight of the component, it is also possible to change the mouse cursor. The method here is setCursor. To use one of the predefined cursors it is possible to do so using this method: Cursor.getPredefinedCursor(Cursor.XXX_CURSOR). So it would give:Get an icon
It is possible to obtain an icon using an image that is placed in the project class directory.Use html and unicode characters
The string in components often accept the basic html code. Thus it is possible to set a color, text in italics. For example: To use a Unicode character you enter, for example: \ u0645The buttons
There are two families of buttons, the traditional buttons JButton. And buttons that remain pressed JToggleButton, JCheckBox and JRadioButton. These can be tested using the isSelected method.It is possible to react to this buttons events via an actionListener. We add an actionListener which is an interface that contains only one method, void actionPerformed (ActionEvent e). So we catch the actions on the buttons as follows:
JButton b=new JButton(); b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("bouton pressed"); } });
The text fields
JTextField | a text line |
JFormattedTextField | a formatted text line (email, ip ..) |
JPasswordField | a text line, replaced by stars |
JTextArea | multiple lines of text |
JEditorPane | multiline formatted text |
JTextPane | multiline formatted text |