title: How do i add an action listener to a JList in a proper way?
body: <p>I'm trying to add an actionListener to a JList, so whenever a user click a value in the JList , it will just println the value.</p>
  
  <p>Here's the code</p>
  
  <pre><code>public class FontProgram {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          JFrame mainFrame = new JFrame("Fonts Frame");
          JPanel panel = new JPanel(new BorderLayout());
  
          GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
          String[] fontNames = e.getAvailableFontFamilyNames();
  
  
  
  
          JComboBox fontbox = new JComboBox(fontNames);
  
          JList fontList = new JList(fontNames);
          JButton button = new JButton("Submit");
  
          JScrollPane scrollPane = new JScrollPane();
  
          scrollPane.setViewportView(fontList);
  
          fontList.addListSelectionListener(new SharedListSelectionHandler());
          panel.add(fontbox, BorderLayout.NORTH);
          panel.add(scrollPane, BorderLayout.CENTER);
          panel.add(button, BorderLayout.SOUTH);
  
          mainFrame.add(panel);
          mainFrame.setVisible(true);
          mainFrame.setSize(250, 250);
          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  
      }
  
  }
  </code></pre>
  
  <p>Here's the result of the codes</p>
  
  <p><img src="https://i.stack.imgur.com/J1aPd.png" alt="enter image description here"></p>
  
  <p>So how do I add an action Listener to the JList?</p>