为什么我选择的项目背景没有改变?我当然知道我按下列表项,因为我的 System.out.println 说我的当前选择并显示所选项目的 id。没有错误没有什么只是不工作。为什么 ?
Object[] tablen = sqltable.toArray();
JList list;
list = new JList(tablen);
list.addListSelectionListener(new ListSelectionListener(){
@Override
public void valueChanged(ListSelectionEvent e) {
int idx = list.getSelectedIndex();
setOpaque(true);
if (idx != -1){
//list.setSelectionBackground(Color.lightGray);
// list.setSelectionForeground(Color.lightGray);
setForeground(Color.red);
setBackground(Color.BLUE);
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
System.out.println("Current selection: " + tablen[idx]);
}else{
setForeground(Color.red);
setBackground(Color.BLUE);
setBackground(list.getBackground());
setForeground(list.getForeground());
System.out.println("Please choose a language.");
}
}
});
list.setCellRenderer(new ListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
String[] val = (String[]) value;
return new JLabel(val[0]);
}
});