Я создал индивидуальный комбинированный список, который расширяет JCombobox.Имеет флажки во всплывающем списке.Когда я пытаюсь установить / установить флажок внутри всплывающего окна, цвет выделения отсутствует.
Я попытался установить фоновый флажок внутри собственного средства визуализации, но он не работает.Вот мой код, если какие-либо идеи, пожалуйста, помогите.Заранее спасибо.
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class MyCombo extends JComboBox implements ActionListener
{
/**
* Serial version uid
*/
private static final long serialVersionUID = 1L;
/**
* Renderer for CheckBoxListComboBox
*/
protected CheckComboRenderer renderer;
/**
* Editor for CheckBoxListComboBox
*/
protected ComboEditor editor;
/**
* String Buffer that holds currently selected values
*/
protected StringBuffer selectedValues;
/**
* Creates a <code>CheckBoxListComboBox</code> that contains the elements
* in the specified arrayList. By default no item becomes selected.
* @param arrayList ArrayList holds data to display
*/
public MyCombo()
{
renderer = new CheckComboRenderer();
editor = new ComboEditor();
selectedValues = new StringBuffer();
setEditable(true);
setModel(makeModel());
addActionListener(this);
setRenderer(renderer);
setEditor(editor);
}
/* (non-Javadoc)
* @see javax.swing.JComboBox#setPopupVisible(boolean)
*/
public void setPopupVisible(boolean v)
{
//Dont hide popup on click
//super.setPopupVisible(v);
}
/* (non-Javadoc)
* @see javax.swing.JComboBox#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e)
{
JComboBox cb = (JComboBox)e.getSource();
CheckComboStore store = (CheckComboStore)cb.getSelectedItem();
CheckComboRenderer ccr = (CheckComboRenderer)cb.getRenderer();
ccr.checkBox.setSelected((store.state = !store.state));
JTextField tf = (JTextField) editor.getEditorComponent();
StringBuffer buffer = new StringBuffer("");
DefaultComboBoxModel mdl = (DefaultComboBoxModel) cb.getModel();
for (int i = 0; i < mdl.getSize(); i++)
{
store = (CheckComboStore)mdl.getElementAt(i);
if (store.getState().booleanValue())
{
if (buffer.length() == 0)
buffer.append(store.getId());
else
buffer.append("; " + store.getId());
}
}
tf.setText(buffer.toString());
}
private DefaultComboBoxModel makeModel()
{
DefaultComboBoxModel model = new DefaultComboBoxModel();
model.addElement(new CheckComboStore("Item1", false));
model.addElement(new CheckComboStore("Item2", false));
model.addElement(new CheckComboStore("Item3", false));
model.addElement(new CheckComboStore("Item4", false));
model.addElement(new CheckComboStore("Item5", false));
return model;
}
private JPanel getContent()
{
final JComboBox combo = new JComboBox()
{
public void setPopupVisible(boolean v)
{
//Dont hide popup on click
//super.setPopupVisible(v);
}
};
combo.setPreferredSize(new Dimension(250,25));
combo.setEditable(true);
combo.setModel(makeModel());
combo.addActionListener(this);
combo.setRenderer(renderer);
combo.setEditor(editor);
JPanel panel = new JPanel();
panel.add(combo);
return panel;
}
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new MyCombo().getContent());
f.setSize(300,160);
f.setLocation(200,200);
f.setVisible(true);
}
class CheckComboRenderer implements ListCellRenderer
{
JCheckBox checkBox;
public CheckComboRenderer()
{
checkBox = new JCheckBox();
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus)
{
CheckComboStore store = (CheckComboStore)value;
checkBox.setText(store.getId());
checkBox.setSelected(((Boolean)store.state).booleanValue());
checkBox.setBackground(isSelected ? Color.red : Color.white);
return checkBox;
}
}
class ComboEditor implements ComboBoxEditor
{
JTextField tf;
public ComboEditor()
{
tf = new JTextField("");
}
public void addActionListener( ActionListener l ) {}
public void addKeyListener( KeyListener k){}
public Component getEditorComponent()
{
return tf;
}
public Object getItem()
{
return tf.getText();
}
public void removeActionListener( ActionListener l ) {}
public void selectAll() {}
public void setItem( Object o ) {}
}
class CheckComboStore
{
String id = "";
Boolean state = false;
public String toString()
{
return id;
}
public CheckComboStore(String id, Boolean state)
{
this.id = id;
this.state = state;
}
public String getId()
{
return id;
}
public Boolean getState()
{
return state;
}
}
}