Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #4004

Adding Component above al

From "Royan" <royan@THRWHITE.remove-dii-this>
Subject Adding Component above al
Message-ID <0962ef85-9071-436b-937a-d2f8e402a882@e39g2000hsf.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
Date 2011-04-27 15:48 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
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 {
    private final GridBagLayout layout;
    private final GridBagConstraints gbc;

    public GBLTablePanel() {
	layout = new GridBagLayout();
	gbc = new GridBagConstraints();
	setLayout(layout);

	JTable table = new JTable();

	table.setModel(new DefaultTableModel(new Object[][] {
		{ "aaaaaaaaaaaaaaa", 3, "a", null }, { "adasda", 10, "b", null },
		{ "aaaaaaaaaaaaaaa", 3, "a", null }, { "adasda", 10, "b", null },
		{ "aaaaaaaaaaaaaaa", 3, "a", null }, { "adasda", 10, "b", null },
		{ "aaaaaaaaaaaaaaa", 3, "a", null }, { "adasda", 10, "b", null },
		{ "assssss", 55, "c", null }, { "asdafasfa", 44, "d", null } },
		new String[] { "Title 1", "Title 2", "Title 3", "Title 4" }));

	JScrollPane scrollPane = new JScrollPane(table);

	gbc.gridx = 0;
	gbc.gridy = 0;
	gbc.fill = GridBagConstraints.BOTH;
	gbc.weightx = 1.0;
	gbc.weighty = 1.0;
	layout.setConstraints(scrollPane, gbc);

	add(scrollPane);

	JButton showPanelBtn = new JButton(new AbstractAction("Show panel") {
	    @Override
	    public void actionPerformed(ActionEvent e) {

		/* When this button is clicked I want that panel to appear on the
top (above all components of the GBLTablePanel ) */
		JPanel panel = new JPanel();
		panel.add(new JLabel("This must be on the top of JTable"));
		panel.setSize(panel.getSize().width, 30);
		panel.setPreferredSize(new Dimension(panel.getPreferredSize().width,
30));

		layout.setConstraints(panel, gbc);
		gbc.gridy = 0;
		GBLTablePanel.this.add(panel, 0);
	    }
	});

	gbc.gridwidth = GridBagConstraints.REMAINDER;
	gbc.gridy = 1;
	layout.setConstraints(scrollPane, gbc);
	add(showPanelBtn);
    }

    public static void main(String[] args) {
	JFrame f = new JFrame("Test");
	f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

	/* Construct table managed by GridBagLayout */
	GBLTablePanel panel = new GBLTablePanel();
	f.add(panel);
	f.pack();
	f.setVisible(true);
    }
}


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 :(

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | Next | Find similar


Thread

Adding Component above al "Royan" <royan@THRWHITE.remove-dii-this> - 2011-04-27 15:48 +0000

csiph-web