Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I suspect it's the layout-management of <code>textPane</code> that is the issue. From what I can see, the <code>listForSuggestions</code> should not occupy more space than it needs to display those items, if it's preferred size is respected.</p> <p>So the <code>JTextPane</code> is a <code>Container</code>, that is, you can add subcomponents to it. But how are those subcomponents layed out? That is up to the layout manager currently in use. If the layout manager respects the preferred dimension of the <code>listForSuggestios</code> I think you should be ok. Not sure though.</p> <p>From what I can see, you get the "null-layout" by just instantiating a <code>JTextPane</code>, which means that unless you set another layout manager explicitly, you would need to take care of placement / resizing of the subcomponents yourself.</p> <p>You could try to do something like</p> <pre><code>Dimension dim = listForSuggestions.getPreferredSize(); listForSuggestions.setBounds(xPos, yPos, dim.getWidth(), dim.getHeight()); </code></pre> <p>Here is a complete example</p> <pre><code>import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import javax.swing.*; public class FrameTest { public static void main(String[] args) { JFrame f = new JFrame("Frame Test"); ArrayList&lt;String&gt; str = new ArrayList&lt;String&gt;(); for (int i = 0; i &lt; 20; i++) str.add("number " + i); JTextPane tp = new JTextPane(); int visibleRowCount = str.size(); System.out.println("visibleRowCount " + visibleRowCount); JList listForSuggestion = new JList(str.toArray()); listForSuggestion.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); listForSuggestion.setSelectedIndex(0); listForSuggestion.setVisibleRowCount(5); System.out.println(listForSuggestion.getVisibleRowCount()); JScrollPane 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.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK)); listForSuggestion.addMouseListener(mouseListener); Dimension dim = listForSuggestion.getPreferredSize(); listForSuggestion.setBounds(20, 20, (int) dim.getWidth(), (int) dim.getHeight()); tp.add(listForSuggestion); f.add(tp); f.setSize(400, 400); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } } </code></pre> <p>I think the most elegant way of doing this is to roll your own layout-manager. (It's actually quite simple.) And then, instead of doing <code>textPane.add(list)</code>, you do <code>textPane.add(list, YourLayoutManager.POPUP_LIST)</code>. The layout-manager then remembers the fact that <code>list</code> was supposed to be layed out according to it's preferred size, and layes it out accordingly in its <code>layoutContainer</code>-method. (If you give the <code>YourLayoutManager</code> a reference to the <code>JTextPane</code> that it is attached to, you could probably even make it layout the <code>list</code> right beside the current caret location.)</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