views:

1623

answers:

3

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?

A: 

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());
    }

}
Savvas Dalkitsis
That is way over the top! A simple `JSplitPane` will get the job done.
jjnguy
i know. i just answered his question.
Savvas Dalkitsis
It is an interesting solution though.
jjnguy
the problem is that the jframe does not automatically resize with the panel. i tried pack() in every call (expensive i know) but didn't work. Maybe a different layout manager will do the trick?
Savvas Dalkitsis
A: 

You might have to specify JFrame.setResizeable = true; on both the Parent JFrame(the one with the border layout) and the child JFrame.

You also might want to use a JPanel in the south border.

He needs to resize the panel, not the frame
jjnguy
+4  A: 

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 teh orientation of the two panelse with setOrientation(JSplitPane.VERTICAL_SPLIT ) .

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

jjnguy