MrLegendGaming
MrLegendGaming

Reputation: 85

How to move an undecorated JFrame window?

So I've been working on a program and I wanted to make it borderless with jframe.setUndecorated(true);. However, I can't really move the JFrame window around the screen. Is there a way I can fix that?

Here is my current code for the JFrame:

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 450);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.getContentPane().setBackground(Color.BLACK);

Upvotes: 1

Views: 1507

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51533

To move the JFrame, left-click inside the JFrame somewhere, drag the mouse to the new position, and release the mouse button when the JFrame is in the correct position.

Here's the complete runnable code.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

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

public class MoveUndecoratedJFrame implements Runnable {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new MoveUndecoratedJFrame());
    }
    
    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        MoveListener listener = new MoveListener();
        panel.addMouseListener(listener);
        panel.addMouseMotionListener(listener);
        panel.setBackground(Color.BLACK);
        panel.setPreferredSize(new Dimension(700, 400));
        
        return panel;
    }
    
    public class MoveListener implements MouseListener, MouseMotionListener {
        
        private Point pressedPoint;
        private Rectangle frameBounds;
        private Date lastTimeStamp;

        @Override
        public void mouseClicked(MouseEvent event) {
        }

        @Override
        public void mousePressed(MouseEvent event) {
            this.frameBounds = frame.getBounds();
            this.pressedPoint = event.getPoint();
            this.lastTimeStamp = new Date();
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            moveJFrame(event);
        }

        @Override
        public void mouseEntered(MouseEvent event) {
        }

        @Override
        public void mouseExited(MouseEvent event) {
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            moveJFrame(event);
        }

        @Override
        public void mouseMoved(MouseEvent event) {
        }
        
        private void moveJFrame(MouseEvent event) {
            Point endPoint = event.getPoint();

            int xDiff = endPoint.x - pressedPoint.x;
            int yDiff = endPoint.y - pressedPoint.y;

            Date timestamp = new Date();

            //One move action per 60ms to avoid frame glitching
            if(Math.abs(timestamp.getTime() - lastTimeStamp.getTime()) > 60){ 
                if((xDiff>0 || yDiff>0)||(xDiff<0 || yDiff<0)) {
                    frameBounds.x += xDiff;
                    frameBounds.y += yDiff;
                    System.out.println(frameBounds);
                    owner.setBounds(frameBounds);
                }
                this.lastTimeStamp = timestamp;
            }
        }
        
    }

}

Upvotes: 4

Related Questions