-1

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+"   ");
        }

    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. .. – Andrew Thompson May 01 '17 at 11:29
  • .. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 01 '17 at 11:32
  • `e.getActionCommand()=="Fragebogen erstellen"` is not how `String`s are compared in Java – MadProgrammer May 01 '17 at 11:34
  • You're calling `draw` twice, which is going to cause no end of issue as it's re-creating many of you objects. In this case, there's no reason for `draw` to be `public`, in fact, for the most part, it could just be part of classes constructor – MadProgrammer May 01 '17 at 11:37

1 Answers1

0

You're calling draw twice, which is going to cause no end of issue as it's re-creating many of you objects.

Basically what's happening, is your double layering your components, you actually have two JComboBoxs on the screen, one you interact with and one which you can't. In your case, it's actually the one you can't interact with which was created last, it's returning 0 constantly when you call getSelectedIndex, because that's what's selected

In this case, there's no reason for draw to be public, in fact, for the most part, it could just be part of classes constructor

e.getActionCommand()=="Fragebogen erstellen" is not how Strings are compared in Java, you should be using String#equals

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366