Hi everyone I have a JFrame which has three components inside.
Initially my goal was to create a design like the following:
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:
The tabbed pane will have some panels inside and those panels will follow the grid layout.
the Panel at the bottom will follow the grid layout.
the JFrame will follow the grid layout as well.
Results:
As you can see this is not what I wanted, the buttons are too big and although the application resizes very well:
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:
seems good, but when I resize this is what I get:
thanks in advance.
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
.
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
BorderLayout.SOUTH
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();
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With