Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

public class Main extends JFrame {

    JComboBox jComboBox1;
    boolean boloComboBox = false;

    public Main() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jComboBox1 = new JComboBox();
        jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "1", "2" }));
        jComboBox1.addItemListener(evt -> {
            if (jComboBox1.isPopupVisible()) {
                jComboBox1.setPopupVisible(false);
                fireTask(evt);
            }
        });
        add(jComboBox1);
        pack();
    }

    private void fireTask(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == 2) {
            int i = JOptionPane.showConfirmDialog(jComboBox1, "Message Text", "Title",
                    JOptionPane.OK_CANCEL_OPTION);
            System.out.println("Result:" + i);
        }
    }

    public static void main(String args[]) {
        new Main().setVisible(true);
    }
}