-2

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?

Warz
  • 7,386
  • 14
  • 68
  • 120
  • 3
    This looks like a good candidate for your creating and posting an [SSCCE](http://SSCCE.org). – Hovercraft Full Of Eels Aug 07 '11 at 18:03
  • 1
    -1, I see you still haven't learned much from your last posting (http://stackoverflow.com/questions/6969358/jlist-with-image-and-text-where-text-is-coming-from-an-arrayliststring). Do you expect us to keep coming back to check if you have posted a SSCCE? – camickr Aug 07 '11 at 18:36
  • @camickr, the posting i made was asking a general 'how to' programming question. I asked if there is a way to set a JList layout such that the items in the list fill the width of the list before starting a new row. – Warz Aug 07 '11 at 20:10
  • @Warz: no, you're wrong; it's a what's wrong with my code question. Yes, you need an SSCCE. – Hovercraft Full Of Eels Aug 07 '11 at 20:15
  • @Hovercraft, i ran your sample SSCCE as an example, its getting me started with an SSCCE of mine. i will post one similar with image icons instead of just text. Thanks – Warz Aug 07 '11 at 20:19
  • 1
    @Warz, no, you wrote some code and it didn't work the way you expected. Therefore you post your SSCCE with the question. Its simple. – camickr Aug 07 '11 at 22:16

1 Answers1

3

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @Warz: I just read camickr's comment. Why haven't you created your own SSCCE yet? What are you waiting for? – Hovercraft Full Of Eels Aug 07 '11 at 18:37
  • Got it to work as i was building an SSCCE. Thanks for getting me started. – Warz Aug 07 '11 at 22:26
  • You're welcome. That's one of the benefits of creating these guys as often you find the problem and its solution in the process. Congrats! – Hovercraft Full Of Eels Aug 07 '11 at 22:28
  • 2
    @Warz, And how many times have I told you that is the point of creating a SSCCE so you can actually solve your own problems? It's amazing what you can do when you actually make an effort. – camickr Aug 07 '11 at 22:50