Solved

how to resize the panel within  a jFrame automatically

Posted on 2010-11-19
11
442 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
Comment Utility

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
Comment Utility

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
Comment Utility
a LOOONG time ago, I used a percent layout for such a thing
0
 

Author Comment

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

Expert Comment

by:Michel Plungjan
Comment Utility
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:shragi
Comment Utility
i am using windows 7
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Try flowlayout
0
 

Author Comment

by:shragi
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
Hmm so why was that supposed to be so hard?

and does it work on a Mac?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

743 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

17 Experts available now in Live!

Get 1:1 Help Now