问题描述
我想将一些JSeparator添加到JList中。我正在使用DefaultListModel,当我尝试:
I want to add some JSeparators to a JList. I am using a DefaultListModel, and when I try:
((DefaultListModel)myListModel).addElement(new JSeparator());
我执行时将其写入JList而不是分隔符:
I'm getting this written into the JList instead of the separator when I execute:
javax.swing.JSeparator [,0,0,0x0,无效,alignmentX = 0.0,alignmentY = 0.0,border = javax.swing.plaf.synth.SynthBorder @ 962d513,flags = 0,maximumSize =,minimumSize =,preferredSize =,orientation = HORIZONTAL]
javax.swing.JSeparator[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@962d513,flags=0,maximumSize=,minimumSize=,preferredSize=,orientation=HORIZONTAL]
推荐答案
渲染器中有点复杂但可能,因为 JComboBox
的示例,但 ListCellRenderer
little bit complicated in the renderer but possible, since example for JComboBox
but with ListCellRenderer
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class BlockComboBoxExample extends JFrame {
private static final long serialVersionUID = 1L;
final String SEPARATOR = "SEPARATOR";
public BlockComboBoxExample() {
super("Block ComboBox Example");
String[][] str = {{"A", "B", "C"}, {"1", "2", "3"}, {"abc", "def", "ghi"}};
JComboBox combo = new JComboBox(makeVectorData(str));
combo.setRenderer(new ComboBoxRenderer());
combo.addActionListener(new BlockComboListener(combo));
combo.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXX");
setLayout(new FlowLayout());
add(combo);
pack();
setVisible(true);
}
private Vector<String> makeVectorData(String[][] str) {
boolean needSeparator = false;
Vector<String> data = new Vector<String>();
for (int i = 0; i < str.length; i++) {
if (needSeparator) {
data.addElement(SEPARATOR);
}
for (int j = 0; j < str[i].length; j++) {
data.addElement(str[i][j]);
needSeparator = true;
}
}
return data;
}
public static void main(String args[]) {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
//UIManager.getLookAndFeelDefaults().put("Panel.background", Color.white);
//UIManager.getLookAndFeelDefaults().put("Button.contentMargins", new InsetsUIResource(0,0,0,0));
}
}
} catch (Exception e) {
e.printStackTrace();
}
BlockComboBoxExample frame = new BlockComboBoxExample();
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private static final long serialVersionUID = 1L;
private JSeparator separator;
public ComboBoxRenderer() {
setOpaque(true);
setBorder(new EmptyBorder(1, 1, 1, 1));
separator = new JSeparator(JSeparator.HORIZONTAL);
}
@Override
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;
}
}
private class BlockComboListener implements ActionListener {
private JComboBox combo;
private Object currentItem;
BlockComboListener(JComboBox combo) {
this.combo = combo;
combo.setSelectedIndex(0);
currentItem = combo.getSelectedItem();
}
@Override
public void actionPerformed(ActionEvent e) {
String tempItem = (String) combo.getSelectedItem();
if (SEPARATOR.equals(tempItem)) {
combo.setSelectedItem(currentItem);
} else {
currentItem = tempItem;
}
}
}
}
这篇关于将JSeparator添加到DefaultListModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!