我想将一些JSeparator添加到JList中。我正在使用DefaultListModel,当我尝试:
((DefaultListModel)myListModel).addElement(new JSeparator());
我执行时将其写入JList而不是分隔符:
javax.swing.JSeparator中[,0,0,0x0,无效,alignmentX = 0.0,alignmentY = 0.0,边界= javax.swing.plaf.synth.SynthBorder @ 962d513,标志= 0,MAXIMUMSIZE =,=的minimumSize,首选大小=,取向=水平]
答案 0 :(得分:7)
渲染器中的一点点复杂但可能,例如JComboBox
的示例,但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;
}
}
}
}
答案 1 :(得分:0)
您可以将DefaultListModel包装到JList中,并将自定义单元格渲染器添加到JList。
class MyListCellRenderer implements ListCellRenderer{
final JLabel jlblCell = new JLabel(" ", JLabel.LEFT);
Border lineBorder = BorderFactory.createLineBorder(Color.GRAY, 1);
public Component getListCellRendererComponent(JList jList, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
jlblCell.setOpaque(true);
if (isSelected) {
jlblCell.setForeground(jList.getSelectionForeground());
jlblCell.setBackground(jList.getSelectionBackground());
jlblCell.setBorder(new LineBorder(Color.BLUE));
} else {
jlblCell.setForeground(jList.getForeground());
jlblCell.setBackground(jList.getBackground());
}
jlblCell.setText(value.toString());
jlblCell.setBorder(lineBorder);
return jlblCell;
}
}
def renderer = new MyListCellRenderer();
def listModel = new DefaultListModel();
def list = new JList(listModel);
.
.
.
def dialog = new JDialog(ui.frame);
dialog.add(new JScrollPane(list));