Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class Main {
    public static void main(String[] arg) {
        DefaultListModel<String> listModel = new DefaultListModel<String>();
        for (int i = 0; i < 10; i++) {
            listModel.addElement("Item " + (i + 1));
        }

        JList<String> list = new JList<String>(listModel);
        list.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                if (SwingUtilities.isRightMouseButton(me)) {
                    list.clearSelection();
                }
            }
        });

        JScrollPane listScrollPane = new JScrollPane(list);
        JFrame f = new JFrame();
        f.getContentPane().add(listScrollPane);

        f.setSize(500, 400);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}