Article View: comp.lang.java.api
Article #16SWING, layout relationship problem, how do i fix this sample?
From: Henrik Wahlberg
Date: Wed, 24 Nov 1999 00:00
Date: Wed, 24 Nov 1999 00:00
147 lines
4787 bytes
4787 bytes
This is a multi-part message in MIME format.
--------------CAFF8DBCE0336FC7EC19F6D2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi.
I'm trying to make i combo panel similar to the tabpane, but with the tabs replaced by a Combobox.
To achieve this I have subclassed a JPanel, called PDropDownPane. In this I setup a borderlayout, and add a combobox in the top (NORTH) and a cardlayout in CENTER.
To test it, I add this panel to a JFrame center, and add an "ADD" button on the NORTH border, to test adding panels dynamically. This snip is placed in DDTest.java.
The problem is, that the combobox gets overlayed (most of the time) by the cardpane layout, which then leaves a blank(gray) area in the SOUTH border region, same height as the combobox or the ADD button.
Before loosing all of my hair, please look at my code??
Is there a tool to visualize the "window" relationship ?. On MS Windows i had a tool to do this kind of spying, but I belive it's no good for swing.
--
MVH/Regards Henrik Wahlberg, EDB Gruppen A/S.
henrik.wahlberg.#.herning.mail.telia.com (Replace .#. with @)
--------------CAFF8DBCE0336FC7EC19F6D2
Content-Type: text/plain; charset=us-ascii;
name="PDropDownPane.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="PDropDownPane.java"
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class PDropDownPane extends JPanel {
JComboBox selector; //frame.north has combobox to select which pane is shown below
JPanel layoutArea; //frame.center will contain this panel with cardlayout manager
public PDropDownPane() {
setLayout(new BorderLayout());
selector = new JComboBox();
selector.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
selectLayout(e.getItem().toString());
}
}
});
add(selector, BorderLayout.NORTH);
layoutArea = new JPanel(new CardLayout());
add(layoutArea, BorderLayout.CENTER);
}
public void addTab(String a, Icon b, JComponent c, String d) {
selector.addItem(a);
layoutArea.add(c,a);
c.setToolTipText(a);
this.repaint();
}
public Component getComponent(int index) {
return layoutArea.getComponent(index);
}
public int getSelectedIndex() {
return selector.getSelectedIndex();
}
void selectLayout(String layout) {
((CardLayout)layoutArea.getLayout()).show(layoutArea, (String)layout);
}
public void setSelectedIndex(int nChild) {
selector.setSelectedIndex(nChild);
}
}
--------------CAFF8DBCE0336FC7EC19F6D2
Content-Type: text/plain; charset=us-ascii;
name="DDTest.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="DDTest.java"
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DDTest extends JFrame {
PDropDownPane dd = null;
int nChild = 0;
public DDTest() {
super();
init();
}
public DDTest(String title) {
super(title);
init();
}
public void init() {
dd = new PDropDownPane();
getContentPane().add(dd, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//Add some initial pages
for (int n = 1; n < 5; n++) {
JTree jt = new JTree();
dd.addTab("Startup "+n, null, jt, "Startup "+n);
}
//Add an "ADD" button to add panels at runtime
JButton addButton = new JButton("Add");
getContentPane().add(addButton, BorderLayout.NORTH);
ActionListener addAction = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTree jt = new JTree();
nChild++;
dd.addTab("Dynamic "+nChild, null, jt, "Dynamic "+nChild);
dd.setSelectedIndex(nChild);
}
};
addButton.addActionListener(addAction);
}
public static void main(String[] args) {
DDTest f = new DDTest("GridLineLayoutTest");
f.setSize(600, 400);
f.setLocation(100, 100);
f.setVisible(true);
}
}
--------------CAFF8DBCE0336FC7EC19F6D2--
Technical Details
Message-ID:
<383C2AC5.62719436@herning.mail.telia.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f9a23,8b919f0df6571ed8,start X-Google-Attributes: gidf9a23,public X-Google-Thread: fecd1,8b919f0df6571ed8,start X-Google-Attributes: gidfecd1,public From: Henrik Wahlberg <henrik.wahlberg@herning.mail.telia.com> Subject: SWING, layout relationship problem, how do i fix this sample? Date: 1999/11/24 Message-ID: <383C2AC5.62719436@herning.mail.telia.com> X-Deja-AN: 552601991 X-Accept-Language: en,da Content-Type: multipart/mixed; boundary="------------CAFF8DBCE0336FC7EC19F6D2" Organization: EDB Gruppen Systems A/S Mime-Version: 1.0 Newsgroups: comp.lang.java.api,dk.edb.programmering.java