Swing - SPLessons

Swing Windows

Chapter 26

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Windows

Swing Windows

shape Description

Moving a Swing Windows on the screen is like a small entertaining game.User can change the place of the window application any where on the screen, while moving Swing Windows from one place to another place user can get the same result of an application then the resolution of the system will not be visible. In moving Swing Windows the resolution of the system will be visible that can be visible via x-axis and y-axis direction. The complete logic behind the moving window has been shown in below example.

Moving Swing Windows

shape Example

This example describes what is technique behind the moving window.

shape Step-1

Create a package and import all the required packages to hire the properties.

package swing;//create the package
import java.awt.Container;//import all the required packages.
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

shape Step-2

Create a class with the name SwingMovingWindow that should extend from the JFrame and should implement the ComponentListener. The functionality of ComponentListener is to receive the events.

public class SwingMovingWindow extends JFrame
        implements ComponentListener

shape Step-3

Create labels x and y and create constructor with name of class name.

private JLabel labelx;
    private JLabel labely;

    public SwingMovingWindow() {

        initUI();
    }

shape Step-4

Create the object to the group layout and create the layout to the container pane and add listeners that performs on components as follows.

Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);  
        addComponentListener(this);

shape Step-5

Create the labels x and y, add font size, set the bounds.

labelx = new JLabel("x: ");
        labelx.setFont(new Font("Serif", Font.BOLD, 14));
        labelx.setBounds(20, 20, 60, 25);

        labely = new JLabel("y: ");
        labely.setFont(new Font("Serif", Font.BOLD, 14));
        labely.setBounds(20, 45, 60, 25);

shape Step-6

Set the gaps between the components.

gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);

shape Step-7

Create horizontal group layout and vertical group layout with parallel and sequential modes and components should add labels x, y by providing gaps between the components as follows.

 gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(labelx)
                .addComponent(labely)
                .addGap(250)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(labelx)
                .addComponent(labely)
                .addGap(130)


shape Step-8

Set the title to the window and write the method to close the window.

setTitle("Moving window");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);


shape Step-9

Create the three methods and here used componetmoved() remaining methods are empty methods.

 @Override
    public void componentShown(ComponentEvent e) {
    }


shape Step-9

Here get the components of x and y.

labelx.setText("x: " + x);
        labely.setText("y: " + y);

SwingMovingWindow.java

package swing;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class SwingMovingWindow extends JFrame
        implements ComponentListener {

    private JLabel labelx;
    private JLabel labely;

    public SwingMovingWindow() {

        initUI();
    }

    private void initUI() {

        Container pane = getContentPane();
        GroupLayout gl = new GroupLayout(pane);
        pane.setLayout(gl);  
        
        addComponentListener(this);

        labelx = new JLabel("x: ");
        labelx.setFont(new Font("Serif", Font.BOLD, 14));
        labelx.setBounds(20, 20, 60, 25);

        labely = new JLabel("y: ");
        labely.setFont(new Font("Serif", Font.BOLD, 14));
        labely.setBounds(20, 45, 60, 25);

        gl.setAutoCreateContainerGaps(true);
        gl.setAutoCreateGaps(true);
        
        gl.setHorizontalGroup(gl.createParallelGroup()
                .addComponent(labelx)
                .addComponent(labely)
                .addGap(250)
        );

        gl.setVerticalGroup(gl.createSequentialGroup()
                .addComponent(labelx)
                .addComponent(labely)
                .addGap(130)
        );
        
        pack();   

        setTitle("Moving window");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void componentResized(ComponentEvent e) {
    }

    @Override
    public void componentMoved(ComponentEvent e) {
        
        int x = e.getComponent().getX();
        int y = e.getComponent().getY();
        
        labelx.setText("x: " + x);
        labely.setText("y: " + y);
    }

    @Override
    public void componentShown(ComponentEvent e) {
    }

    @Override
    public void componentHidden(ComponentEvent e) {
    }


    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SwingMovingWindow ex = new SwingMovingWindow();
                ex.setVisible(true);
            }
        });
    }
}

Output:

Output will be as follows.When user move the window then resolution of the application will be shown via x and y axis.

Summary

shape Key Points

  • ComponentShown, ComponentHidden, ComponentMoved, ComponentResized will be in ComponentListener interface.
  • To place the location of an application on the screen setLoc