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.