我有一个自定义组合框ListCellRenderer,如下所示。
class ArtikelListRenderer extends JLabel implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Artikel artikels = (Artikel) value;
setText(artikels.getName());
return this;
}
}
我将此渲染添加到组合框中,如下所示。
public void setUpArtikelComboBox() {
panel.lstArtikel = readFromArtikel();
panel.cbxArtikel = new WebComboBox(new ComboBoxModelArtikel(panel, lstArtikel));
panel.cbxArtikel.setAction(new ComboBoxArtikelActionListener(panel));
panel.cbxArtikel.setRenderer(new ArtikelListRenderer());
panel.artikelTabPanel.add(panel.cbxArtikel, BorderLayout.NORTH);
}
为什么我
Exception in thread "AWT-EventQueue-1" java.lang.ClassCastException: java.lang.String
在ListCellRenderer类中的行:
Artikel artikels = (Artikel) value;
答案 0 :(得分:1)
我认为ComboBoxModelArtikel
是您的自定义ComboBoxModel
,并使用List
或其他Collection
。
我还假设在打电话时
panel.cbxArtikel = new WebComboBox(new ComboBoxModelArtikel(panel, lstArtikel));
您正确初始化了内部List
(或代表您模型的其他类)。
你有
吗?JList.setPrototypeCellValue(Object prototypeCellValue)
代码中的某处和String
作为参数?您使用ListCellRenderer
prototypeCellValue
答案 1 :(得分:0)
这意味着“价值”不属于Artikel类型。
答案 2 :(得分:0)
我似乎你的组合框模型包含String
s。
WebComboBox和ComboBoxModelArtikel的来源是什么?
修改强>
在我看来,它正如我所料(我想模拟你的问题)......有3个未解答的问题:
package net.betlista.swing;
import java.awt.Component;
import java.util.Arrays;
import java.util.List;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListDataListener;
public class JComboBoxTest extends JFrame {
private JComboBox cb;
public JComboBoxTest() {
super( "JComboBoxTest" );
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
initGUI();
this.pack();
this.setVisible( true );
}
private void initGUI() {
initCB();
this.add( cb );
}
private void initCB() {
final Person[] data = { new Person( "A" ), new Person( "B" ) };
final PersonComboBoxModel model = new PersonComboBoxModel( Arrays.asList( data ) );
cb = new JComboBox( model );
cb.setRenderer( new PersonRenderer() );
}
class Person {
String name;
public Person(final String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
class PersonRenderer implements ListCellRenderer {
@Override
public Component getListCellRendererComponent( final JList list, final Object value, final int index,
final boolean isSelected, final boolean cellHasFocus ) {
System.out.println( "class: " + ( value == null ? "" : value.getClass() ) );
return new JLabel( String.valueOf( value == null ? "" : value ) );
}
}
class PersonComboBoxModel implements ComboBoxModel {
List<Person> list;
int selected = -1;
public PersonComboBoxModel(final List<Person> pl) {
list = pl;
}
@Override
public int getSize() {
return list.size();
}
@Override
public Object getElementAt( final int index ) {
return list.get( index );
}
@Override
public void addListDataListener( final ListDataListener l ) {
}
@Override
public void removeListDataListener( final ListDataListener l ) {
}
@Override
public void setSelectedItem( final Object anItem ) {
list.indexOf( anItem );
}
@Override
public Object getSelectedItem() {
if ( 0 <= selected && selected < list.size() ) return list.get( selected );
return null;
}
}
public static void main( final String[] args ) {
new JComboBoxTest();
}
}