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 java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class Main {
    public static void main(String[] args) {

        String[] listValues = { "Click", "To", "Add", "New", "Values" };
        DefaultListModel<String> model = new DefaultListModel<>();
        for (String s : listValues) {
            model.addElement(s);
        }
        JList<String> list = new JList<>(model);
        MouseListener addListener = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                model.addElement("aa");
            }
        };
        list.addMouseListener(addListener);
        JOptionPane.showMessageDialog(null, new JScrollPane(list));
    }
}