简体   繁体   English

JList:如何在“单元格”之间获取LineBorder?

[英]JList: How to get a LineBorder “between” cells?

This is an example of a JList. 这是JList的一个例子。 I know how to set a border to the entire JList or the panel containing the JList. 我知道如何设置整个JList或包含JList的面板的边框。 My question is that how can we create line borders between the cells?` 我的问题是我们如何在细胞之间创建线条边界?`

在此输入图像描述

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 JComboBo x or a JTree (because of the second element and its two children); 请不要说这是JComboBo x或JTree (因为第二个元素及其两个子元素); even if it is not a JLis t, I want my JList to have similar LineBorders between the cells . 即使它不是JLis ,我希望我的JListcells之间有类似的LineBorders


My web search led me to this interface . 我的网络搜索引导我进入这个 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. 它的方法getListCellRendererComponent接受E value, int index, boolean isSelected, boolean cellHasFocus ,而我希望LineBorder出现在所有单元格之间,无论它们具有什么索引以及它们是否被选中等。


EDIT:- 编辑:-

在此输入图像描述

The border is thicker in some places and fine in others. 边境在某些地方较厚,在其他地方较好。

You can achive that with help of ListCellRenderer . 您可以在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: 看起来像:

在此输入图像描述

Read more in tutorial . 阅读更多教程

EDIT: Just change LineBorder to MatteBorder (have changed code and image). 编辑:只需将LineBorder更改为MatteBorder (已更改代码和图像)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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