Note that there are some explanatory texts on larger screens.

plurals
  1. POSet the amount of rows JList show (Java)
    text
    copied!<p>Problem: I have a method that creates a list from the parsed ArrayList. I manage to show the list in the GUI, without scrollbar. However, I am having problem setting it to show only the size of ArrayList. Meaning, say if the size is 6, there should only be 6 rows in the shown List. Below is the code that I am using. I tried setting the <code>visibleRowCount</code> as below but it does not work. I tried printing out the result and it shows that the change is made.</p> <pre><code>private void createSuggestionList(ArrayList&lt;String&gt; str) { int visibleRowCount = str.size(); System.out.println("visibleRowCount " + visibleRowCount); listForSuggestion = new JList(str.toArray()); listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listForSuggestion.setSelectedIndex(0); listForSuggestion.setVisibleRowCount(visibleRowCount); System.out.println(listForSuggestion.getVisibleRowCount()); listScrollPane = new JScrollPane(listForSuggestion); MouseListener mouseListener = new MouseAdapter() { @Override public void mouseClicked(MouseEvent mouseEvent) { JList theList = (JList) mouseEvent.getSource(); if (mouseEvent.getClickCount() == 2) { int index = theList.locationToIndex(mouseEvent.getPoint()); if (index &gt;= 0) { Object o = theList.getModel().getElementAt(index); System.out.println("Double-clicked on: " + o.toString()); } } } }; listForSuggestion.addMouseListener(mouseListener); textPane.add(listScrollPane); repaint(); } </code></pre> <p>To summarize: I want the JList to show <strong>as many rows as the size of the parsed ArrayList</strong>, <strong>without a scrollbar</strong>.</p> <p>Here is the picture of the problem:</p> <p><a href="https://i.stack.imgur.com/Rf04g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rf04g.png" alt="jlist 1"></a></p> <p>Here's the link to the other 2 as the picture resolution is quite big I'm scared it will distort the view:</p> <p><a href="https://i.stack.imgur.com/Rf04g.png" rel="nofollow noreferrer">JList 1</a> &amp; <a href="https://i.stack.imgur.com/C0I2o.png" rel="nofollow noreferrer">JList 2</a></p> <p>The JList 1 and 2 pictures shows it clearly. The JList displays empty rows, which I do not want it to happen.</p> <p>Any ideas? Please help. Thanks. Please let me know if a picture of the problem is needed in case I did not phrase my question correctly.</p> <p>--</p> <p>Edit: </p> <pre><code>JScrollPane scrollPane = new JScrollPane(textPane); scrollPane.setPreferredSize(new Dimension(200, 200)); //Create the text area for the status log and configure it. changeLog = new JTextArea(5, 30); changeLog.setEditable(false); JScrollPane scrollPaneForLog = new JScrollPane(changeLog); //Create a split pane for the change log and the text area. JSplitPane splitPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT, scrollPane, scrollPaneForLog); splitPane.setOneTouchExpandable(true); //Create the status area. JPanel statusPane = new JPanel(new GridLayout(1, 1)); CaretListenerLabel caretListenerLabel = new CaretListenerLabel("Caret Status"); statusPane.add(caretListenerLabel); //Add the components. getContentPane().add(splitPane, BorderLayout.CENTER); getContentPane().add(statusPane, BorderLayout.PAGE_END); </code></pre> <p>How the textPane is included into the container, if that helps</p> <p>Another edit:</p> <pre><code>public void showSuggestionList(JScrollPane pane, Rectangle caretCoords) { pane.setVisible(false); pane.setBounds(caretCoords.x - 5, caretCoords.y + 25, 400, 250); pane.setVisible(true); repaint(); } </code></pre> <p>showSuggestionList() is being called my CaretListener, to show the JScrollPane when the caret moves.</p>
 

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