简体   繁体   中英

Add space between JList items

How can I add a space between JList items? is there a way doing it?

On this Image white is the background and yellow is the customize DefaultListCellRenderer of JList and I want each of the JList Item to have a space like 5 pixels to each other.

http://img819.imageshack.us/img819/5772/spacemd.png

dlm = new DefaultListModel<String>();
jlist = new JList<String>(dlm);
jlist.setFont(new Font("Calibri",Font.BOLD,16));
jlist.setCellRenderer(new Renderer());
jlist.setFixedCellWidth(100);
jlist.setBorder(new EmptyBorder(10,10, 10, 10));

I don't see your renderer, but you can add the component to a panel. This one relies on the default five pixel gap from the default FlowLayout , but you could add a border to the panel, too.

list.setCellRenderer(new DefaultListCellRenderer(){
    private static final int N = 2;

    @Override
    public Component getListCellRendererComponent(JList list, Object value,
            int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(
            list, value, index, isSelected, cellHasFocus);
        c.setBackground(Color.yellow);
        JPanel p = new JPanel();
        p.add(c);
        return p;
    }
});

尝试使用JList.setFixedCellHeight(someValue),通常也会这样做。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM