开发者

JList items with horizontal wrap not extending all the way through

开发者 https://www.devze.com 2023-03-26 17:58 出处:网络
I have a Jlist like the one below. I am using the JList.HorizontalWrap to achieve this, but for some reason, after the 4th item in the list, it starts a new row.

I have a Jlist like the one below. I am using the JList.HorizontalWrap to achieve this, but for some reason, after the 4th item in the list, it starts a new row.

Here is the configurations I used to get the list looking this.

        sList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        sList.setVisibleRowCount(-1);
        sList.setLayoutOrientation(JList.开发者_运维知识库HORIZONTAL_WRAP);

Is there any way I can set the List row count to be the width of the Jlist so that all items in the list will be set across before starting a new row?


As I noted in my comment, your problem lends itself well to solving through creation of an SSCCE. In fact, I did one myself using your code snippet and some of my code:

import java.awt.Dimension;
import javax.swing.*;

public class Foo001 {

   private static void createAndShowUI() {
      DefaultListModel model = new DefaultListModel();
      JList sList = new JList(model);
      for (int i = 0; i < 100; i++) {
         model.addElement("String " + i);
      }

      sList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
      sList.setVisibleRowCount(-1);
      sList.setLayoutOrientation(JList.HORIZONTAL_WRAP);

      JFrame frame = new JFrame("Foo001");
      frame.getContentPane().add(new JScrollPane(sList));
      frame.getContentPane().setPreferredSize(new Dimension(400, 300));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Since I cannot reproduce your problem with my code, I must conclude that your problem lies elsewhere in code that you've not shown us. Again, if you can create and post your SSCCE, we will likely be able to help answer your question, but until then, I'm not sure if we can even guess what the problem is.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号