Re: Adding Component above all other components
On 16 =C1=D7=C7, 15:06, Royan <romayan...@gmail.com> wrote:
My initial problem is to put the panel (which is created as a result
of a click on a button) above all components that have been added to
the GBLTablePanel. I've written the following sample that demonstrates
my problem
package test;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
public class GBLTablePanel extends JPanel {
=9A =9A private final GridBagLayout layout;
=9A =9A private final GridBagConstraints gbc;
=9A =9A public GBLTablePanel() {
=9A =9A =9A =9A layout = new GridBagLayout();
=9A =9A =9A =9A gbc = new GridBagConstraints();
=9A =9A =9A =9A setLayout(layout);
=9A =9A =9A =9A JTable table = new JTable();
=9A =9A =9A =9A table.setModel(new DefaultTableModel(new Object[][] {
=9A =9A =9A =9A =9A =9A =9A =9A { "aaaaaaaaaaaaaaa", 3, "a", null }, { "a=
dasda", 10, "b", null },
=9A =9A =9A =9A =9A =9A =9A =9A { "aaaaaaaaaaaaaaa", 3, "a", null }, { "a=
dasda", 10, "b", null },
=9A =9A =9A =9A =9A =9A =9A =9A { "aaaaaaaaaaaaaaa", 3, "a", null }, { "a=
dasda", 10, "b", null },
=9A =9A =9A =9A =9A =9A =9A =9A { "aaaaaaaaaaaaaaa", 3, "a", null }, { "a=
dasda", 10, "b", null },
=9A =9A =9A =9A =9A =9A =9A =9A { "assssss", 55, "c", null }, { "asdafasf=
a", 44, "d", null } },
=9A =9A =9A =9A =9A =9A =9A =9A new String[] { "Title 1", "Title 2", "Tit=
le 3", "Title 4" }));
=9A =9A =9A =9A JScrollPane scrollPane = new JScrollPane(table);
=9A =9A =9A =9A gbc.gridx = 0;
=9A =9A =9A =9A gbc.gridy = 0;
=9A =9A =9A =9A gbc.fill = GridBagConstraints.BOTH;
=9A =9A =9A =9A gbc.weightx = 1.0;
=9A =9A =9A =9A gbc.weighty = 1.0;
=9A =9A =9A =9A layout.setConstraints(scrollPane, gbc);
=9A =9A =9A =9A add(scrollPane);
=9A =9A =9A =9A JButton showPanelBtn = new JButton(new AbstractAction("=
Show panel") {
=9A =9A =9A =9A =9A =9A @Override
=9A =9A =9A =9A =9A =9A public void actionPerformed(ActionEvent e) {
=9A =9A =9A =9A =9A =9A =9A =9A /* When this button is clicked I want tha=
t panel to appear on the
top (above all components of the GBLTablePanel ) */
=9A =9A =9A =9A =9A =9A =9A =9A JPanel panel = new JPanel();
=9A =9A =9A =9A =9A =9A =9A =9A panel.add(new JLabel("This must be on the=
top of JTable"));
=9A =9A =9A =9A =9A =9A =9A =9A panel.setSize(panel.getSize().width, 30);
=9A =9A =9A =9A =9A =9A =9A =9A panel.setPreferredSize(new Dimension(pane=
l.getPreferredSize().width,
30));
=9A =9A =9A =9A =9A =9A =9A =9A layout.setConstraints(panel, gbc);
=9A =9A =9A =9A =9A =9A =9A =9A gbc.gridy = 0;
=9A =9A =9A =9A =9A =9A =9A =9A GBLTablePanel.this.add(panel, 0);
=9A =9A =9A =9A =9A =9A }
=9A =9A =9A =9A });
=9A =9A =9A =9A gbc.gridwidth = GridBagConstraints.REMAINDER;
=9A =9A =9A =9A gbc.gridy = 1;
=9A =9A =9A =9A layout.setConstraints(scrollPane, gbc);
=9A =9A =9A =9A add(showPanelBtn);
=9A =9A }
=9A =9A public static void main(String[] args) {
=9A =9A =9A =9A JFrame f = new JFrame("Test");
=9A =9A =9A =9A f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)=
;
=9A =9A =9A =9A /* Construct table managed by GridBagLayout */
=9A =9A =9A =9A GBLTablePanel panel = new GBLTablePanel();
=9A =9A =9A =9A f.add(panel);
=9A =9A =9A =9A f.pack();
=9A =9A =9A =9A f.setVisible(true);
=9A =9A }
}
I hope the example is not too confusing, honestly I'm not sure what do
I try next to fix my problem. The panel simply does not show up :(
OK it seems like calling validate/revalidate:
GBLTablePanel.this.add(panel, 0);
GBLTablePanel.this.revalidate();
will make JLabel visible, but thus JTable gets disappeared, why?!?!