Solved

how to resize the panel within  a jFrame automatically

Posted on 2010-11-19
11
495 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
Technology Partners: 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!

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

710 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