import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class List { public static void main(String[] args) { JFrame frame = new JFrame(); String[] trees = { "Maple", "Larch", "Spruce", "Balsam" }; JList treeList = new JList(trees); JPanel panel = new JPanel(); JLabel label = new JLabel("Select a Tree"); treeList.add(panel); JButton button = new JButton("Show Me"); button.setBounds(10, 150, 230, 30); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String data = ""; if (treeList.getSelectedIndex() != -1) { data = "Trees: " + treeList.getSelectedValue(); button.setText(data); } } }); frame.add(button); frame.add(treeList); frame.setSize(250, 250); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); } }