Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing: How can I change the size of one row in a Grid layout and allow it to resize dynamically only horizontally and not vertically?

Hi everyone I have a JFrame which has three components inside.

  1. A menu
  2. A tabbed pane
  3. a panel that has some buttons

Initially my goal was to create a design like the following: enter image description here

when the user would resize the application, everything would be resized as well. So I thought maybe if I used a simple grid layout my problem would be solved, so I decided to do the following:

  1. The tabbed pane will have some panels inside and those panels will follow the grid layout.

  2. the Panel at the bottom will follow the grid layout.

  3. the JFrame will follow the grid layout as well.

Results:

enter image description here

As you can see this is not what I wanted, the buttons are too big and although the application resizes very well:

enter image description here

The buttons resize as well but they remain too big. I would like to be able to set a small size for the buttons(more specifically the panel they are contained in) and when the user resizes the application, the buttons would resize only horizontally never vertically which means that ONLY the tabbed pane would grow bigger vertically. Is this achievable? I searched and found out about gridbaglayout, I started playing a little bit, but I didn't achieve anything. All I managed to do is the following:

enter image description here

seems good, but when I resize this is what I get:

enter image description here

thanks in advance.

like image 563
ksm001 Avatar asked Sep 26 '12 14:09

ksm001


2 Answers

I would use BorderLayout for the content pane. Put the tabbed pane in it using BorderLayout.CENTER, and the panel of buttons in it using BorderLayout.SOUTH.

like image 150
splungebob Avatar answered Oct 27 '22 19:10

splungebob


the JFrame will follow the grid layout as well.

This is your problem. Instead, use the default BorderLayout (so remove your call that set the layout of the JFrame to a GridLayout) and

  1. Add your button panel with the constraint BorderLayout.SOUTH
  2. Add your tabbed pane without any constraints (which means the constraint BorderLayout.CENTER)

Take a look at the Swing LayoutManager tutorial and in particular, the example that concerns BorderLayout.

Small example:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class TestLayout {

    protected void initUI() {
        final JFrame frame = new JFrame(TestLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenuItem newItem = new JMenuItem("New...");
        JMenuItem open = new JMenuItem("Open...");
        fileMenu.add(newItem);
        fileMenu.add(open);
        menuBar.add(fileMenu);
        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("Tab 1", new JPanel());
        tabs.addTab("Tab 2", new JPanel());
        tabs.addTab("Tab 3", new JPanel());
        JPanel buttonPanel = new JPanel(new GridLayout());
        buttonPanel.add(new JButton("Button-1"));
        buttonPanel.add(new JButton("Button-2"));
        buttonPanel.add(new JButton("Button-3"));
        frame.add(tabs);
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.setJMenuBar(menuBar);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestLayout().initUI();
            }
        });
    }
}
like image 34
Guillaume Polet Avatar answered Oct 27 '22 17:10

Guillaume Polet