Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to @Guillaume Polet's good <a href="https://stackoverflow.com/a/14801818/230513">advice</a>, <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JList.html#getVisibleRowCount%28%29" rel="nofollow noreferrer"><code>setVisibleRowCount()</code></a> may be useful to you going forward.</p> <p><img src="https://i.stack.imgur.com/o0U6K.png" alt="image"></p> <pre><code>import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JScrollPane; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; /** @see https://stackoverflow.com/a/14801908/230513 */ public class SongLib { String songs[] = {"Song1", "Song2", "Song3", "Song4", "Song5"}; JList list = new JList(songs); public SongLib() { JFrame songLibrary = new JFrame("Song Library"); songLibrary.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); list.setVisibleRowCount(4); list.addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent evt) { int i = list.getSelectedIndex(); if (i != -1) { System.out.println("Selected: " + songs[i]); } else { System.out.println("Choose a song"); } } }); JScrollPane JSPane = new JScrollPane(list); songLibrary.add(JSPane); songLibrary.pack(); songLibrary.setLocationRelativeTo(null); songLibrary.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new SongLib(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload