Как получить значение выбранного JComboBox в переменную, которая может быть использована в ActionListener? - PullRequest
0 голосов
/ 28 октября 2019

Я знаю, как я могу получить значение выделенного текста, но я не знаю, как я могу использовать его в ActionListener.

Я пытался создать глобальную переменную и сохранить значениев этом, но, к сожалению, это неправильная идея. Я все еще получаю сообщение об ошибке, что значение этой переменной застряло в методе public void itemStateChanged(ItemEvent arg0){.

String [] WarenTypListe = {"","Hochfrequente Ware", "Normale Ware"};
JComboBox comboBoxHochfrequent = new JComboBox(WarenTypListe);
comboBoxHochfrequent.addItemListener(new ItemListener() {
    public void itemStateChanged(ItemEvent arg0) {
        if(arg0.getStateChange() ==ItemEvent.SELECTED) {
            //System.out.println(comboBoxHochfrequent.getSelectedIndex());
            int s= comboBoxHochfrequent.getSelectedIndex();

        }
    }
});
comboBoxHochfrequent.setFont(new Font("Tahoma", Font.BOLD, 30));
comboBoxHochfrequent.setBounds(600, 144, 224, 49);
contentPane.add(comboBoxHochfrequent);

JButton btnArtikelHinzufuegen = new JButton("Artikel hinzuf\u00FCgen");
btnArtikelHinzufuegen.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        DBConnect connect = new DBConnect();
        int s = comboBoxHochfrequent.getSelectedIndex(); // <-- here i try to use the variable s to get the value which was selected in the combo box
           boolean hochfrequent = false;
            if (s==1) {
                hochfrequent = true;
        }
        if (s==2) {
            hochfrequent = false;
        }
        System.out.println("es hat geklappt"+s);
        try {
            //(int arikel_ID, String artikelname, int anzahl, int VE_Menge, String Zielort, int Regalnummer, boolean hochfrequent)

        connect.setWareHinzufuegen(ArtikelIdInt, Artikelname, AnzahlWare, VeInt, zielort, regalnummerInt, hochfrequent);
        }
        catch (Exception e) {
            System.err.println(e);
        }
    }
});

Я надеюсь, что вы можете помочь мне создать переменную, которую я могу использовать в ActionListener.

Редактировать: я использую System.out.println для проверки кода.

...