javaswingjframemouseeventmouse-listeners

Drag and Resize undecorated JFrame


Currently, I am using the following code to drag and move my undecordated JFrames.

private void initialiseGUI(Component component){
    //<editor-fold defaultstate="collapsed" desc="code">
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            posX = e.getX();
            posY = e.getY();
        }
    });

    component.addMouseMotionListener(new MouseAdapter() {
        public void mouseDragged(MouseEvent evt) {
            //sets frame position when mouse dragged            
            Rectangle rectangle = getBounds();
            getGUI().setBounds(evt.getXOnScreen() - posX, evt.getYOnScreen() - posY, rectangle.width, rectangle.height);
        }
    });
    //</editor-fold>
}

What must I write so that the user can resize it like a decorated window, by dragging the side?


Solution

  • You can check out mr Rob Camick's ComponentResizer class. Pretty simple and straight forward to use.

    Just instantiate the ComponentResizer and register the frame with something like:

    JFrame frame = new JFrame();
    ComponentResizer cr = new ComponentResizer();
    cr.registerComponent(frame);
    cr.setSnapSize(new Dimension(10, 10));
    cr.setMaximumSize(new Dimension(...));
    cr.setMinimumSize(new Dimension(...));
    

    Here's a complete example of using the class

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    
    public class UndecoratedExample {
    
        private JFrame frame = new JFrame();
    
        class MainPanel extends JPanel {
    
            public MainPanel() {
                setBackground(Color.gray);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        }
    
        class BorderPanel extends JPanel {
    
            private JLabel label;
            int pX, pY;
    
            public BorderPanel() {
                label = new JLabel(" X ");
                label.setOpaque(true);
                label.setBackground(Color.RED);
                label.setForeground(Color.WHITE);
    
                setBackground(Color.black);
                setLayout(new FlowLayout(FlowLayout.RIGHT));
    
                add(label);
    
                label.addMouseListener(new MouseAdapter() {
                    public void mouseReleased(MouseEvent e) {
                        System.exit(0);
                    }
                });
                addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent me) {
                        // Get x,y and store them
                        pX = me.getX();
                        pY = me.getY();
    
                    }
    
                     public void mouseDragged(MouseEvent me) {
    
                        frame.setLocation(frame.getLocation().x + me.getX() - pX,
                                frame.getLocation().y + me.getY() - pY);
                    }
                });
    
                addMouseMotionListener(new MouseMotionAdapter() {
                    public void mouseDragged(MouseEvent me) {
    
                        frame.setLocation(frame.getLocation().x + me.getX() - pX,
                                frame.getLocation().y + me.getY() - pY);
                    }
                });
            }
        }
    
        class OutsidePanel extends JPanel {
    
            public OutsidePanel() {
                setLayout(new BorderLayout());
                add(new MainPanel(), BorderLayout.CENTER);
                add(new BorderPanel(), BorderLayout.PAGE_START);
                setBorder(new LineBorder(Color.BLACK, 5));
            }
        }
    
        private void createAnsShowGui() {
            ComponentResizer cr = new ComponentResizer();
            cr.setMinimumSize(new Dimension(300, 300));
            cr.setMaximumSize(new Dimension(800, 600));
            cr.registerComponent(frame);
            cr.setSnapSize(new Dimension(10, 10));
            frame.setUndecorated(true);
            frame.add(new OutsidePanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new UndecoratedExample().createAnsShowGui();
                }
            });
        }
    }