Java JFrame help

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator:Moderators

Post Reply
Positron
Posts:53
Joined:Mon Mar 31, 2008 11:10 pm
Java JFrame help

Post by Positron » Sat Nov 01, 2008 6:05 pm

Does anyone know how to create a JFrame that has no decorations but that is still movable? When I remove the decorations (the border with the minimize/maximize, and close buttons) it seems to be locked into a specific location and is not movable.

User avatar
Fenrir
Posts:14
Joined:Wed Aug 27, 2008 9:20 am

Post by Fenrir » Mon Dec 15, 2008 1:23 am

That's interesting, I've never tried creating an undecorated JFrame before.

I guess the thing is, in Microsoft Windows atleast, the border is the only comonent ever used to move windows around. If you want such behaviour, I s'pose you're going to have to write a mouse listener for the JFrame and tap in the movement code yourself - something like...

Code: Select all

package test;

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

public class Main
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        WindowMovementListener listener = new WindowMovementListener(frame);
        Container c = frame.getContentPane();
        JPanel toppanel = new JPanel();
        JButton closebutton = new JButton("X");
        
        closebutton.addActionListener(new CloseListener(frame));
        toppanel.setBackground(Color.BLUE);
        toppanel.setLayout(new BorderLayout());
        toppanel.add(closebutton,BorderLayout.EAST);
        c.setBackground(Color.DARK_GRAY);
        c.setLayout(new BorderLayout());
        c.add(toppanel,BorderLayout.NORTH);
        
        frame.addMouseListener(listener);
        frame.addMouseMotionListener(listener);
        
        frame.setUndecorated(true);
        frame.setBounds(100,100,500,300);
        frame.setVisible(true);
    }
}

class WindowMovementListener implements MouseMotionListener,MouseListener
{
    private JFrame frm;
    private int x;
    private int y;
    
    public WindowMovementListener(JFrame pFrm)
    {
        frm = pFrm;
        x = 0;
        y = 0;
    }
    
    public void mouseClicked(MouseEvent e)
    {
    }
    public void mouseEntered(MouseEvent e)
    {
    }
    public void mouseExited(MouseEvent e)
    {
    }
    
    public void mousePressed(MouseEvent e)
    {
        x = e.getXOnScreen();
        y = e.getYOnScreen();
    }

    public void mouseReleased(MouseEvent e)
    {
    }
    
    public void mouseMoved(MouseEvent e)
    {
    }

    public void mouseDragged(MouseEvent e)
    {
        int movx = e.getXOnScreen() - x;
        int movy = e.getYOnScreen() - y;
        frm.setLocation(frm.getLocation().x + movx, frm.getLocation().y + movy);
        
        x = e.getXOnScreen();
        y = e.getYOnScreen();
    }
}

class CloseListener implements ActionListener
{
    private JFrame frm;
    
    public CloseListener(JFrame pFrm)
    {
        frm = pFrm;
    }
    
    public void actionPerformed(ActionEvent e)
    {
        frm.dispose();
    }
}
I know it's a month and a half late, but I hope it helps.

Post Reply