2

I want to specify fixed width of my JList

I followed example provided by Gilbert Le Blanc 22k

        getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
        Panel panel = new JPanel();
        panel.setBackground(Color.WHITE);
        getContentPane().add(panel);

        String [] queries = {"a", "b", "a", "b" , "a", "b", "a", "b"};
        panel.setLayout(new BorderLayout());
        JList list = new JList(queries);
        list.setMaximumSize(new Dimension(200, 200));  // this line does not do the job
        list.setMinimumSize (new Dimension (200,200));
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setVisibleRowCount(5);
        JScrollPane scrollPane_1 = new JScrollPane(list);
        panel.add(scrollPane_1 , BorderLayout.CENTER);

But this does not force the size to be 200 200. I need the width of the JScrollPane to be 200

Community
  • 1
  • 1
Donotello
  • 157
  • 2
  • 2
  • 14
  • 1
    please whats I want to specify size of my JList, its about size of JList or number of visisble Items or ??? ..... – mKorbel Nov 06 '13 at 18:50
  • it is about the width of the JScrollPane – Donotello Nov 06 '13 at 18:58
  • 1
    _I need the width of the JScrollPane to be 200_ - why? Fixed pixel sizes are **always** (up to third approximation or so ;-) a sign of something wrong with your approach/requirement. Anyway, [don't use setXXSize, ever](http://stackoverflow.com/a/7229519/203657) http://stackoverflow.com/a/7229519/203657 – kleopatra Nov 07 '13 at 00:07
  • 1
    You can provide a prototype value which the `JList` will use to determine it's preferred width and row height without needing to process the entire list model – MadProgrammer Nov 07 '13 at 06:14

6 Answers6

7

No, don't set the size of the JList which is already added to JScrollpane. Because JList has an implemented function getPreferredScrollableViewportSize() which computes preferable viewport size by computing cell width and cell height it contains. Let the JScrollPane to handle that. If require, you try adding JScrollPane's dimension. However, to control the item display number and orientation, you can use:

  • setLayoutOrientation which lets the list display its data in multiple columns. The value:

    1. JList.HORIZONTAL_WRAP: specifies that the list should display its items from left to right before wrapping to a new row.
    2. JList.VERTICAL_WRAP: which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column.
  • setVisibleRowCount(-1) makes the list display the maximum number of items possible in the available space onscreen. Another common use of setVisibleRowCount is to specify to the lists's scroll pane how many rows the list prefers to display.

Check out: How to Use List for Demo and examples.

Sage
  • 15,290
  • 3
  • 33
  • 38
  • `setVisibleRowCount(-1)` makes my JScrollpane and JList disappear, as the layoutmanager doesn't know how much space it prefers or needs at a minimum, a work around is to set setVisibleRowCount(size) to a size higher than fits on the screen, that way your layout will allocate as much space as possible/available to the Scrollpane and JList. – HopefullyHelpful Jan 29 '22 at 14:02
5

The solution is to resize the JScrollPane. If I remember correctly, JList fill up the JScrollPane.

JScrollPane scrollPane_1 = new JScrollPane(list);
scrollPane_1.setMaximumSize(new Dimension(200, 200));
scrollPane_1.setMinimumSize (new Dimension (200,200));
panel.add(scrollPane_1 , BorderLayout.CENTER);
Coneone
  • 292
  • 1
  • 8
  • Yeah the method is `yourScrollPane.getViewport().setViewSize(yourDimension);`. – Radiodef Nov 06 '13 at 18:54
  • No, you are encouraging the OP to go into wrong direction. never to use `NullLayout` , never to `setLocation` and `bounds` – Sage Nov 06 '13 at 23:07
5

it is about the width of the JScrollPane

Most layout managers use the preferred size to determine the optimal size and then use min/max to handle resizing if the frame is ever resized.

I would try something like:

JList list = new JList(queries);
list.setVisibleRowCount(5);
JScrollPane scrollPane_1 = new JScrollPane(list);
Dimension d = list.getPreferredSize();
d.width = 200;
scrollPane_1.setPreferredSize(d);

Although it is rarely a good idea to manually set a preferred size of a component. Let the LAF determine the appropriate preferred size. For example I would never hard code height, because then your list might display 4 1/2 rows which is unprofessional looking.

The width I can understand a little bit if you only ever have a single character string it may look too narrow. But in this case a better solution would be to create a custom renderer and use an EmptyBorder that allocates extra space on the left/right and then let the LAF size the JList.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • even with a _little_ understanding and even if _only_ setting the width: hard-coding xxSizes will break visuals at the slightest change and halfway stable application code would need to re-set dynamically on whatever property (which can't be known at coding time) that might effect the value. The only way out is not using those methods at all. – kleopatra Nov 07 '13 at 08:39
3

Normally your list's size is the number of elements you put into it, so it won't get fixed this way. You can't create a list of N empty spaces, because it would not be a list then (what would be its length?) - an array would suit here much better. If you want to create a fixed-size list refer to:

fixed size list in Java

You may also create a class which allows you to add a specified number of elements to your list and then forbids further addition (when your list's size gets to desired N).

Community
  • 1
  • 1
3yakuya
  • 2,622
  • 4
  • 25
  • 40
3

You can simply use setFixedCellWidth of your JList.

Pooya
  • 531
  • 1
  • 5
  • 10
1
    // Loop through the arrayList of doubles and convert to String
    // with dollar signs, 2 decimal places, and extra spacing.
    for(int i = 0; i < bookPrices.size(); i++)
    {
        bookPricesOnlyStrings.add(String.format("$%,.2f%-35s", bookPrices.get(i), ""));
        System.out.println(bookPricesOnlyStrings.get(i));
    }

I had the same problem. My data was only 5 characters and looked off compared to my other window. So I used String.format to add spaces to the end of it. This widened my JList. I also was able to add dollar signs and format the decimal places.

enter image description here

Christopher Adams
  • 1,240
  • 10
  • 14