Solved

how to resize the panel within  a jFrame automatically

Posted on 2010-11-19
11
451 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 video teaches viewers about errors in exception handling.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now