What I am trying to do is to create a JLabel dependent of the selection in the combo box, i.e. if "History Book" is selected create a label "Enter period", if "Fictional Book" is selected create label "Enter genre". The thing is no matter what I select the Label is not visible, the place it should be stays empty. Any ideas where I am wrong ?
Here is my code:
JComboBox comboBox = new JComboBox(bookTypes);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"History Book", "Fictional Book", "Textbook"}));
comboBox.setToolTipText("Choose the type of book here!");
comboBox.setBounds(196, 48, 156, 20);
ActionListener cbActionListener = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s = (String) comboBox.getSelectedItem();
if(s.equals("History Book")){
JLabel lblWriteThePeriod = new JLabel("Write the Period:");
lblWriteThePeriod.setBounds(10, 317, 142, 14);
dialog.getContentPane().add(lblWriteThePeriod);
}else if(s.equals("Fictional Book")){
JLabel lblWriteTheGenre = new JLabel("Write the genre:");
lblWriteTheGenre.setBounds(10, 317, 142, 14);
dialog.getContentPane().add(lblWriteTheGenre);
}
}
};
comboBox.addActionListener(cbActionListener);
dialog.getContentPane().add(comboBox);
dialog.setVisible(true);