Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a JPanel manually resizable

I have a JFrame with BorderLayout as the layout manager.

In the south border, I have a JPanel, I want this JPanel's size to be adjustable by the user, i.e. the user can click on the edge of the border and drag it up to make it larger.

Is there any way you know that I can do this?

like image 753
saralk Avatar asked Jul 13 '09 15:07

saralk


People also ask

How do you make a JPanel not resizable?

Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. : JFrame Window « Swing « Java Tutorial.

How do you make a frame resizable in Java?

We use absolute positioning for a resizable component. By providing null to the constructor of a JPanel , we create a panel with absolute positioning. addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent me) { requestFocus(); res. repaint(); } });

How do I change the content of a JPanel?

Just call the method pack() after setting the ContentPane , ( java 1.7 , maybe older) like this: JFrame frame = new JFrame(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); .... frame. setContentPane(panel1); frame. pack(); ... frame.

Which method used for resizing the frame?

To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height. Below is the code to change the window size of a JFrame.


2 Answers

In order to make panels in a frame individually resizable you need to add them onto a JSplitPane.

Instead of putting it in the South portion of the Frame, put the JSplitPane in the Center. The split pane will make the bottom panel in the split seem like it is in the South, and the top panel in the split will be in the Center of the frame.

Make sure you set the orientation of the two panels with setOrientation(JSplitPane.VERTICAL_SPLIT ).

Then, you can resize the panels that are in the pane.

like image 82
jjnguy Avatar answered Oct 30 '22 03:10

jjnguy


I think you meant to say JPanel. You can add a custom mouseListener and handle mouse clicks, drags and mouse releases and then resize the panel programmaticaly.

This will demonstrate this. Note that the jframe does NOT resize automatically with the JPanel. To make the effect more visible i painted the panel red and added a beveled border :

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

@SuppressWarnings("serial")
public class ResizablePanel extends JPanel {

    private boolean drag = false;
    private Point dragLocation  = new Point();

    public  ResizablePanel() {
        setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        setPreferredSize(new Dimension(500, 500));
        final JFrame f = new JFrame("Test");
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                drag = true;
                dragLocation = e.getPoint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                drag = false;
            }
        });
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (drag) {
                    if (dragLocation.getX()> getWidth()-10 && dragLocation.getY()>getHeight()-10) {
                        System.err.println("in");
                        setSize((int)(getWidth()+(e.getPoint().getX()-dragLocation.getX())),
                                (int)(getHeight()+(e.getPoint().getY()-dragLocation.getY())));
                        dragLocation = e.getPoint();
                    }
                }
            }
        });
        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(this,BorderLayout.CENTER);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new ResizablePanel();
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.red);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

}
like image 28
Savvas Dalkitsis Avatar answered Oct 30 '22 03:10

Savvas Dalkitsis