简体   繁体   中英

Actionlistener in JComboBox not working properly

So i got this Actionlistener (Check code below)

    import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListCellRenderer;

import java.awt.TextArea;

import javax.swing.JTextPane;

import java.awt.Component;
import java.awt.TextField;
import java.awt.Label;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Arrays;

import javax.swing.JComboBox;

import java.awt.event.ActionEvent;


public class Frame extends Register {
private int TempInt;
    JFrame frame;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() 
        {
            public void run() {
                try {
                    Frame window = new Frame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public void windowClosing (WindowEvent e) {
        JOptionPane.showMessageDialog(frame, "Programmet sparas och kommer nu stängas av");
        System.exit(1);
    }


    /**
     * Create the application.
     */
    public Frame() {

        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    void initialize() {

        frame = new JFrame();
        frame.setBounds(100, 100, 450, 424);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);




        final TextArea textArea = new TextArea();
        textArea.setEditable(false);        
        textArea.setBounds(0, 105, 440, 243);
        frame.getContentPane().add(textArea);


        // Använd k.setBelopp beroende på vilken man väljer


        String s = "";
        int il = beraknaSaldo();
        s = Integer.toString(il);
        JTextPane saldoText = new JTextPane();
        saldoText.setText(s);
        saldoText.setEditable(false);
        saldoText.setBounds(208, 354, 42, 22);
        frame.getContentPane().add(saldoText);

        final TextField beloppField = new TextField();
        beloppField.setEditable(false);
        beloppField.setBounds(120, 10, 102, 22);
        frame.getContentPane().add(beloppField);


        final TextField tidField = new TextField();
        tidField.setEditable(false);
        tidField.setBounds(255, 10, 102, 22);
        frame.getContentPane().add(tidField);

        Label saldoLabel = new Label("Saldo:");
        saldoLabel.setBounds(162, 354, 40, 22);
        frame.getContentPane().add(saldoLabel);

        Label beloppLabel = new Label("Belopp:");
        beloppLabel.setBounds(72, 10, 50, 22);
        frame.getContentPane().add(beloppLabel);

        Label tidLabel = new Label("Tid:");
        tidLabel.setBounds(228, 10, 22, 22);
        frame.getContentPane().add(tidLabel);

        Label besökartypLabel = new Label("Bes\u00F6kartyp");
        besökartypLabel.setBounds(0, 77, 62, 22);
        frame.getContentPane().add(besökartypLabel);

        Label beloppLabel_1 = new Label("Belopp");
        beloppLabel_1.setBounds(122, 77, 62, 22);
        frame.getContentPane().add(beloppLabel_1);

        Label tidLabel_1 = new Label("Tid");
        tidLabel_1.setBounds(246, 77, 22, 22);
        frame.getContentPane().add(tidLabel_1);

        Label lopnrLabel = new Label("L\u00F6pnummer");
        lopnrLabel.setBounds(345, 77, 79, 22);
        frame.getContentPane().add(lopnrLabel);

        final JComboBox<String> comboBox = new JComboBox<String>();
        comboBox.addItem("Företag");
        comboBox.addItem("Normal");
        comboBox.addItem("Student");
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                int kb = 0;


                JComboBox<?> comboBox = (JComboBox<?>) event.getSource();
                Object selected = comboBox.getSelectedItem();
                 if(selected.toString().equals("Normal")) {
                        Register r = new Register();
                        Kund k = new Normal();
                        k.setBelopp(100);
                        k.setDatum("20:15");
                        r.regKund(k);
                        int tempbelopp = k.getBelopp();
                        String b = Integer.toString(tempbelopp);
                        beloppField.setText(b);
                        kb = kb + 100;
                        tidField.setText("20:15");
                        textArea.append(r.getLista()); 
                        }
                    else if(selected.toString().equals("Student")) {
                        Register r = new Register();
                        Kund k = new Normal();
                        k=new Student();
                        k.setBelopp(50);
                        k.setDatum("20:16");
                        beloppField.setText("100");
                        tidField.setText("20:15");
                        if( kb >=50)
                            r.regKund(k);
                            kb = kb - 50;

                                                textArea.append("Det finns inga cash, student reggades ej!" + "\n");

                }
                    else if(selected.toString().equals("Företag")) {
                        Register r = new Register();
                        Kund k = new Normal();
                        k.setBelopp(0);
                        k.setDatum("20:17");
                        k.setLopnummer(r.getLopnummer());
                        r.regKund(k);
                        // Den skriver ut samma skit ändra det
                        textArea.append(r.getLista());
                        textArea.append("Kassan har nu "+kb+" kr" + "\n");
                    }

            }
        });
        comboBox.setToolTipText("Välj vilken typ av Kund du är");
        comboBox.setRenderer(new MyComboBoxRenderer("Välj..."));  
        comboBox.setSelectedIndex(-1);
        comboBox.setBounds(171, 46, 97, 22);        
        frame.getContentPane().add(comboBox);


    }
    }


class MyComboBoxRenderer extends JLabel implements ListCellRenderer<Object> {
    private String title;

    public MyComboBoxRenderer(String newTitle) {
        title = newTitle;
    }

    @Override
    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean hasFocus) {
        if (index == -1 && value == null) setText(title );
        else setText(value.toString());
        return this;
    }
}

And it gives me error cause of this line comboBox.setSelectedIndex(-1); is there any way to change it. Since what it does it sets the comboBox defult text to "Välj..." (Which means choose in swedish) i would like to keep this without removing it. When i remove that line the program works perfectly.

When setSelectedIndex is called this statement should throw an NPE (not tested)

Object selected = comboBox.getSelectedItem();

since nothing is selected. You could simply move the setSelectedIndex statement before the ActionListener is registered with the combobox

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM