[英]Add space between JList items
How can I add a space between JList
items? 如何在
JList
项之间添加空格? 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. 在此图像上白色是背景,黄色是
JList
的自定义DefaultListCellRenderer
,我希望每个JList
项目都有一个彼此JList
5个像素的空间。
http://img819.imageshack.us/img819/5772/spacemd.png 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. 这个依赖于默认的
FlowLayout
默认的五个像素间隙,但您也可以为面板添加边框。
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),
通常也会这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.