Problem with swapping out panels

C

cbongior

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
 
C

cbongior

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top