本文介绍了带有行分隔的 Java 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我如何让我的 Java SWT 列表看起来像这里 http://flavio.tordini.org/minitunes 我的意思是每个元素都用一条线相互隔开.或者我有任何其他解决方案可以在 minitunes 中创建一个列表,这样我就可以添加数字.. 等.

Hey, how do i make my java SWT list look like here http://flavio.tordini.org/minitunes I mean that each element is separated by a line from eachother. Or i there any other solution to have a list like in the minitunes, so i can add number.. etc.

推荐答案

您应该使用 ListCellRenderer 在特定位置添加分隔符...

You should use ListCellRenderer to add the separators at specific positions...


 jList1 = new javax.swing.JList();
//String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
jList1.setModel(new javax.swing.AbstractListModel() {
    String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
    Vector v = makeVectorData(strings);
    public int getSize() { return v.size(); }
    public Object getElementAt(int i) { return v.get(i); }
});
jList1.setCellRenderer(new JlistRenderer());
jList1.addFocusListener(new JListFocusListener(jList1));

公共类 JlistRenderer 扩展 JLabel 实现 ListCellRenderer {JSeparator 分隔符;final String SEPARATOR = "SEPARATOR";公共 JlistRenderer() {设置不透明(真);setBorder(new EmptyBorder(1, 1, 1, 1));分隔符 = 新的 JSeparator(JSeparator.HORIZONTAL);}公共组件 getListCellRendererComponent(JList 列表,对象值,int index, boolean isSelected, boolean cellHasFocus) {字符串 str = (value == null) ?"" : value.toString();如果 (SEPARATOR.equals(str)) {返回分隔符;}如果(被选中){setBackground(list.getSelectionBackground());setForeground(list.getSelectionForeground());} 别的 {setBackground(list.getBackground());setForeground(list.getForeground());}setFont(list.getFont());设置文本(字符串);返回这个;}}

public class JlistRenderer extends JLabel implements ListCellRenderer { JSeparator separator; final String SEPARATOR = "SEPARATOR"; public JlistRenderer() { setOpaque(true); setBorder(new EmptyBorder(1, 1, 1, 1)); separator = new JSeparator(JSeparator.HORIZONTAL); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { String str = (value == null) ? "" : value.toString(); if (SEPARATOR.equals(str)) { return separator; } if (isSelected) { setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); setText(str); return this; }}

公共类 JListFocusListener 实现 FocusListener {JList列表;对象 currentItem;final String SEPARATOR = "SEPARATOR";JListFocusListener(JList 列表) {this.list=列表;list.setSelectedIndex(0);currentItem = list.getSelectedValue();}公共无效focusGained(FocusEvent e){String tempItem = (String) list.getSelectedValue();如果 (SEPARATOR.equals(tempItem)) {list.setSelectedValue(currentItem,true);} 别的 {当前项目 = 临时项目;}}}

public class JListFocusListener implements FocusListener { JList list; Object currentItem; final String SEPARATOR = "SEPARATOR"; JListFocusListener(JList list) { this.list= list; list.setSelectedIndex(0); currentItem = list.getSelectedValue(); } public void focusGained(FocusEvent e) { String tempItem = (String) list.getSelectedValue(); if (SEPARATOR.equals(tempItem)) { list.setSelectedValue(currentItem,true); } else { currentItem = tempItem; } }}

希望以上代码有帮助...

Hope the above code helps...

这篇关于带有行分隔的 Java 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 20:45