Changing Panel view from JComboBox

Hi all.

I hava a problem with my Gui. I have:

1 Frame with 2 panels (JPanel)

on first JPanel I have JComboBox object

and on second panel i want to have different objects for each

JComboBox Item :(JSlider, JTable etc) and i dont know how to do this.

I'm beginner in java

My code that works bad (creating a another Jpanel inside second JPanel

depending of choice JComboBox)

publicclass MainFrameextends JFrame{

(...Code...)

public MainFrame(){

(...Code...)

//mainDesktops@MainFrame

//1->z modelami

mainDesktop1 =new JPanel();

comboBox =new JComboBox();

mainDesktop1.add(comboBox);

comboBox.setBackground(Color.WHITE);

for(int i=0; i<comboBoxItems.length;i++)

comboBox.addItem(comboBoxItems[i]);

comboBox.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent eve){

mainDesktop2.removeAll();

mainDesktop2.validate();

mainDesktop2.add( setView(comboBox.getSelectedIndex()),BorderLayout.CENTER );

validate();

}//actionPerf

});

//2 -> opcje modeli

mainDesktop2 =new JPanel();

titleBorder2 =

BorderFactory.createLineBorder(Color.RED);

add(mainDesktop2,BorderLayout.CENTER);

mainDesktop2.setBackground(Color.BLACK);

mainDesktop2.setBorder(new

javax.swing.border.TitledBorder(titleBorder2,titleBorder));

(...Code...)

}//konstruktor

(...Code...)

JPanel setView(int wybor){

JPanel pan =new JPanel();

if (wybor==0){

bscLabel.setText("\n BER - probablity: \n" +

(int)(bscKanal.getProbablity()*100) +"%");

bscLabel.setForeground(Color.BLUE);

bscSlider.setMajorTickSpacing(20);

bscSlider.setPaintTicks(true);

bscSlider.setPaintLabels(true);

bscSlider.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));

bscKanal.setProbablity(bscSlider.getValue()/

100);

pan.add(bscLabel);

pan.add(bscSlider,BorderLayout.AFTER_LAST_LINE);

}

return pan;

}

(...Code...)

}//class

I will be thankfull if someone show a code that changing panel view

from JComboBox or any Idea how to do this

thanks

[3621 byte] By [RafalJa] at [2007-11-27 10:35:47]
# 1

Don't really understand your question so I'll point you to the Swing tutorial on "Using Card Layout" which shows how to change panels by selecting an item in a combo box:

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

In the future, Swing related questions should be posted in the Swing forum.

camickra at 2007-7-28 18:37:10 > top of Java-index,Desktop,Core GUI APIs...