Solved

how to resize the panel within  a jFrame automatically

Posted on 2010-11-19
11
512 Views
Last Modified: 2012-05-10
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.
0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 17

Expert Comment

by:Dejan Pažin
ID: 34179759

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

0
 

Author Comment

by:shragi
ID: 34179827

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.


0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34179839
a LOOONG time ago, I used a percent layout for such a thing
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:shragi
ID: 34179854
So cant I have an automatic resize using gridbaglayout?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34179903
0
 

Author Comment

by:shragi
ID: 34179906
i am using windows 7
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34179921
Try flowlayout
0
 

Author Comment

by:shragi
ID: 34179928
if it is not possible with the gridbaglayout then I will try other layout options, otherwise I want to use gridbaglayout
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34179942
have a look here. I no longer do applet stuff
Much more here: http://www.google.com/search?q=gridbaglayout+resizing+panel
0
 
LVL 17

Accepted Solution

by:
Dejan Pažin earned 125 total points
ID: 34180027

Of course you can use gridBagLayout. Here is the code:
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(new GridBagLayout());

        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"});
        JButton button = new JButton("test");
        JScrollPane scroll = new JScrollPane(table);

        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 0;

        panel.add(button, c);

        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 1;
        c.gridx = 0;
        c.gridy = 1;

        panel.add(scroll, c);

        setLayout(new BorderLayout());
        add(panel);

        pack();
        setVisible(true);
    }

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

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34183412
Hmm so why was that supposed to be so hard?

and does it work on a Mac?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question