JavaFX TableCell ClassCastException

时间:2014-06-03 14:05:16

标签: java exception javafx tableview javafx-8

我有一个带可编辑单元格的TableView。

One Cell包含一个带有组合框的可编辑单元格。如果我想更改当前值,我会得到一个ClassCast异常。但我找不到任何像String这样的变量。

哪种方法更改实例String?

谢谢!

控制器:

cols.get(1).setCellValueFactory(new PropertyValueFactory<LieferscheinArtikel, Produkt>("produkt"));
    Callback<TableColumn, TableCell> cellFactoryProdukt =
              new Callback<TableColumn, TableCell>() {
                  public TableCell call(TableColumn p) {
                      return new LieferscheinArtikelEditingProdukt();
                  }
              };
    cols.get(1).setCellFactory(cellFactoryProdukt);
    cols.get(1).setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<LieferscheinArtikel, Produkt>>() {

        @Override
        public void handle(CellEditEvent<LieferscheinArtikel, Produkt> event) {
            // TODO Auto-generated method stub

        }
    });

TableCell类:

public class LieferscheinArtikelEditingProdukt extends
    TableCell<LieferscheinArtikel, Produkt> {

private ComboBox<Produkt> combobox;
private AutoCompleteComboBoxListener<Produkt> auto;

public LieferscheinArtikelEditingProdukt() {
    combobox = new ComboBox<Produkt>();
    auto = new AutoCompleteComboBoxListener<Produkt>(combobox);
    combobox.setItems(ProduktDataObserv.getInstance("normal").getProduktData());
    combobox.valueProperty().addListener(new ChangeListener<Produkt>() {

        @Override
        public void changed(ObservableValue<? extends Produkt> observable,
                Produkt oldValue, Produkt newValue) {
            System.out.println("change: "+oldValue+" "+newValue);
            setItem(newValue);
            System.out.println((getItem() instanceof Produkt) +" "+getItem());
        }
    });

}

@Override
public void startEdit() {
    super.startEdit();

    if(getItem() != null){
        System.out.println((getItem() instanceof Produkt) +" "+getItem());
        combobox.getSelectionModel().select(getItem());
    }
    //setGraphic(textField);
    setGraphic(combobox);
    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
    //textField.selectAll();
}

@Override
public void cancelEdit() {
    //super.cancelEdit();
    //System.out.println("exit: "+combobox.getSelectionModel().getSelectedItem());
    //setText(getItem());
    System.out.println("can: "+getItem() instanceof String);
    //setItem(combobox.getSelectionModel().getSelectedItem());
    //setText(combobox.getSelectionModel().getSelectedItem().toString());
    //setText(getItem().toString());
    setGraphic(null);
    setContentDisplay(ContentDisplay.TEXT_ONLY);
}

@Override
public void updateItem(Produkt item, boolean empty) {
    if(getItem() != null){
    System.out.println("upd: "+this.getItem()!=null?getItem().toString():null);
    //super.updateItem(item, empty);
    System.out.println((getItem() instanceof Produkt) +" "+getItem());
    }
    if (empty) {
        //setText(null);
        setGraphic(null);
    } else {
        if (isEditing()) {
            if (combobox != null) {
                combobox.getSelectionModel().select(item);
            }
            setGraphic(combobox);
            setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        } else {
            setItem(item);
            //setText(getItem() != null ? getItem().toString() : null);
            setContentDisplay(ContentDisplay.TEXT_ONLY);
        }
    }
    setItem(item);
    //setText(getItem() != null ? getItem().toString() : null);
}

错误消息:

Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: java.lang.String cannot be cast to Produkte.Produkt
at LieferscheinArtikelBearbeiten.LieferscheinArtikelEditingProdukt$1.changed(LieferscheinArtikelEditingProdukt.java:1)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:105)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:167)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.setTextFromTextFieldIntoComboBoxValue(ComboBoxListViewSkin.java:512)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.access$200(ComboBoxListViewSkin.java:57)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$6.changed(ComboBoxListViewSkin.java:416)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$6.changed(ComboBoxListViewSkin.java:406)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:7486)
at javafx.scene.Node.setFocused(Node.java:7537)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$FakeFocusTextField.setFakeFocus(ComboBoxListViewSkin.java:697)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$2.changed(ComboBoxListViewSkin.java:145)
at com.sun.javafx.scene.control.skin.ComboBoxListViewSkin$2.changed(ComboBoxListViewSkin.java:141)
at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
at javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:7486)
at javafx.scene.Scene$15.invalidated(Scene.java:2073)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:145)
at javafx.scene.Scene$KeyHandler.setFocusOwner(Scene.java:3910)
at javafx.scene.Scene$KeyHandler.requestFocus(Scene.java:3956)
at javafx.scene.Scene$KeyHandler.access$2200(Scene.java:3896)
at javafx.scene.Scene.requestFocus(Scene.java:2040)
at javafx.scene.Node.requestFocus(Node.java:7647)
at com.sun.javafx.scene.control.behavior.TableViewBehaviorBase.mousePressed(TableViewBehaviorBase.java:430)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:95)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:204)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3746)
at javafx.scene.Scene$MouseHandler.access$1800(Scene.java:3471)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1695)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2486)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:314)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:243)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:345)
at com.sun.glass.ui.View.handleMouseEvent(View.java:526)
at com.sun.glass.ui.View.notifyMouse(View.java:898)

LieferscheinArtikel:

公共课LieferscheinArtikel {

private IntegerProperty position;
private FloatProperty menge;
private ObjectProperty<Produkt> produkt = new SimpleObjectProperty<Produkt>(this,"produkt");
private FloatProperty preis;
private StringProperty notiz;
private FloatProperty retour;

public LieferscheinArtikel() {
    produkt.set(ProduktDataObserv.getInstance("normal").getProdukt(1));
    preis = new SimpleFloatProperty(produkt.get().getPreis());
    retour = new SimpleFloatProperty(0);
}

public LieferscheinArtikel(int produktnr) {
    produkt.set(ProduktDataObserv.getInstance("normal").getProdukt(produktnr));
    preis = new SimpleFloatProperty(produkt.get().getPreis());
    retour = new SimpleFloatProperty(0);
}

public LieferscheinArtikel(int position, float menge, float retour,
        int produktnr, String produktart, float preis, String notiz) {
    this.position = new SimpleIntegerProperty(position);
    this.menge = new SimpleFloatProperty(menge);
    produkt.set(ProduktDataObserv.getInstance(
            produktart.startsWith("KNr") ? produktart : "normal")
            .getProdukt(produktnr));
    this.preis = new SimpleFloatProperty(preis);
    this.notiz = new SimpleStringProperty(notiz);
    this.retour = new SimpleFloatProperty(retour);
}

public float getMenge() {
    return menge.get();
}

public void setMenge(float menge) {
    this.menge.set(menge);
}

public float getPreis() {
    return preis.get();
}

public void setPreis(float preis) {
    this.preis.set(preis);
    ;
}

public Produkt getProdukt() {
    return produkt.get();
}

public void setProdukt(Produkt produkt) {
    System.out.println("setin");
    this.produkt.set(produkt);
}

public String getNotiz() {
    return notiz == null ? "" : notiz.get();
}

public void setNotiz(String notiz) {
    this.notiz.set(notiz);
}

public int getPosition() {
    return position == null ? 1 : position.get();
}

public void setPosition(int position) {
    this.position.set(position);
}

public float getRetour() {
    return retour.get();
}

public void setRetour(float retour) {
    this.retour.set(retour);
}

public String getPreisanz() {
    return String.format("%.2f \u20ac", preis.get());
}

public ObjectProperty<Produkt> produktProperty(){
    return produkt;
}

public String getRetouranz() {
    System.out.println(retour + " "+produkt.get().isIsRetourMoeglich());
    if (!produkt.get().isIsRetourMoeglich())
        return "XXXX";
    else
    if (retour == null)
        return "";
    else
        return String.format("%.2f", retour.get());
}

@Override
public int hashCode() {
    int hash = 7;
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if (getClass() != obj.getClass()) {
        return false;
    }
    final LieferscheinArtikel other = (LieferscheinArtikel) obj;
    if (this.menge != null && other != null)
        try {
            if (Float.floatToIntBits(this.menge.get()) != Float
                    .floatToIntBits(other.menge.get())) {
                return false;
            }
        } catch (NullPointerException ex) {

        }
    if (this.produkt != null)
        if (!Objects.equals(this.produkt, other.produkt)) {
            return false;
        }
    if (this.preis != null)
        if (Float.floatToIntBits(this.preis.get()) != Float
                .floatToIntBits(other.preis.get())) {
            return false;
        }
    if (this.notiz != null)
        if (!Objects.equals(this.notiz, other.notiz)) {
            return false;
        }
    return true;
}

}

PRODUKT:

public class Produkt {

private IntegerProperty produktid;
private StringProperty bezeichnung;
private StringProperty mengeneinheit;
private FloatProperty preis;
private IntegerProperty mwst;
private StringProperty art_der_ware;
private BooleanProperty isRetourMoeglich;

public Produkt(int produktid, String bezeichnung, String mengeneinheit, float preis, int mwst, String art_der_ware, boolean isRetourMoeglich) {
    this.produktid = new SimpleIntegerProperty(produktid);
    this.bezeichnung = new SimpleStringProperty(bezeichnung);
    this.mengeneinheit = new SimpleStringProperty(mengeneinheit);
    this.preis = new SimpleFloatProperty(preis);
    this.mwst = new SimpleIntegerProperty(mwst);
    this.art_der_ware = new  SimpleStringProperty(art_der_ware);
    this.isRetourMoeglich = new SimpleBooleanProperty(isRetourMoeglich);
}

public Produkt(int produktid, String bezeichnung, String mengeneinheit, float preis, int mwst, String art_der_ware) {
    this.produktid = new SimpleIntegerProperty(produktid);
    this.bezeichnung = new SimpleStringProperty(bezeichnung);
    this.mengeneinheit = new SimpleStringProperty(mengeneinheit);
    this.preis = new SimpleFloatProperty(preis);
    this.mwst = new SimpleIntegerProperty(mwst);
    this.art_der_ware = new  SimpleStringProperty(art_der_ware);
    this.isRetourMoeglich =new SimpleBooleanProperty(false);

}

public Produkt(){
    this.produktid = new SimpleIntegerProperty(0);
    this.bezeichnung =new SimpleStringProperty("");
    this.mengeneinheit = new SimpleStringProperty("");
    this.preis = new SimpleFloatProperty(0.0f);
    this.mwst = new SimpleIntegerProperty(0);
    this.art_der_ware = new SimpleStringProperty("");
    this.isRetourMoeglich =new SimpleBooleanProperty(false);
}

public Produkt(int produktNr){
    this.produktid = new SimpleIntegerProperty(produktNr);
}


public int getProduktid() {
    return produktid.get();
}

public void setProduktid(int produktid) {
    this.produktid.set(produktid);
}

public String getBezeichnung() {
    return bezeichnung.get();
}

public void setBezeichnung(String bezeichnung) {
    this.bezeichnung.set(bezeichnung);
}

public String getMengeneinheit() {
    return mengeneinheit.get();
}

public void setMengeneinheit(String mengeneinheit) {
    this.mengeneinheit.set(mengeneinheit);
}

public float getPreis() {
    return preis.get();
}

public void setPreis(float preis) {
    this.preis.set(preis);
}

public int getMwst() {
    return mwst.get();
}

public void setMwst(int mwst) {
    this.mwst.set(mwst);
}

public String getArt_der_ware() {
    return art_der_ware.get();
}

public void setArt_der_ware(String art_der_ware) {
    this.art_der_ware.set(art_der_ware);
}

public boolean isSomethingEmpty() {
    return (!art_der_ware.get().isEmpty()
            && !bezeichnung.get().isEmpty()
            && !mengeneinheit.get().isEmpty()
            && (mwst.get() != 0)
            && (preis.get() != 0)
            && produktid.get() != 0);
}

public boolean isIsRetourMoeglich() {
    return isRetourMoeglich.get();
}

public void setIsRetourMoeglich(boolean isRetourMoeglich) {
    this.isRetourMoeglich.set(isRetourMoeglich);
}



@Override
public String toString() {
    return bezeichnung.get();
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return false;
    }
    if(obj instanceof Produkt){
    final Produkt other = (Produkt) obj;
    /*if (this.produktid == other.produktid
            && this.art_der_ware.equals(other.art_der_ware)
            && this.bezeichnung.equals(other.bezeichnung)
            && this.mengeneinheit.equals(other.mengeneinheit)
            && this.mwst.get() == other.getMwst()
            && this.preis.get() == other.getPreis()
            && this.isRetourMoeglich.get() == other.isIsRetourMoeglich()) {
        return true;
    }*/

    if (this.produktid.get() == other.getProduktid()) {
        return true;
    }
    }else if (obj instanceof String){
        if(((String)obj).equals(this.getBezeichnung()))
            return true;
    }
    return false;
}

}

1 个答案:

答案 0 :(得分:0)

在此语句中,您尝试将String对象分配给Produkt对象。

cols.get(1).setCellValueFactory(new PropertyValueFactory<LieferscheinArtikel, Produkt>("produkt"));

为PropertyValueFactory()构造函数使用Produkt对象。