Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a java swing frame movable and setUndecorated

Tags:

java

swing

I have created a frame without the title bar, for that I used the setUndecorated(true); method but after that the frame is became unmovable for some reason.

How can I make my frame movable and still hide my title bar?

like image 883
Imri Persiado Avatar asked Apr 16 '13 20:04

Imri Persiado


People also ask

How do you make a JFrame movable?

The way you move a Swing Frame is by clicking on its title bar and dragging it around.

What are the ways to create a frame using Java Swing?

There are two ways to create a frame: By creating the object of Frame class (association) By extending Frame class (inheritance)

Is Swing outdated Java?

JavaFX new fixes will continue to be supported on Java SE 8 through March 2022 and removed from Java SE 11. Swing and AWT will continue to be supported on Java SE 8 through at least March 2025, and on Java SE 11 (18.9 LTS) through at least September 2026.


1 Answers

The following code will create a JFrame without a title bar, which you can still move around:

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class FrameDragListenerExample {

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                final JFrame frame = new JFrame("Hello");

                frame.setUndecorated(true);
                frame.setBounds(0, 0, 400, 400);

                JPanel contentPane = new JPanel(new BorderLayout());
                JLabel label = new JLabel("Click anywhere in the Jframe and drag");
                label.setFont(label.getFont().deriveFont(16f));
                label.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
                contentPane.add(label);
                frame.setContentPane(contentPane);

                FrameDragListener frameDragListener = new FrameDragListener(frame);
                frame.addMouseListener(frameDragListener);
                frame.addMouseMotionListener(frameDragListener);

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(runnable);
    }

    public static class FrameDragListener extends MouseAdapter {

        private final JFrame frame;
        private Point mouseDownCompCoords = null;

        public FrameDragListener(JFrame frame) {
            this.frame = frame;
        }

        public void mouseReleased(MouseEvent e) {
            mouseDownCompCoords = null;
        }

        public void mousePressed(MouseEvent e) {
            mouseDownCompCoords = e.getPoint();
        }

        public void mouseDragged(MouseEvent e) {
            Point currCoords = e.getLocationOnScreen();
            frame.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
        }
    }
}

You can still drag it around by dragging the body of the frame.

like image 178
Erik Pragt Avatar answered Oct 04 '22 02:10

Erik Pragt