You are here:Home » Java Swing » First programs in JAVA Swing

First programs in JAVA Swing

Java Swing first programs

In this chapter, we will program our first programs in Swing toolkit. The examples are going to be very simple. We will cover some basic functionality.

Our first example

In our first example, we will show a basic window on the screen.
package zetcode;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Example extends JFrame {

public Example() {
setTitle("Simple example");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Example ex = new Example();
ex.setVisible(true);
}
});
}
}
While this code is very small, the application window can do quite a lot. It can be resized, maximized, minimized. All the complexity that comes with it has been hidden from the application programmer.
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
Here we import Swing classes, that will be used in the code example.
public class Example extends JFrame {
The Example class inherits from the JFrame widget. JFrameis a toplevel container, which is used for placing other widgets.
setTitle("Simple example");
Here we set the title of the window using the setTitle()method.
setSize(300, 200);
This code will resize the window to be 300px wide and 200px tall.
setLocationRelativeTo(null);
This line will center the window on the screen.
setDefaultCloseOperation(EXIT_ON_CLOSE);
This method will close the window, if we click on the close button of the titlebar. By default nothing happens.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Example ex = new Example();
ex.setVisible(true);
}
});
We create an instance of our code example and make it visible on the screen. The invokeLater() method places the application on the Swing Event Queue. It is used to ensure that all UI updates are concurrency-safe. In other words, it is to prevent GUI from hanging in certain situations. This topic is an advanced concept and we should not worry right now about it.
Simple example
Figure: Simple example

Quit button

In our next example, we will have a button. When we click on the button, the application terminates.
package zetcode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

public Example() {
initUI();
}

public final void initUI() {

JPanel panel = new JPanel();
getContentPane().add(panel);

panel.setLayout(null);

JButton quitButton = new JButton("Quit");
quitButton.setBounds(50, 60, 80, 30);
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});

panel.add(quitButton);

setTitle("Quit button");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Example ex = new Example();
ex.setVisible(true);
}
});
}
}
We position a JButton on the window. We will add an action listener to this button.
public Example() {
initUI();
}
It is a good programming practice to put the code that creates the GUI inside a specific method.
JPanel panel = new JPanel();
getContentPane().add(panel);
We create a JPanel component. It is a generic lightweight container. We add the JPanel to the JFrame.
panel.setLayout(null);
By default, the JPanel has a FlowLayout manager. The layout manager is used to place widgets onto the containers. If we call setLayout(null) we can position our components absolutely. For this, we use the setBounds() method.
JButton quitButton = new JButton("Quit");
quitButton.setBounds(50, 60, 80, 30);
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
Here we create a button. We position it by calling the setBounds() method. Then we add an action listener. The action listener will be called, when we perform an action on the button. In our case, if we click on the button. The click will terminate the application.
panel.add(quitButton);
In order to show the quit button, we must add it to the panel.
Quit button
Figure: Quit button

A tooltip

Tooltips are part of the internal application's help system. The Swing shows a small rectangular window, if we hover a mouse pointer over an object.
package zetcode;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Example extends JFrame {

public Example() {
initUI();
}

public final void initUI() {

JPanel panel = new JPanel();
getContentPane().add(panel);

panel.setLayout(null);
panel.setToolTipText("A Panel container");

JButton button = new JButton("Button");
button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");

panel.add(button);

setTitle("Tooltip");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Example ex = new Example();
ex.setVisible(true);
}
});
}
}
In the example, we set the tooltip for the frame and the button.
panel.setToolTipText("A Panel container");
To enable a tooltip, we call the setTooltipText() method.
Tooltip
Figure: Tooltip
In this chapter, we have created some simple Java Swing programs.

0 comments:

Post a Comment