GettingSettingSelectedItem.java Source code

Java tutorial

Introduction

Here is the source code for GettingSettingSelectedItem.java

Source

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;

public class GettingSettingSelectedItem {

    public static void main(String[] a) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton jButton1 = new JButton("Button");

        String[] mystring = { "Java", "JBuilder", "JFC", "Swing" };
        final JList jList1 = new JList(mystring);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Object contents = jList1.getSelectedValue();
                System.out.println(contents);
            }
        });

        frame.add(jList1, "Center");
        frame.add(jButton1, "South");

        frame.setSize(300, 200);
        frame.setVisible(true);
    }

}