Reputation: 353
I'm trying to add a JPanel
into another and to fit it to the parent `JPanel size.
I have a JPanel
which contains a JTable
and a JButton
using this code:
JScrollPane attributeTable = new JScrollPane(this);
JPanel attributesPanel = new JPanel();
JPanel textFieldPanel=new JPanel();
JPanel buttonsPanel = new JPanel();
JButton newAttributeButton = new JButton("New Attribute");
attributesPanel.add(textFieldPanel, BorderLayout.CENTER);
attributesPanel.add(buttonsPanel, BorderLayout.SOUTH);
newAttributeButton.addActionListener(AttributesTableController.getInstance());
GroupLayout layout = new GroupLayout(textFieldPanel);
textFieldPanel.setLayout(layout);
// Turn on automatically adding gaps between components
layout.setAutoCreateGaps(true);
// Turn on automatically creating gaps between components that touch
// the edge of the container and the container.
layout.setAutoCreateContainerGaps(true);
// Create a sequential group for the horizontal axis.
GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
// The sequential group in turn contains two parallel groups.
// One parallel group contains the labels, the other the text fields.
// Putting the labels in a parallel group along the horizontal axis
// positions them at the same x location.
//
// Variable indentation is used to reinforce the level of grouping.
hGroup.addGroup(layout.createParallelGroup().
addComponent(attributeTable).addComponent(newAttributeButton));
layout.setHorizontalGroup(hGroup);
// Create a sequential group for the vertical axis.
GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(attributeTable));
vGroup.addGroup(layout.createParallelGroup(Alignment.CENTER).
addComponent(newAttributeButton));
layout.setVerticalGroup(vGroup);
When I had this panel (named attributesPanel
) into a tab of a JTabbedPane
, it just display the JTable
and the JButton
, but in the center of the panel. I would like that the dimension of attributesPanel
are the same as the dimension of opened tab.
This is the code I use to add the JPanel into my JTabbedPane:
TabbedPane tabbedPane = new TabbedPane();
tabbedPane.setComponentAt(1, attributesPanel);
I tried using a GridLayout
, it fitted the JPanel
well but I was not able to resize the button. I tried with a FlowLayout
and a GridBagLayout
, but I was not able to display them correctly because I had the same problem.
Thank you in advance.
Upvotes: 0
Views: 4997
Reputation: 13900
Your example is incomplete sop it is hard to tell what is wrong, but maybe this will help:
JTable table = new JTable (12, 5);
JButton button = new JButton ("Button");
JPanel panel = new JPanel ();
panel.setLayout (new BorderLayout ());
panel.add (table, BorderLayout.CENTER);
panel.add (button, BorderLayout.SOUTH);
JTabbedPane tabbedPane = new JTabbedPane ();
tabbedPane.addTab ("Tab", panel);
JFrame frame = new JFrame ();
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane ().setLayout (new BorderLayout ());
frame.getContentPane ().add (tabbedPane, BorderLayout.CENTER);
frame.pack ();
frame.setVisible (true);
Upvotes: 1