13

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?

Imri Persiado
  • 1,857
  • 8
  • 29
  • 45
  • The way you move a Swing Frame is by clicking on its title bar and dragging it around. No title bar implies no movement. How did you plan to drag it around without a title bar? – JohnnyO Apr 16 '13 at 20:53
  • 3
    That's the default way, you never saw a program without a title bar which is movable?well I did and I would like to create one :) – Imri Persiado Apr 16 '13 at 20:55
  • 2
    Basically, you need to define an area around the border of the frame that can be "dragged", using a combination of a MouseListerner and MouseMotionListener you need to delta the difference between the click point and the drag point – MadProgrammer Apr 16 '13 at 20:55

3 Answers3

20

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.

Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
Erik Pragt
  • 13,513
  • 11
  • 58
  • 64
2

Maybe this will help you Moving Window

DRastislav
  • 1,892
  • 3
  • 26
  • 40
0

I encapsulate a extended JFrame class for you, I called it MoveaFrame, you just need to "extend MoveaFrame" in your practice:

Just copy below codes to your project, and extend it, you can make your Frame window draggable!

Extend MoveJFrame like extend a JFrame, you can directly drag your window:

public class ContactUi extends MoveJFrame implements  Runnable {

The MoveJFrame class code, just copy it and extend it like extend JFrame:

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

public class MoveJFrame extends JFrame {

    public MoveJFrame() {


        this.setUndecorated(true);


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

        this.pack();
        this.setLocationRelativeTo(null);
        this.setVisible(true);

    }

    public static void main(String[] args) throws IOException {
        new MoveJFrame();
    }

    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);
        }
    }
}
MartenCatcher
  • 2,713
  • 8
  • 26
  • 39
Super-ilad
  • 101
  • 11