“JavaFX Application Thread”java.lang.IllegalArgumentException:参数类型不匹配

时间:2017-04-22 21:39:55

标签: java javafx combobox

当我向下组合一个组合框时,我收到此错误,我使用Javafx库Javoenix

> Exception in thread "JavaFX Application Thread"
> java.lang.IllegalArgumentException: argument type mismatch    at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:497)     at
> sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)    at
> sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>   at java.lang.reflect.Method.invoke(Method.java:497)     at
> sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)   at
> javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)     at
> javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
>   at
> com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
>   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.EventUtil.fireEventImpl(EventUtil.java:74)
>   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)  at
> javafx.event.Event.fireEvent(Event.java:198)  at
> javafx.scene.Node.fireEvent(Node.java:8411)   at
> com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.handleControlPropertyChanged(ComboBoxListViewSkin.java:179)
>   at
> com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
>   at
> com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
>   at
> javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
>   at
> com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
>   at
> com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
>   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:146)
>   at javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:150)
>   at javafx.scene.control.ComboBox.updateValue(ComboBox.java:463)     at
> javafx.scene.control.ComboBox.access$200(ComboBox.java:192)   at
> javafx.scene.control.ComboBox$3.changed(ComboBox.java:446)    at
> com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:361)
>   at
> com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
>   at
> javafx.beans.property.ReadOnlyObjectWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyObjectWrapper.java:176)
>   at
> javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:142)
>   at
> javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
>   at
> javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
>   at
> javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:102)
>   at
> javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$154(ComboBox.java:494)
>   at
> com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:137)
>   at
> com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
>   at
> javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:176)
>   at
> javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:142)
>   at
> javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
>   at
> javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
>   at
> javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
>   at
> javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215)
>   at
> javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
>   at
> com.sun.javafx.scene.control.skin.ComboBoxListViewSkin.lambda$createListView$323(ComboBoxListViewSkin.java:484)
>   at
> com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:349)
>   at
> com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
>   at
> javafx.beans.property.ReadOnlyIntegerWrapper$ReadOnlyPropertyImpl.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:176)
>   at
> javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:142)
>   at
> javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
>   at
> javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
>   at
> javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:68)
>   at
> javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:404)
>   at
> javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:356)
>   at
> javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1403)
>   at
> com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:256)
>   at
> com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:220)
>   at
> com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:150)
>   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.EventUtil.fireEventImpl(EventUtil.java:74)
>   at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)  at
> javafx.event.Event.fireEvent(Event.java:198)  at
> javafx.scene.Scene$MouseHandler.process(Scene.java:3757)  at
> javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)  at
> javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)    at
> javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)  at
> com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
>   at
> com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
>   at java.security.AccessController.doPrivileged(Native Method)   at
> com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
>   at
> com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
>   at
> com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
>   at com.sun.glass.ui.View.handleMouseEvent(View.java:555)    at
> com.sun.glass.ui.View.notifyMouse(View.java:937)  at
> com.sun.glass.ui.win.WinApplication._runLoop(Native Method)   at
> com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
>   at java.lang.Thread.run(Thread.java:745)

这是User类

public class User {

    private int id;
    private  String nom;
    private  String prenom;
    private  String cin;
    private  String tel;
    private  String adresse;
    private  String dn;
    static final String URL = "jdbc:mysql://localhost:3306/gestionpermission";
    static final String Driver ="com.mysql.jdbc.Driver";

    public User(String cin, String nom, String prenom, String dn, String tel, String adresse) {
        this.nom = nom;
        this.prenom = prenom;
        this.cin = cin;
        this.tel = tel;
        this.adresse = adresse;
        this.dn = dn;
    }

    public User() {

    }

    public int getId() {
        return id;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public void setCin(String cin) {
        this.cin = cin;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setDn(String dn) {
        this.dn = dn;
    }

    public String getNom() {
        return nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public String getCin() {
        return cin;
    }

    public String getTel() {
        return tel;
    }

    public String getAdresse() {
        return adresse;
    }

    public String getDn() {
        return dn;
    }

并在控制器中

User u=new User();
          cbo_users.setCellFactory((comboBox) -> {
                return new ListCell<User>() {
                    @Override
                    protected void updateItem(User item, boolean empty) {
                        super.updateItem(item, empty);

                        if (item == null || empty) {
                            setText(null);
                        } else {
                            setText(item.getNom()+" " +item.getPrenom());
                        }
                    }
                };
            });
          cbo_users.setConverter(new StringConverter<User>() {
            @Override
            public String toString(User u) {
                if (u == null) {
                    return null;
                } else {
                    return u.getNom() + " " + u.getPrenom();
                }
            }

            @Override
            public User fromString(String personString) {
                return null; // No conversion fromString needed.
            }
        });

          cbo_users.setItems(u.afficher_user());

组合框上的动作事件

 void select_user(MouseEvent event) {
              User selectuser = cbo_users.getSelectionModel().getSelectedItem();
            System.out.println(selectuser.getNom());
           // the same result on selectuser.tostring();
        }

fxml文件:

  <JFXComboBox fx:id="cbo_users" layoutX="543.0" layoutY="35.0" onAction="#select_user" prefHeight="33.0" prefWidth="185.0" />

方法Afficher_user返回一个Observablelist:

public ObservableList afficher_user() throws SQLException{    
         ObservableList<User> data = FXCollections.observableArrayList();
            Connection conn=null;
          try {
                Class.forName(Driver);
                conn = DriverManager.getConnection(URL,"root","");
                String sql ="select * from utilisateur";
                PreparedStatement Stmt = conn.prepareStatement(sql);
                ResultSet rs = Stmt.executeQuery(sql);
                while(rs.next()){
                User us = new User();
                us.setId(rs.getInt("id"));
                us.setNom(rs.getString("nom"));
                us.setPrenom(rs.getString("prenom"));
                us.setCin(rs.getString("cin"));
                us.setAdresse(rs.getString("adresse"));
                us.setTel(rs.getString("tel"));
                us.setDn(rs.getString("dn"));  
                data.add(us);
                }
                rs.close();
                conn.close();
          } catch (ClassNotFoundException ex) {
                System.err.println(ex.getMessage());
          }
      return data;
}

1 个答案:

答案 0 :(得分:0)

改变:

void select_user(MouseEvent event) {
              User selectuser = cbo_users.getSelectionModel().getSelectedItem();
            System.out.println(selectuser.getNom());
  }

用这个:

void select_user(ActionEvent event) {
              User selectuser = cbo_users.getSelectionModel().getSelectedItem();
            System.out.println(selectuser.getNom());
  }