Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Main {

    public static void main(String[] args) {
        String[] items = { "item1", "item2", "item1" };
        JList<String> list = new JList<>(items);
        JTextField output = new JTextField(15);
        JPanel gui = new JPanel();
        gui.add(list);
        gui.add(output);
        list.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent lse) {
                int index = list.getSelectedIndex();
                String outputText = "Index: " + index + "  Value: " + items[index];
                output.setText(outputText);
            }
        });
        JOptionPane.showMessageDialog(null, gui);
    }
}