Im just working on a JFrame.I added a JComboBox there but unfortunately the JComboBox Index, and so the selected Item doesn't change upon "the action of changing", I mean when I select another Item on the Swing Frame. When requested, it only returns an Index of 0, no matter what Item is selected. The name of the ComboBox is "Kataloge".
It does not return me any Error. How can I fix this ?
static BufferedImage icon;
private JButton update;
private JButton getKata;
private JComboBox<String> Kataloge;
private JLabel Title;
private JLabel WhichKatalog;
private JLabel WhichDatum;
private JLabel line;
private JPanel topper;
private JPanel middle;
private JPanel bottom;
private JPanel frame;
public String Katalog = "Fragenkatalog 1 (normiert)";
public static void main(String args[]) {
MainFrame frame = new MainFrame();
frame.draw();
}
public MainFrame(){
setTitle("Fragebogen erstellen Section");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
}
draw();
setResizable(false);
try {
icon = ImageIO.read(new File("icon.png"));
} catch (IOException e) {
e.printStackTrace();
}
setIconImage(icon);
setVisible(true);
}
public void draw(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
update = new JButton();
getKata = new JButton();
Title = new JLabel();
line = new JLabel();
WhichKatalog = new JLabel();
WhichDatum = new JLabel();
topper = new JPanel();
middle = new JPanel();
bottom = new JPanel();
frame = new JPanel();
setSize(575, 220);
getKata.setFont(new java.awt.Font("Tahoma", 0, 14));
getKata.setText("Fragebogen erstellen");
getKata.addActionListener(new ActionHandler());
update.setFont(new java.awt.Font("Tahoma", 0, 14));
update.setText("Katalog bearbeiten");
update.addActionListener(new ActionHandler());
Kataloge = new JComboBox<String>();
Kataloge.addItem("Fragenkatalog 1 (normiert)");
Kataloge.addItem("Fragenkatalog 2 (normal)");
Kataloge.setFont(new java.awt.Font("Tahoma", 0, 14));
Kataloge.addItemListener(new ItemHandlerMainFrame());
Title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
Title.setText("Main Menu");
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
WhichDatum.setText(new Datum().getDatum());
line.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
frame.setSize(575, 220);
topper.setSize(575,40);
topper.setLayout(new FlowLayout());
topper.add(Title, CENTER_ALIGNMENT);
middle.setSize(575, 150);
middle.setLayout(null);
middle.add(getKata).setBounds(15, 30, 160, 30);;
middle.add(update).setBounds(195,30,145,30);;
middle.add(Kataloge).setBounds(360, 30, 200, 30);;
bottom.setSize(575,30);
bottom.setLayout(new BorderLayout(50,5));
bottom.add(line, BorderLayout.NORTH);
bottom.add(WhichKatalog, BorderLayout.WEST);
bottom.add(WhichDatum, BorderLayout.EAST);
frame.setLayout(null);
frame.add(topper).setBounds(0, 10, 575, 40);;
frame.add(middle).setBounds(0,45,575,60);;
frame.add(bottom).setBounds(15, 150, 535, 30);;
getContentPane().add(frame);
setLocationRelativeTo(null);
setLocationRelativeTo(null);
}
public void close(){
this.setVisible(false);
this.dispose();
}
private class ActionHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand()=="Fragebogen erstellen"){
close();
FragebogenErstellen frame = new FragebogenErstellen();
frame.drawIt();
}
}
}
private class ItemHandlerMainFrame implements ItemListener{
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange() == ItemEvent.SELECTED){
System.out.println("Changed to: "+Kataloge.getSelectedIndex());
}
//Katalog = (String) Kataloge.getSelectedItem();
if (Katalog == "Fragenkatalog 1 (normiert)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog);
}if (Katalog == "Fragenkatalog 2 (normal)"){
WhichKatalog.setText("Ausgewählter Katalog: "+Katalog+" ");
}
}
}