Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

how to resize the panel within a jFrame automatically

I am having a JFrame and JPanel.

JPanel is having GridBagLayout(). within the panel I have a button and a JScrollPane and JScrollPane is having table.

I filled the JFrame with JPanel using jframe.setContentPane(jpane).

When I increase or decrease the size of the jframe window its components i.e. jpane is remaining in the middle of the window but it's size is not varying.

So when i increase the size of the jframe window how should I make the jpane to increase its size.
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image


Here is an example, showing the elements you described with the table resizing as the JFrame resizes. It all depends on the exact layouts that you use and how you use them.

When you add a component to another component its best to set the BorderLayout and simply add it, that will make the child component take up the complete parent component.
import javax.swing.*;
import java.awt.*;

/**
 * User: Dejan
 * Date: 20.11.2010
 * Time: 14:52:57
 */
public class ResizeExample extends JFrame {


    public ResizeExample(){
        JPanel panel = new JPanel();

        JButton button = new JButton("test");
        // You probably dont want buttons to resize, so wrap them to another panel:
        JPanel buttonsWrapper = new JPanel();
        buttonsWrapper.add(button);

        String[][] data = new String[][]{new String[]{"a", "b"}, new String[]{"a", "b"}, new String[]{"a", "xx"}};
        JTable table = new JTable(data, new String[]{"aa", "bb"});
        JScrollPane scroll = new JScrollPane(table);
        panel.setLayout(new BorderLayout());
        panel.add(buttonsWrapper, BorderLayout.NORTH);
        // If you ommit the constraint, the component will be added to BorderLayout.CENTER, which takes up whatever space is left
        panel.add(scroll);

        setLayout(new BorderLayout());
        // Again, added to the center:
        add(panel);

        pack();
        setVisible(true);

    }

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

Open in new window

Avatar of shragi

ASKER


I am still have gridbaglayout for the panel. I cannot have border layout since I need add several buttons of varying sizes at specified positions.  

I made the frame to have borderlayout and added the panel to it like this:
frame.setLayout(new BorderLayout());
frame.add(panel);

So when I increase the size of the frame , table and buttons that are added to the panel are remaining in the center.
What should I change so that table and buttons vary their size when frame size is increased with panel still having gridbaglayout.


a LOOONG time ago, I used a percent layout for such a thing
Avatar of shragi

ASKER

So cant I have an automatic resize using gridbaglayout?
Avatar of shragi

ASKER

i am using windows 7
Try flowlayout
Avatar of shragi

ASKER

if it is not possible with the gridbaglayout then I will try other layout options, otherwise I want to use gridbaglayout
have a look here. I no longer do applet stuff
Much more here: http://www.google.com/search?q=gridbaglayout+resizing+panel
ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hmm so why was that supposed to be so hard?

and does it work on a Mac?