Two Components, one with a set width
This seems like a completely rudimentary question but I'm a beginner with swing. I have a resizable JFrame with a JPanel to the left and and a JTabbedPane to the right. I want the JTabbedPane to have a constant width and grow in height to match the JFrame height. The JPanel should fill the remaining width and height of the JFrame. What am I doing wrong here?
public Example()
{
f = new JFrame();
f.setResizable(true);
f.setPreferredSize(new Dimension(1000, 800));
f.setLayout(new FlowLayout());
JPanel viewerPanel = new JPanel()
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
System.out.println("W:" + getSize().width + ", H:" + getSize().height);
g2d.fillRect(0, 0, getSize().width, getSize().height);
}
};
f.getContentPane().add(viewerPanel);
JTabbedPane tabs = new JTabbedPane();
tabs.setSize(400, f.getHeight());
tabs.addTab("Tab1", new JLabel("Content1"));
tabs.addTab("Tab2", new JLabel("Content2"));
tabs.addTab("Tab3", new JLabel("Content3"));
tabs.addTab("Tab4", new JLabel("Content4"));
f.getContentPane().add(tabs);
f.pack();
f.setVisible(true);
}
Solution 1:
Best way is to let the layout do the work and setting preferred or minimum sizes :
Try using a BorderLayout and adding the panel to the center (which grows both in height and width) and adding your tabbedPane to the RIGHT (which grows in height) while setting a preferred or minimum size to your tabbedPane
Your tabbedPane should always have the same with, while the panel will grow according to the size there is left for him