Make JPanel resizable

507 views Asked by At

I want to make a draggable and resizable panel which also can be closed/removed. Dragging the panel is working good, but there is some error in my resizing code. It is working but not very well. Maybe there is someone who can help me to fix it. The issue is to set the correct location of the labels "close" and "resize".

Thank you for help

public class PanelDraggable extends JPanel {

    private Point pointPressed;
    int width1;
    int height1;
    int headerHeight;
    int closeDim;

    JLabel close;
    JLabel resize;
    JPanel cs;
    JLabel header;

    public PanelDraggable( int x,  int y, int width, int height, String lblName) {

        //        draggable.setCursor(draggable.getCursor());
        //        setCursor(new Cursor(Cursor.HAND_CURSOR));

        width1 = width; height1 = height;
        closeDim = 15;

        headerHeight = 20;

        setBounds(x, y, width, height);
        this.setLayout(null);
        setBorder(new LineBorder(new Color(147, 147, 255), 3, true));
        this.setOpaque(true);


        //header
        header = new JLabel(lblName);
        this.add(header);
        header.setBounds(0, 0, width-(closeDim + 2), headerHeight);
        header.setBorder(new LineBorder(new Color(147, 147, 255), 3, true));
        header.setBackground(new Color(147, 147, 255));
        header.setOpaque(true);
        MouseInputAdapter mouseAdapter = new HeaderMouseHandler();
        header.addMouseMotionListener(mouseAdapter);
        header.addMouseListener(mouseAdapter);

        //close Button
        close = new JLabel("cx");
        close.setIcon(new ImageIcon("icons/close_16x16.png"));
        close.setBounds( (width- (closeDim + 2)) , 2, closeDim, closeDim);
        close.setCursor(new Cursor(Cursor.HAND_CURSOR));
        this.add(close);

        //Container
        cs = new JPanel();
        cs.setBounds(0, headerHeight, width, (height-headerHeight) );
        cs.setOpaque(false);
        cs.setBackground(Color.red);
        this.add(cs);

        //Resise
        resize = new JLabel("re");
        resize.setBounds( (width-closeDim), (height-closeDim) , closeDim, closeDim);
        resize.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
        this.add(resize);

        MouseInputAdapter resizeMouseAdapter = new ResizeMouseHandler();
        resize.addMouseMotionListener(resizeMouseAdapter);
        resize.addMouseListener(resizeMouseAdapter);

    }


    private class HeaderMouseHandler extends MouseInputAdapter {

        public void mouseDragged(final MouseEvent e) {
            Point pointDragged = e.getPoint();
            Point loc = getLocation();
            loc.translate(pointDragged.x - pointPressed.x,
                pointDragged.y - pointPressed.y);
            setLocation(loc);

        }

        public void mousePressed(final MouseEvent e) {

                pointPressed = e.getPoint();

        }
    }

    private class ResizeMouseHandler extends MouseInputAdapter {

        public void mouseDragged(final MouseEvent e) {
            Point pointDragged = e.getPoint();

            int w = width1 + pointDragged.x;
            int h = height1 + pointDragged.y;

            setSize(w , h);
            cs.setSize(w, (h-headerHeight) );
            header.setSize(w -(closeDim + 2), headerHeight);

            //Point loc = getLocation();
            //loc.translate(pointDragged.x - pointPressed.x,
                //pointDragged.y - pointPressed.y);

            //System.out.println("p: "+Integer.toString((pointDragged.x - pointPressed.x)) + " / " + Integer.toString((pointDragged.y - pointPressed.y)));
            //System.out.println("n: "+Integer.toString(w) + " / " + Integer.toString(h));
            //System.out.println("o: "+Integer.toString(width1) + " / " + Integer.toString(height1));

            Point resizeloc = new Point();
            resizeloc.translate(w-closeDim, h-closeDim);
            resize.setLocation(resizeloc);

            //Point closeloc = new Point();
            //closeloc.translate((w - (closeDim + 2)) , 2 );
            //close.setLocation(closeloc);


            revalidate(); 
            repaint();
        }

        public void mousePressed(final MouseEvent e) {

                pointPressed = e.getPoint();

        }
    }
}
1

There are 1 answers

0
coo12 On

Now solved. The problem was that the Dimensions of the panel (which are my references for the new locations of the labels) were not variable.

int w = width1 + pointDragged.x;
        int h = height1 + pointDragged.y;