Java ActionListener handle action event on JComboBox

Description

Java ActionListener handle action event on JComboBox

import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class Demo extends JPanel {
   JLabel jlab = new JLabel();

   public Demo() {

      JComboBox<String> jcb;

      String options[] = { "CSS", "HTML", "Java", "Javascript" };

      setLayout(new FlowLayout());

      jcb = new JComboBox<String>(options);
      add(jcb);//w  w w.ja  va2s .  co  m

      jcb.addActionListener(e -> {
         String s = (String) jcb.getSelectedItem();
         jlab.setText(s);
      });
      // Create a label and add it to the content pane.
      jlab.setText("not selection");
      add(jlab);
   }
}

public class Main {
   public static void main(String[] args) {
      JFrame application = new JFrame();
      application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      application.add(new Demo());
      application.setSize(250, 250);
      application.setVisible(true);
   }
}



PreviousNext

Related