Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

public class Main {

    public static void main(String args[]) {
        String ITEMS[] = { "Black", "Blue", "Green", "Orange", "Purple", "Red", "White" };
        JList jList = new JList(ITEMS);
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane scroll = new JScrollPane(jList);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        JCheckBox chkEnable = new JCheckBox("Enable", true);
        chkEnable.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                jList.setEnabled(chkEnable.isSelected());
            }
        });

        JFrame f = new JFrame("Colors");
        Container contentPane = f.getContentPane();
        contentPane.setLayout(new BorderLayout());
        contentPane.add(scroll, BorderLayout.CENTER);
        contentPane.add(chkEnable, BorderLayout.NORTH);

        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(180, 220);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}