如何在javaFX中单击按钮时使用用户输入填充表视图中的数据

如何在javaFX中单击按钮时使用用户输入填充表视图中的数据,java,javafx-2,tableview,fxml,netbeans-8,Java,Javafx 2,Tableview,Fxml,Netbeans 8,我在我的fxml文件中给出了正确的id,但代码不起作用,也没有错误。我想输入所有文本,在按下“添加”按钮时,必须将数据填入tableview。输入是文本、单选按钮和转换为字符串的复选框。在选择行时,必须单击“删除”按钮删除该行 public class CashDrawerController implements Initializable { @FXML TextField UnitText; @FXML CheckBox NCCheck; @FXML TextField

我在我的fxml文件中给出了正确的id,但代码不起作用,也没有错误。我想输入所有文本,在按下“添加”按钮时,必须将数据填入tableview。输入是文本、单选按钮和转换为字符串的复选框。在选择行时,必须单击“删除”按钮删除该行

public class CashDrawerController implements Initializable 
{
   @FXML TextField UnitText;
   @FXML CheckBox NCCheck;
   @FXML TextField CurrencyText;
   @FXML  RadioButton BillRadio;
   @FXML RadioButton CoinRadio;
   String select="";
   String NC="N";
   @FXML TableView<Person> table;
   @FXML TableColumn<Person,String> unitcol ;
   @FXML TableColumn<Person,String> valuecol ;
   @FXML TableColumn<Person,String> typecol ;
   @FXML TableColumn<Person,String> nccol ;
  private finalObservableList<Person>data=FXCollections.observableArrayList(
                new Person("Jacob",  "Smith","jacob.smith@example.com","sadfdsf"),
                new Person("Isabella", "Johnson", "isabella.johnson@example.com","sdfsdaf"),
                new Person("Ethan", "Williams", "ethan.williams@example.com","adsfdsf"),
                new Person("Emma", "Jones", "emma.jones@example.com","dsfsad"),
                new Person("Michael", "Brown", "michael.brown@example.com","sdfsafd")
        );
@FXML
private void handleRemoveButtonAction(ActionEvent ev){

}
@FXML
private void  handleAddButtonAction(ActionEvent eve){
    if(BillRadio.isSelected()){
        select="Bill";
    }
    if(CoinRadio.isSelected()){
      select="Coin";
    }
    if(NCCheck.isSelected()){
        NC="Y";
    }        
   data.add(new Person(
            UnitText.getText(),
            CurrencyText.getText(),
            select,NC));
    UnitText.clear();
    CurrencyText.clear();
    Person obj=new Person("name1","val1","type","nc1");
    obj.setUnitName("name2");
    obj.setValue("val2");
    obj.setType("type2");
    obj.setNC("nc2");
unitcol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person,String>("UnitName"));
  valuecol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person,String>("Value"));


 typecol.setCellValueFactory(new javafx.scene.control.cell.PropertyValueFactory<Person,String>("Type"));



nccol.setCellValueFactory(newjavafx.scene.control.cell.PropertyValueFactory<Person,String>("NC"));  

}

@Override
public void initialize(URL url, ResourceBundle rb) {            

}    


public static class Person {

    private final SimpleStringProperty UnitName;
    private final SimpleStringProperty Value;
    private final SimpleStringProperty Type;
     private final SimpleStringProperty NC;
    private Person(String uName, String val, String type,String nc) {
        this.UnitName = new SimpleStringProperty(uName);
        this.Value = new SimpleStringProperty(val);
        this.Type = new SimpleStringProperty(type);
        this.NC = new SimpleStringProperty(nc);
    }

    public String getUnitName() {
        return UnitName.get();
    }

    public void setUnitName(String uName) {
        UnitName.set(uName);
    }

    public String getValue() {
        return Value.get();
    }

    public void setValue(String val) {
        Value.set(val);
    }

    public String getType() {
        return Type.get();
    }

    public void setType(String type) {
        Type.set(type);
    }

    public String getNC(){
        return NC.get();
    }

    public void setNC(String nc){
        NC.set(nc);
    }
}

}
公共类现金抽屉控制器实现可初始化
{
@FXML文本字段UnitText;
@FXML复选框NCCheck;
@FXML文本字段CurrencyText;
@FXML收音机按钮;
@FXML收音机按钮;
字符串select=“”;
字符串NC=“N”;
@FXML表视图表;
@FXML表列单位;
@FXML表列值COL;
@FXML表柱型色谱柱;
@FXML表柱nccol;
private finalObservableListdata=FXCollections.observableArrayList(
新人(“雅各布”、“史密斯”、“雅各布”。smith@example.com“,“sadfdsf”),
新人(“伊莎贝拉”、“约翰逊”、“伊莎贝拉”。johnson@example.com“,“sdfsdaf”),
新人(“伊桑”、“威廉姆斯”、“伊桑”。williams@example.com“,“adsfdsf”),
新人(“艾玛”、“琼斯”、“艾玛”。jones@example.com“,“dsfsad”),
新人(“迈克尔”、“布朗”、“迈克尔”。brown@example.com“,“自卫队”)
);
@FXML
私有无效句柄移动按钮操作(ActionEvent ev){
}
@FXML
私有无效手动按钮操作(ActionEvent eve){
如果(BillRadio.isSelected()){
选择=“票据”;
}
if(CoinRadio.isSelected()){
选择“硬币”;
}
if(NCCheck.isSelected()){
NC=“Y”;
}        
数据。添加(新人员)(
UnitText.getText(),
CurrencyText.getText(),
选择(NC));
UnitText.clear();
CurrencyText.clear();
人员对象=新人员(“姓名1”、“val1”、“类型”、“nc1”);
对象setUnitName(“名称2”);
对象设定值(“val2”);
对象设置类型(“类型2”);
对象setNC(“nc2”);
unitcol.setCellValueFactory(新的javafx.scene.control.cell.PropertyValueFactory(“UnitName”);
valuecol.setCellValueFactory(新的javafx.scene.control.cell.PropertyValueFactory(“值”);
typecol.setCellValueFactory(新的javafx.scene.control.cell.PropertyValueFactory(“类型”);
nccol.setCellValueFactory(newjavafx.scene.control.cell.PropertyValueFactory(“NC”);
}
@凌驾
公共无效初始化(URL,ResourceBundle rb){
}    
公共静态类人员{
私有最终SimpleStringProperty单位名称;
私有财产价值;
私有最终SimpleStringProperty类型;
私人最终财产;
私人(字符串uName、字符串val、字符串类型、字符串nc){
this.UnitName=新的SimpleStringProperty(uName);
this.Value=新的SimpleStringProperty(val);
this.Type=新的SimpleStringProperty(类型);
this.NC=新的SimpleStringProperty(NC);
}
公共字符串getUnitName(){
返回UnitName.get();
}
public void setUnitName(字符串uName){
UnitName.set(uName);
}
公共字符串getValue(){
返回值。get();
}
公共无效设置值(字符串值){
值集(val);
}
公共字符串getType(){
返回类型:get();
}
公共void集合类型(字符串类型){
类型。设置(类型);
}
公共字符串getNC(){
返回NC.get();
}
公共无效设置nc(字符串nc){
NC.set(NC);
}
}
}

您不需要在每次单击时设置单元格工厂,可以将所有
setCellValueFactory
放入
initialize

public void initialize(URL url, ResourceBundle rb) { 
    unitcol.setCellValueFactory(new PropertyValueFactory<Person,String>("UnitName"));
    valuecol.setCellValueFactory(new PropertyValueFactory<Person,String>("Value"));
    typecol.setCellValueFactory(new PropertyValueFactory<Person,String>("Type"));
    nccol.setCellValueFactory(new PropertyValueFactory<Person,String>("NC")); 
} 

以及最重要的部分,我看不到您将数据设置到表中的位置:将此添加到
初始化
表中。setItems(数据)

这是一个示例,在初始化方法中,我使用setCellValueFactory,并通过Mytable将项目(可观察列表)分配给表trought。setItems方法:

package application.view;

import java.io.IOException;
import application.Main;
import application.model.ActoHabla;
import application.model.agent;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.util.StringConverter;

public class CommunicationModelController {
    private Main mainApp;
    private agent agente;
    @FXML
    private ProgressBar progreso;
    @FXML
    private TreeView<Object> arbol;
    @FXML
    private TextField nombre;
    @FXML
    private ComboBox<String> tipo;
    @FXML
    private ComboBox<agent> agentes_participantes;
    @FXML
    private TextArea objetivo;
    @FXML
    private TextArea datos_intercambiados;
    @FXML
    private TextArea precondicion;
    @FXML
    private TextArea terminacion;
    @FXML
    private TextArea descripcion;
    @FXML
    private TableView<ActoHabla> tabla_actos;
     @FXML
     private TableColumn<ActoHabla, String> NameColumn;
    @FXML
    private TableView<agent> tabla_agentes;
    @FXML
    private TableColumn<agent, String> AgenteColumn;

    @FXML
    TableColumn<Fila, String> clave;
    @FXML
    TableColumn<Fila, String> valor;
    @FXML
    private TableView<Fila> tabla= new TableView<>();
    private ObservableList<Fila> filas=FXCollections.observableArrayList();


    @FXML
    private void initialize() {
        NameColumn.setCellValueFactory(cellData -> cellData.getValue().getNombre());
        AgenteColumn.setCellValueFactory(cellData -> cellData.getValue().getName());

        ah=new ActoHabla();
        tabla_agentes.setItems(ah.getAgentes());
        iniciar_tabla();
        tabla_actos.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> ShowActoValues(newValue));

        agentes_participantes.setConverter(new StringConverter<agent>() {

            @Override
            public String toString(agent object) {
                return object.getNameString();
            }

            @Override
            public agent fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });
    }


    public Main getMainApp() {
        return mainApp;
    }
    public void setMainApp(Main mainApp) {
        this.mainApp = mainApp;
        ObservableList<agent> a=FXCollections.observableArrayList();
        a.addAll(mainApp.getAgentData());
        agentes_participantes.setItems(a);
        tipo.setItems(mainApp.getDatabase().getActos_habla());
    }


    /**
     * Funcion que guarda un acto de habla en la lista correspondiente
     *
     * **/
    @FXML
    public void handleRegistrar(){
        ah.setNombre(new SimpleStringProperty(nombre.getText()));
        nombre.setText("");

        ah.setTipo(new SimpleStringProperty(tipo.getSelectionModel().getSelectedItem()));
        tipo.getSelectionModel().clearSelection();

        ah.setObjetivo(new SimpleStringProperty(objetivo.getText()));
        objetivo.setText("");

        ah.setDatosintercambiados(new SimpleStringProperty(datos_intercambiados.getText()));
        datos_intercambiados.setText("");

        ah.setPrecondicion(new SimpleStringProperty(precondicion.getText()));
        precondicion.setText("");

        ah.setCTerminacion(new SimpleStringProperty(terminacion.getText()));
        terminacion.setText("");

        ah.setDescripcion(new SimpleStringProperty(descripcion.getText()));
        descripcion.setText("");

        ObservableList<agent> a=FXCollections.observableArrayList();
        a.addAll(mainApp.getAgentData());

        agentes_participantes.setItems(a);

        boolean is=false;
        for (ActoHabla iter : agente.getModelo_comunicaciones().getActor()) {
            System.out.println(iter.getNombre()+" "+ah.getNombre());
            if(iter.getNombre().get().equals(ah.getNombre().get())){
                is=true;
                agente.getModelo_comunicaciones().getActor().removeAll(iter);
                agente.getModelo_comunicaciones().getActor().add(ah);
                break;
            }//hay uno igual
        }


        if(!is)
            agente.getModelo_comunicaciones().getActor().add(ah);

        ah=new ActoHabla();
        ObservableList<agent> ab=FXCollections.observableArrayList();
        ab.addAll(mainApp.getAgentData());
        agentes_participantes.setItems(ab);

        ah.getAgentes().removeAll();
        tabla_agentes.setItems(ah.getAgentes());
        mainApp.getMensaje().set("Acto de habla registrado!");
    }//

    @FXML
    public void handleAgregar(){
        ah.getAgentes().add(agentes_participantes.getSelectionModel().getSelectedItem());
        agentes_participantes.getItems().remove(agentes_participantes.getSelectionModel().getSelectedItem());
        agentes_participantes.getSelectionModel().clearSelection();
        mainApp.getMensaje().set("Agente agregado");
    }

    public void ShowActoValues(ActoHabla acto){
        //igualar acto con ah
        nombre.setText(acto.getNombre().get());
        ah.setNombre(acto.getNombre());

        tipo.getSelectionModel().select(acto.getTipo().get());
        ah.setTipo(acto.getTipo());

        objetivo.setText(acto.getObjetivo().get());
        ah.setObjetivo(acto.getObjetivo());

        ah.setAgentes(acto.getAgentes());
        tabla_agentes.setItems(ah.getAgentes());
        ObservableList<agent> temp=mainApp.getAgentData();
        temp.removeAll(acto.getAgentes());
        agentes_participantes.setItems(temp);


        ah.setDatosintercambiados(acto.getDatosintercambiados());
        datos_intercambiados.setText(acto.getDatosintercambiados().get());

        ah.setPrecondicion(acto.getPrecondicion());
        precondicion.setText(acto.getPrecondicion().get());

        terminacion.setText(acto.getCTerminacion().get());
        ah.setCTerminacion(acto.getCTerminacion());

        descripcion.setText(acto.getDescripcion().get());
        ah.setDescripcion(acto.getDescripcion());

    }

    public agent getAgente() {
        return agente;
    }

    public void setAgente(agent agente) {
        this.agente = agente;
        tabla_actos.setItems(agente.getModelo_comunicaciones().getActor());
    }
}
packapplication.view;
导入java.io.IOException;
进口申请.Main;
导入application.model.ActoHabla;
导入应用程序.model.agent;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.collections.FXCollections;
导入javafx.collections.ListChangeListener;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.fxml.fxmloader;
导入javafx.scene.control.ComboBox;
导入javafx.scene.control.ProgressBar;
导入javafx.scene.control.SelectionMode;
导入javafx.scene.control.TableColumn;
导入javafx.scene.control.TableView;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextField;
导入javafx.scene.control.TreeView;
导入javafx.scene.layout.ancorpane;
导入javafx.scene.text.text;
导入javafx.util.StringConverter;
公共类通信模型控制器{
私有主应用程序;
私人代理;
@FXML
私人ProgressBar progreso;
@FXML
私人树丛;
@FXML
私有文本字段名称;
@FXML
(一)私人组合箱(tipo);;
@FXML
私人组合框代理和参与者;
@FXML
私有文本区域对象;
@FXML
私人文本区datos_intercambiados;
@FXML
私人区域预先决定;
@FXML
专用区域终端;
@FXML
私有文本区域描述;
@FXML
私人桌面视图tabla_actos;
@FXML
私有表列名称列;
@FXML
私人TableView表格代理;
@FXML
私有表列代理列;
@FXML
表柱状裂纹;
@FXML
表列英勇;
@FXML
private TableView tabla=新建TableView();
私有ObservableList filas=FXCollections.observableArrayList();
@FXML
私有void初始化(){
NameColumn.setCellValueFactory(cellData->cellData.getValue().getNombre());
AgenteColumn.setCellValueFactory(cellData->cellData.getValue().getName());
ah=新阿克托哈布拉();
tabla_agents.setItems(ah.getagents());
iniciar_tabla();
tabla_actos.getSelectionModel().se
package application.view;

import java.io.IOException;
import application.Main;
import application.model.ActoHabla;
import application.model.agent;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import javafx.util.StringConverter;

public class CommunicationModelController {
    private Main mainApp;
    private agent agente;
    @FXML
    private ProgressBar progreso;
    @FXML
    private TreeView<Object> arbol;
    @FXML
    private TextField nombre;
    @FXML
    private ComboBox<String> tipo;
    @FXML
    private ComboBox<agent> agentes_participantes;
    @FXML
    private TextArea objetivo;
    @FXML
    private TextArea datos_intercambiados;
    @FXML
    private TextArea precondicion;
    @FXML
    private TextArea terminacion;
    @FXML
    private TextArea descripcion;
    @FXML
    private TableView<ActoHabla> tabla_actos;
     @FXML
     private TableColumn<ActoHabla, String> NameColumn;
    @FXML
    private TableView<agent> tabla_agentes;
    @FXML
    private TableColumn<agent, String> AgenteColumn;

    @FXML
    TableColumn<Fila, String> clave;
    @FXML
    TableColumn<Fila, String> valor;
    @FXML
    private TableView<Fila> tabla= new TableView<>();
    private ObservableList<Fila> filas=FXCollections.observableArrayList();


    @FXML
    private void initialize() {
        NameColumn.setCellValueFactory(cellData -> cellData.getValue().getNombre());
        AgenteColumn.setCellValueFactory(cellData -> cellData.getValue().getName());

        ah=new ActoHabla();
        tabla_agentes.setItems(ah.getAgentes());
        iniciar_tabla();
        tabla_actos.getSelectionModel().selectedItemProperty().addListener(
                (observable, oldValue, newValue) -> ShowActoValues(newValue));

        agentes_participantes.setConverter(new StringConverter<agent>() {

            @Override
            public String toString(agent object) {
                return object.getNameString();
            }

            @Override
            public agent fromString(String string) {
                // TODO Auto-generated method stub
                return null;
            }
        });
    }


    public Main getMainApp() {
        return mainApp;
    }
    public void setMainApp(Main mainApp) {
        this.mainApp = mainApp;
        ObservableList<agent> a=FXCollections.observableArrayList();
        a.addAll(mainApp.getAgentData());
        agentes_participantes.setItems(a);
        tipo.setItems(mainApp.getDatabase().getActos_habla());
    }


    /**
     * Funcion que guarda un acto de habla en la lista correspondiente
     *
     * **/
    @FXML
    public void handleRegistrar(){
        ah.setNombre(new SimpleStringProperty(nombre.getText()));
        nombre.setText("");

        ah.setTipo(new SimpleStringProperty(tipo.getSelectionModel().getSelectedItem()));
        tipo.getSelectionModel().clearSelection();

        ah.setObjetivo(new SimpleStringProperty(objetivo.getText()));
        objetivo.setText("");

        ah.setDatosintercambiados(new SimpleStringProperty(datos_intercambiados.getText()));
        datos_intercambiados.setText("");

        ah.setPrecondicion(new SimpleStringProperty(precondicion.getText()));
        precondicion.setText("");

        ah.setCTerminacion(new SimpleStringProperty(terminacion.getText()));
        terminacion.setText("");

        ah.setDescripcion(new SimpleStringProperty(descripcion.getText()));
        descripcion.setText("");

        ObservableList<agent> a=FXCollections.observableArrayList();
        a.addAll(mainApp.getAgentData());

        agentes_participantes.setItems(a);

        boolean is=false;
        for (ActoHabla iter : agente.getModelo_comunicaciones().getActor()) {
            System.out.println(iter.getNombre()+" "+ah.getNombre());
            if(iter.getNombre().get().equals(ah.getNombre().get())){
                is=true;
                agente.getModelo_comunicaciones().getActor().removeAll(iter);
                agente.getModelo_comunicaciones().getActor().add(ah);
                break;
            }//hay uno igual
        }


        if(!is)
            agente.getModelo_comunicaciones().getActor().add(ah);

        ah=new ActoHabla();
        ObservableList<agent> ab=FXCollections.observableArrayList();
        ab.addAll(mainApp.getAgentData());
        agentes_participantes.setItems(ab);

        ah.getAgentes().removeAll();
        tabla_agentes.setItems(ah.getAgentes());
        mainApp.getMensaje().set("Acto de habla registrado!");
    }//

    @FXML
    public void handleAgregar(){
        ah.getAgentes().add(agentes_participantes.getSelectionModel().getSelectedItem());
        agentes_participantes.getItems().remove(agentes_participantes.getSelectionModel().getSelectedItem());
        agentes_participantes.getSelectionModel().clearSelection();
        mainApp.getMensaje().set("Agente agregado");
    }

    public void ShowActoValues(ActoHabla acto){
        //igualar acto con ah
        nombre.setText(acto.getNombre().get());
        ah.setNombre(acto.getNombre());

        tipo.getSelectionModel().select(acto.getTipo().get());
        ah.setTipo(acto.getTipo());

        objetivo.setText(acto.getObjetivo().get());
        ah.setObjetivo(acto.getObjetivo());

        ah.setAgentes(acto.getAgentes());
        tabla_agentes.setItems(ah.getAgentes());
        ObservableList<agent> temp=mainApp.getAgentData();
        temp.removeAll(acto.getAgentes());
        agentes_participantes.setItems(temp);


        ah.setDatosintercambiados(acto.getDatosintercambiados());
        datos_intercambiados.setText(acto.getDatosintercambiados().get());

        ah.setPrecondicion(acto.getPrecondicion());
        precondicion.setText(acto.getPrecondicion().get());

        terminacion.setText(acto.getCTerminacion().get());
        ah.setCTerminacion(acto.getCTerminacion());

        descripcion.setText(acto.getDescripcion().get());
        ah.setDescripcion(acto.getDescripcion());

    }

    public agent getAgente() {
        return agente;
    }

    public void setAgente(agent agente) {
        this.agente = agente;
        tabla_actos.setItems(agente.getModelo_comunicaciones().getActor());
    }
}