JavaFX 8 + Scene Builder +编辑单元&写回FXCollections.observableArrayList

时间:2017-03-02 15:55:21

标签: tableview javafx-8 scenebuilder

我想执行以下操作:编辑单元格,将数据保存到FXCollections.observableArrayList中,然后填充表格。

FXMLController:

@FXML void initialize(){     断言nickColumn!= null:" fx:id = \" nickColumn \"没有注入:检查你的FXML文件' Scene.fxml'。&#34 ;;     断言groupColumn!= null:" fx:id = \" groupColumn \"未注入:检查您的FXML文件' Scene.fxml'。";

PlayerList playerList = new PlayerList();

playerTable.setItems(playerList.playerList);

nickColumn.setCellValueFactory(new PropertyValueFactory<Player,String>("nick"));
nickColumn.setCellFactory(TextFieldTableCell.forTableColumn());

groupColumn.setCellValueFactory(new PropertyValueFactory<Player,String>("group"));
yearOfBirthColumn.setCellValueFactory(new PropertyValueFactory<Player,Integer>("yearOfBirth"));
ageColumn.setCellValueFactory(new PropertyValueFactory<Player,Integer>("age"));
genderColumn.setCellValueFactory(new PropertyValueFactory<Player,String>("gender"));
lastOnlineColumn.setCellValueFactory(new PropertyValueFactory<Player,String>("lastOnline"));

}

Player.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.freakyonline.ucone;

import java.time.format.FormatStyle;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

/**
 *
 * @author uwe
 */
public class Player {
    private SimpleStringProperty nick;
    private SimpleStringProperty group;
    private SimpleStringProperty gender;
    private SimpleIntegerProperty yearOfBirth;
    private SimpleIntegerProperty age;
    private SimpleStringProperty lastOnline; 


    public Player (String nick, String group, int yearOfBirth, int age, String gender, String lastOnline) {
        this.nick = new SimpleStringProperty(nick);
        this.group = new SimpleStringProperty(group);
        this.gender = new SimpleStringProperty(gender);
        this.age = new SimpleIntegerProperty(age);
        this.lastOnline = new SimpleStringProperty(lastOnline);
        this.yearOfBirth = new SimpleIntegerProperty(yearOfBirth);

        System.out.println("In Player Class:" + lastOnline);
    }

    public String getNick () {
        return nick.get();
    }

    public String getGroup () {
        return group.get();
    }

    public String getGender () {
        return gender.get();
    }

    public int getYearOfBirth() {
        return yearOfBirth.get();
    }

    public int getAge() {
        return age.get();
    }

    public String getLastOnline() {
        return lastOnline.get();
    }

    public void setNick (String nick) {
        this.nick.set(nick);
    }

    public void setGroup (String group) {
        this.group.set(group);
    }

    public void setGender (String gender) {
        this.gender.set(gender);
    }

    public void setYearOfBirth (int yearOfBirth) {
        this.yearOfBirth.set(yearOfBirth);
    }

    public void setAge (int age) {
        this.age.set(age);
    }

    public void setLastOnline (String lastOnline) {
        this.lastOnline.set(lastOnline);
    }

}

PlayerList.java - 这里是列表,之后所有内容都被填充(playerTable.setItems(playerList.playerList);在FXMLController中:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.freakyonline.ucone;

import de.freakyonline.ucone.Player;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 *
 * @author uwe
 */
public class PlayerList {

    ObservableList<Player> playerList;

    public PlayerList() {
        getPlayersFromServer();
    }

    private void getPlayersFromServer() {
        this.playerList = FXCollections.observableArrayList(
        new Player("freakyy85","Owner",1000,1,"m", "missing..."),
        new Player("Ky3ak","Owner",1999,10,"m", "missing...")
        );
    }
}

我已在Scene Builder中将列设置为可编辑。我已添加此内容以使刻痕可编辑:

nickColumn.setCellFactory(TextFieldTableCell.forTableColumn());

我这是为了处理提交单元格的事件(来自内部场景构建器):

   @FXML
    void handlePlayerEditCommit(ActionEvent event) {
        System.out.println("Commit.");

    }

但按下返回时出现此错误:

线程中的异常&#34; JavaFX应用程序线程&#34; java.lang.IllegalArgumentException:参数类型不匹配     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:498)     at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)     at sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:498)     at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)     在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1769)     在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.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)     在javafx.event.Event.fireEvent(Event.java:198)     在javafx.scene.control.TableCell.commitEdit(TableCell.java:349)     在javafx.scene.control.cell.CellUtils.lambda $ createTextField $ 615(CellUtils.java:248)     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.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)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)     在javafx.event.Event.fireEvent(Event.java:198)     在javafx.scene.Node.fireEvent(Node.java:8411)     at com.sun.javafx.scene.control.behavior.TextFieldBehavior.fire(TextFieldBehavior.java:179)     at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:178)     at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:218)     at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:127)     at com.sun.javafx.scene.control.behavior.BehaviorBase.lambda $ new $ 74(BehaviorBase.java:135)     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.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)     在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)     在javafx.event.Event.fireEvent(Event.java:198)     在javafx.scene.Scene $ KeyHandler.process(Scene.java:3964)     在javafx.scene.Scene $ KeyHandler.access $ 1800(Scene.java:3910)     在javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)     在javafx.scene.Scene $ ScenePeerListener.keyEvent(Scene.java:2501)     at com.sun.javafx.tk.quantum.GlassViewEventHandler $ KeyEventNotification.run(GlassViewEventHandler.java:216)     at com.sun.javafx.tk.quantum.GlassViewEventHandler $ KeyEventNotification.run(GlassViewEventHandler.java:148)     at java.security.AccessController.doPrivileged(Native Method)     at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleKeyEvent $ 353(GlassViewEventHandler.java:247)     at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)     at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:246)     在com.sun.glass.ui.View.handleKeyEvent(View.java:546)     在com.sun.glass.ui.View.notifyKey(View.java:966)     at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)     at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191)     在java.lang.Thread.run(Thread.java:745)

所以我需要知道,我必须做些什么才能使PropertyValueFactory和PropertyValueFactory的参数类型不匹配。

非常感谢你的帮助!

0 个答案:

没有答案