1

This is an example of a JList. I know how to set a border to the entire JList or the panel containing the JList. My question is that how can we create line borders between the cells?`

enter image description here

For example in the given picture the color of the line border between the first cell and the second is grey, while there is a white colored line border between all the other cells.

Please don't say that this is a JComboBox or a JTree (because of the second element and its two children); even if it is not a JList, I want my JList to have similar LineBorders between the cells.


My web search led me to this interface. Its method getListCellRendererComponent takes arguments like E value, int index, boolean isSelected, boolean cellHasFocus, while I want the LineBorder to appear between all the cells no matter what index they have and whether they are selected or not etc.


EDIT:-

enter image description here

The border is thicker in some places and fine in others.

Solace
  • 8,612
  • 22
  • 95
  • 183
  • 1
    you limited possible suggestions or answers by `Please don't say that this is` ---> look into docs and to change statndard borders, you want/have to to override isSelected and hasFocus to, rest is described in refused tutorials JComboBox, JList, JTable, JTree, this concept is similair for all those JComponents – mKorbel May 06 '14 at 12:45
  • how can we create line borders between the cells == search for AbstractBorder – mKorbel May 06 '14 at 12:46

1 Answers1

3

You can achive that with help of ListCellRenderer. Here is simple example:

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;


public class TestFrame extends JFrame{

    public TestFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
        JList<String> list = new JList<>(new String[]{"1","2","3"});
        list.setCellRenderer(getRenderer());
        add(list);
    }

    private ListCellRenderer<? super String> getRenderer() {
        return new DefaultListCellRenderer(){
            @Override
            public Component getListCellRendererComponent(JList<?> list,
                    Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                JLabel listCellRendererComponent = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected,cellHasFocus);
                listCellRendererComponent.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0,Color.BLACK));
                return listCellRendererComponent;
            }
        };
    }

    public static void main(String... strings) {
        new TestFrame();
    }
}

Looks like:

enter image description here

Read more in tutorial.

EDIT: Just change LineBorder to MatteBorder(have changed code and image).

alex2410
  • 10,904
  • 3
  • 25
  • 41
  • Thank you.. actually this is very close to what I wanted. But what I want exactly is a line border just under the cell. Because as you can see in the image, there is a border around the entire list, and there is line border between the cells. I have a border around a list, and then the border around the cells combines with that around the entire list and makes it thick and ugly. I am attaching a picture of my JList. – Solace May 07 '14 at 10:05