Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Timer;

public class Main {
    public static void main(String[] args) {
        int TIMER_DELAY = 2000;
        String[] data = { "A", "B", "C", "D" };

        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(data);

        JComboBox<String> combobox = new JComboBox<>(model);
        JList<String> jlist = new JList<>(model);

        new Timer(TIMER_DELAY, new ActionListener() {
            private int count = 0;

            public void actionPerformed(ActionEvent e) {
                model.addElement("count: " + count);
                count++;
            }
        }).start();

        JPanel comboPanel = new JPanel();
        comboPanel.add(combobox);

        JPanel listPanel = new JPanel();
        listPanel.add(new JScrollPane(jlist));

        JPanel panel = new JPanel(new GridLayout(1, 0));
        panel.add(comboPanel);
        panel.add(listPanel);
        panel.setPreferredSize(new Dimension(400, 200));

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        f.pack();
        f.setVisible(true);
    }
}