Frage

I want to add a JComboBox and a JTextField to a panel when there is an item selected in the combobox.

I have this code for the panel.

aantallenGebruikt.add(new JTextField("", 5));
onderdelenGebruikt.add(new JComboBox(onderdelenBox()));
onderdelenGebruikt.get(0).addActionListener(MyFrame.this);

panelAfronden = new JPanel();
panelAfronden.setLayout(new FlowLayout());

panelAfronden.add(new JLabel("Selecteer onderdeelNr en Vul gebruikte aantallen in"));



panelAfronden2 = new JPanel();
panelAfronden2.setLayout(new FlowLayout());

panelAfronden2.add(onderdelenGebruikt.get(0));
panelAfronden2.add(aantallenGebruikt.get(0));

JScrollPane sPane = new JScrollPane(panelAfronden2);
sPane.setPreferredSize(new Dimension(220, 230));

panelAfronden.add(sPane);

panelAfronden.add(new JLabel("Opmerkingen"));
opmerkingenAfronden = new JTextArea(5, 20);
panelAfronden.add(opmerkingenAfronden);

rondAf = new JButton("Rond Werkzaamheid Af");
rondAf.addActionListener(MyFrame.this);
panelAfronden.add(rondAf);

annuleer = new JButton("Annuleer");
annuleer.addActionListener(MyFrame.this);
panelAfronden.add(annuleer);

I have this in the ActionListener

    if( eventSource == onderdelenGebruikt){
        System.out.println("test");
    }

I know how to add the combobox and textfield to the panel but At the moment it doesn't even print out the test to the console

War es hilfreich?

Lösung

Your Question:

I know how to add the combobox and textfield to the panel but At the moment it doesn't even print out the test to the console.

Answer:

Store the reference of JcomboBox some where and then check the source in ActionListener.

do in this way:

    final JComboBox comboBox = new JComboBox(onderdelenBox());
    onderdelenGebruikt.add(comboBox);

    comboBox.addActionListener(MyFrame.this);

Your ActionListener will look like this.

if( eventSource == comboBox ){
    System.out.println("test");
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top