Problem with swapping out panels

  • Follow


I have a JComboBox and to it I addItem() 4 JPanels

The idea being that I want whatever one is selected shown in a space
below my comboBox

Problem is, ONLY the first one I add (default) ever shows up. I can
switch to the others and get nothing to show, but then I can switch
back to the default and it will show right up. I have tried a bunch of
different tricks but I seem to be missing it. I have tried setting the
layout again when I switch, using remove and removeAl() on the previous
component. Nothing seems to work. Here is the code snippet.

private void init() {
currentPanel = new RapPanel();
configCombo.addItem(currentPanel);
configCombo.addItem(new FilePanel());
configCombo.addItem(new ServerPanel());
configCombo.addItem(new PermiscuousPanel());

configCombo.addItemListener(new ConfigComboListener());


editPanel.setLayout(new GridLayout(1,1));
this.setLayout(new BorderLayout());
editPanel.add(currentPanel);
this.add(configCombo,BorderLayout.NORTH);
this.add(editPanel,BorderLayout.CENTER);

}
....
private class ConfigComboListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
JComboBox jComboBox = (JComboBox) e.getSource();
editPanel.remove(currentPanel);
//editPanel.removeAll(); // this doesn't work either
currentPanel = (JPanel)jComboBox.getSelectedItem();
editPanel.add(currentPanel);
}
}

private class RapPanel extends JPanel implements ArgumentFormatter{
private JLabel lblIP = new JLabel("IP");
private JLabel lblPort = new JLabel("Port");
private JTextField txtIp = new JTextField();
private JTextField txtPort = new JTextField();

public RapPanel() {
lblIP.setFont(MACFrame.macFontPlain);
lblPort.setFont(MACFrame.macFontPlain);
Util.setMacLookAndFeel(txtIp);
Util.setMacLookAndFeel(txtPort);
this.setLayout(new GridLayout(3,3));

add(lblIP);
add(lblPort);
add(txtIp);
add(txtPort);

}

public String toString() {
return "RAP Input";
}

public String getFormattedArgument() {
return txtIp.getText() + ":" + txtPort.getText();
}

public String getType() {
return InputConfig.CLIENT;
}
}

private class FilePanel extends JPanel implements
ArgumentFormatter{
private JLabel lblFile = new JLabel("File path");
private JTextField txtFile = new JTextField();

public FilePanel() {
lblFile.setFont(MACFrame.macFontPlain);
Util.setMacLookAndFeel(txtFile);
setLayout(new GridLayout(3,1));

add(lblFile);
add(txtFile);
}

public String toString() {
return "File";
}

public String getFormattedArgument() {
return txtFile.getText();
}
public String getType() {
return InputConfig.FILE;
}
    }

Christian Bongiorno
http://www.bongiorno.org/resume.PDF

0
Reply cbongior (40) 1/28/2005 10:34:50 PM

On 28 Jan 2005 14:34:50 -0800, cbongior@stny.rr.com wrote:

> I have a JComboBox and to it I addItem() 4 JPanels..
...
That is fine.  Feel free to pop back in any time if you 
should have any questions.

But as an aside..
<http://www.physci.org/codes/javafaq.jsp#cljg>

> ..Here is the code snippet.

Ughh..  Try this instead <http://www.physci.org/codes/sscce.jsp>
You should be able to replicate this problem in less 
than 80 lines of code.

-- 
Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane
0
Reply SeeMySites (3836) 1/29/2005 12:02:12 PM


Turns out that I needed to call

editPanel.paintAll(editPanel.getGraphics());
I knew it was simple.

I have had to do this trick before with JTables -- in that case it
causes a slight flicker -- but that is for another story

0
Reply cbongior (40) 1/31/2005 4:39:03 PM

2 Replies
31 Views

(page loaded in 0.08 seconds)


Reply: