1

enter image description here

I've five lists (enclosed in a jscrollpane) added to a jpanel in a group layout. The problem with the lists is that when a scrollbar appears automatically, the border on the bottom/top disappears (lists 2,3,4). How do I make sure that all lists look the same w.r.t border just like the first/last lists?

I've tried setting viewportborder using setViewPortBroder, but it doesn't change things much. 2,3,4 appear with light border while the rest of the lists have double borders.

EDIT

Adding code sample:

Each list you see is created using this code:

        MyJList jList = new MyJList(value);
        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jList.setModel(listModel);
        jList.setMinimumSize(new Dimension(135, 300));
        jList.setMaximumSize(new Dimension(135, 300));
        jList.clearSelection();
        jList.setSelectionBackground(Color.darkGray);
        jList.setSelectionForeground(Color.white);
        jList.setBorder(new LineBorder(Color.darkGray, 1));
        jList.setFixedCellHeight(30);
        jList.setFixedCellWidth(100);
        Font font = jList.getFont();
        jList.addListSelectionListener(new ListListener());
        return jList;

MyList is an extension of JList. It does nothing special, other than storing some domain related metadata. And, then lists are added to the middle panel like this:

private void layoutLists(JLabel[] labels, JList[] lists) {

        panel.removeAll();
        panel.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.NONE;

        JScrollPane[] jScrollPanes = new JScrollPane[lists.length];
        for (int index = 0; index < lists.length; index++) {
            jScrollPanes[index] = new JScrollPane(lists[index]);
            jScrollPanes[index].setBorder(BorderFactory.createEmptyBorder());
            jScrollPanes[index].setMinimumSize(new Dimension(135, 300));
            jScrollPanes[index].setMaximumSize(new Dimension(135, 300));
            jScrollPanes[index].setPreferredSize(new Dimension(135, 300));
        }

        for (int index = 0; index < labels.length; index++) {
            gbc.gridx = index;
            gbc.gridy = 0;
            gbc.insets = new Insets(8, 8, 8, 8);
            panel.add(labels[index], gbc);
            gbc.gridy = 1;
            if (index == labels.length - 1) {
                gbc.insets = new Insets(8, 8, 8, 13);
            }
            panel.add(jScrollPanes[index], gbc);
        }}
Jay
  • 2,394
  • 11
  • 54
  • 98
  • 2
    It looks like the GUI designer is getting in your way. As shown in this [answer to your previous question on the topic](http://stackoverflow.com/a/11964917/230513), please edit your question to include an [sscce](http://sscce.org/) that produces the illustrated effect and that does _not_ use a GUI designer. – trashgod Aug 17 '12 at 05:02
  • Silly question, are you using `javax.swing.JList` or `java.awt.List`? – MadProgrammer Aug 17 '12 at 05:56
  • guys, its not the GUI designer. It is the actual screenshot of the app, I cannot show you the contents in the lists/headers coz they are copyrighted stuff. I have removed title, and the text using paint. – Jay Aug 17 '12 at 06:40
  • 2
    @jay really need some code examples :( – MadProgrammer Aug 17 '12 at 06:44
  • 2
    unrlelated: don't use setXXSize _ever_ - for reasons see http://stackoverflow.com/a/7229519/203657 In your case, it has zero effect, because the fixed sizes are used (JList being a bit ... weird in calculating its prefScrollable) – kleopatra Aug 17 '12 at 08:02

1 Answers1

3

The explanation of the top/bottom part of the inner (JList) border not being visible is that ... they are not visible if the vertical scrollBar appears (the list is scrolled off)

If you insist, either:

  • switch the borders - empty on the list itself and lineBorder on the scrollPane or
  • set the viewportBorderProperty of the scrollPane to the lineBorder

Beware: it's not recommended to fiddle with the default LAF settings - where-to or not the components have a border should be left to the ui to guarantee consistent visuals across your application. Nor does it look exactly good to have the left border line beside (either outside or inside) the scrollPane's vertical scrollbar.

Edit

Just noticed that you already tried the second option (and are not satisfied with the result :-) But then: where do you want the vertical border line if the scrollBar is visible? Anyway, back to the beware: the outcome is highly LAF dependent ...

kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • I've tried setting viewportBorder property on scrollPane to a lineborder - it just gets uglier: The first and the last list, look like they have thick borders while the middle lists have a thinner version of the line border. – Jay Aug 17 '12 at 08:41
  • @kleopatra agree with L&F sensitive, excelent idea with Borders, there are three JComponents that implemets Borders, but most L&F accepting (nice output) custom CompoundBorder to JScrollPane, Matte(Etched) & Empty :-) – mKorbel Aug 17 '12 at 08:54
  • vertical border? No, I am referring to the horizontal ones (top and bottom - they get lost once scrollbar appears. – Jay Aug 17 '12 at 08:55
  • read again: they don't get lost, they are not visible ;-) If the horizontal part is all you are concerned about: the viewportBorder should be what you need. If not, show a SSCCE (as you have been repeatedly asked to do), so we can play with it – kleopatra Aug 17 '12 at 09:00
  • Ok, I tried removing all other borders (the one the jlist and scrollpane) and tried it with setViewportBorder - and it worked fine. (The border,however, is a little too thick for my taste, even its size set to 1). – Jay Aug 17 '12 at 10:05