Hello.

I have a piece of code currently where I have a JFrame that I have removed the title bar and borders etc from using .setUndecorated(true). I have tried a number of ways with no success to make this window draggable by it's client area.

JFrame f = new JFrame("New Frame");
   f.setUndecorated(true);
   f.setVisible(true);
   f.setBounds(0, 0, 400, 400);

What I want in the end is to have a window that has a no-title bar etc so that I can create the frame from scratch and I would like it to be dragged by just clicking anywhere on the window and be resized by dragging the edges of the frame.

Can anyone give me any examples, advice or point me in the right direction please?

Cheers.

Have you looked at Mouse Listeners?
Are you able to change a JFrame's position on the screen?

That is ultimately what I will have to use if all else fails and I consider that a last resort. I was hoping for something a bit simpler. When I programmed Windows applications in C I read about a little trick that you could do which tricked the Window somehow into thinking the client area was the title bar or something like that (I can't remember fully). I was hoping for something along those lines. Just a few lines of code or a way less messy than implementing all those mouse listeners with global variables etc. I believe I read something about TransferHandler (with exportAsDrag()), could that be along the lines of what I need? I don't really know how to go about implementing it though.

Thanks for the fast response.

Well I had a bash at implementing this drag feature using mouse listeners and it got rather messy.

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;

public class Main
{
    static Point mouseDownScreenCoords;
    static Point mouseDownCompCoords;

    public static void main(String[] args)
    {
        final JFrame f = new JFrame("Hello");

        mouseDownScreenCoords = null;
        mouseDownCompCoords = null;
        f.setUndecorated(true);
        f.setVisible(true);
        f.setBounds(0, 0, 400, 400);
        
        f.addMouseListener(new MouseListener(){
            public void mouseReleased(MouseEvent e) {
                mouseDownScreenCoords = null;
                mouseDownCompCoords = null;
            }
            public void mousePressed(MouseEvent e) {
                mouseDownScreenCoords = e.getLocationOnScreen();
                mouseDownCompCoords = e.getPoint();
            }
            public void mouseExited(MouseEvent e) {
            }
            public void mouseEntered(MouseEvent e) {
            }
            public void mouseClicked(MouseEvent e) {
            }
        });

        f.addMouseMotionListener(new MouseMotionListener(){
            public void mouseMoved(MouseEvent e) {
            }

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

I can imagine this getting a hell of a lot messier if I was to implement resizing etc. I would sincerely appreciate it if anyone could point me in the direction of something a bit cleaner or simpler. Feel free to help make my code a bit less complex too if you don't think it's up to scratch. Criticisms will be taken constructively :)

That looks like what it takes to do the job.

Hey bops I have tried to make your code less complex. Hope you will like it.

import java.awt.Point;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import javax.swing.JFrame;
     
    public class Main
    {
    static Point mouseDownCompCoords;
     
    public static void main(String[] args)
    {
    final JFrame f = new JFrame("Hello");
     
    mouseDownCompCoords = null;
    f.setUndecorated(true);
    f.setVisible(true);
    f.setBounds(0, 0, 400, 400);
     
    f.addMouseListener(new MouseListener(){
    public void mouseReleased(MouseEvent e) {
    mouseDownCompCoords = null;
    }
    public void mousePressed(MouseEvent e) {
    mouseDownCompCoords = e.getPoint();
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
    });
     
    f.addMouseMotionListener(new MouseMotionListener(){
    public void mouseMoved(MouseEvent e) {
    }
     
    public void mouseDragged(MouseEvent e) {
    Point currCoords = e.getLocationOnScreen();
    f.setLocation(currCoords.x - mouseDownCompCoords.x, currCoords.y - mouseDownCompCoords.y);
    }
    });
    }
    }

I've had to do this kind of thing more than once or twice, and I've never found a better way than that (apart from minor code tweaks). I put all that code into a small generic utility class WindowDragger, so the code for the main window just has to call
new WindowDragger(this);
to enable dragging by client area.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.