在TableView中动态添加列并使用javafx编辑单元格

时间:2017-03-20 10:42:38

标签: java javafx tableview tablecolumn

我根据@James_D的建议改变了。我必须动态添加列,并使用TableView手动输入数据(不读取文件)。在这个站点和其他地方的其他问题很好地演示了从文件中读取csv数据文件并在TableView中粘贴,这不是我想要的。

package testmatrix;

import javafx.application.Application;
import javafx.collections.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class TestMATRIX extends Application {

TableView<CommonDataClass> matrixData = new TableView<CommonDataClass>();
final ObservableList<CommonDataClass> matData = FXCollections.
        observableArrayList();
TableColumn[] matrixDataColumns = new TableColumn[6];
int numColVal = 0;
int theNDX = 0;
int maxRowNum = 0;
TextField TC1 = new TextField();
TextField TC2 = new TextField();
TextField TC3 = new TextField();
TextField TC4 = new TextField();
TextField TC5 = new TextField();
TextField TC6 = new TextField();
boolean numColStatus = false;
int oldRowVal = 0;
int oldColVal = 0;
boolean newRow = false;
boolean newCol = false;

@Override
public void start(Stage primaryStage) {
    final BorderPane root = new BorderPane();

    TextField myTextField = new TextField();
    Label colL = new Label("Column Number->");
    TextField colNumT = new TextField();
    Button getNum = new Button("SET");
    colNumT.setMaxWidth(40);

    matrixData.setVisible(true);
    matrixData.setEditable(true);

    matrixData.getSelectionModel().setCellSelectionEnabled(true);
    //matrixData.getSelectionModel ().setSelectionMode (SelectionMode.SINGLE);
    matrixData.addEventFilter(KeyEvent.KEY_PRESSED, (KeyEvent t) -> {
        if (t.getCode() == KeyCode.DOWN || t.getCode() == KeyCode.UP) {
            newRow = true;
        }

        if (matrixData.getEditingCell() == null && ((t.getCode()
                == KeyCode.ENTER) || (t.getCode() == KeyCode.DOWN))) {
            addRowBelow();
            matrixData.getSelectionModel().select(maxRowNum); //.selectBelowCell ();                
            t.consume();
        } else if (t.getCode() == KeyCode.UP) {
           // maxRowNum = (maxRowNum - 1 <= 0) ? 0 : --maxRowNum;
            matrixData.getSelectionModel().select(maxRowNum);
            System.out.println(maxRowNum);
            t.consume();
        }
    });

    HBox hb1 = new HBox();
    HBox hb2 = new HBox();
    hb1.getChildren().add(matrixData);
    hb2.getChildren().addAll(colL, colNumT, getNum);

    root.setCenter(hb1);
    root.setRight(hb2);
    // matrixData.getItems ().clear ();
    //matrixData.getColumns ().clear ();
    getNum.addEventHandler(ActionEvent.ACTION, e -> {
        numColStatus = (colNumT.getText().isEmpty()) ? false : true;
        numColVal = (numColStatus) ? Integer.parseInt(colNumT.getText())
                : 0;

        if (numColStatus) {
            System.out.println("B4 " + maxRowNum);
            addRowBelow();
            for (int ii = 0; ii < numColVal; ii++) {
                matrixDataColumns[ii] = createCol(ii);
                matrixData.getColumns().add(matrixDataColumns[ii]);
                matrixDataColumns[ii].setVisible(true);
                matrixDataColumns[ii].setCellFactory(TextFieldTableCell.
                        <CommonDataClass>forTableColumn());
                matrixDataColumns[ii].setCellValueFactory(
                        new PropertyValueFactory<>("dataName"));
                editCommit(ii);
            }
            System.out.println("A4 " + maxRowNum);
            matrixData.setItems(matData);
            matrixData.refresh();
        }
    });

    Scene scene = new Scene(root, 1200, 400);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

TableColumn<CommonDataClass, String> createCol(int icol) {
    TableColumn<CommonDataClass, String> column = new TableColumn<>();
    column.setMinWidth(30);
    column.setStyle(
            "-fx-alignment: CENTER-RIGHT;-fx-font-family: monospace; -fx-font-size: 10px; ");
    column.setText("C" + (icol + 1));
    // column.setCellValueFactory (new PropertyValueFactory<CommonDataClass, String> (""));

    column.setCellFactory(TextFieldTableCell.forTableColumn());
    column.setMinWidth(120);
    return column;
}

private void editCommit(int ii) {
    System.out.println("Command came here");
    matrixDataColumns[ii].setOnEditCommit(
            new EventHandler<CellEditEvent<CommonDataClass, String>>() {
        @Override
        public void handle(CellEditEvent<CommonDataClass, String> event) {
            event.getTableView().getItems().get(
                    event.getTablePosition().getRow()).setData(event.
                    getNewValue(), theNDX);
            System.out.println("Row->" + event.getTablePosition().
                    getRow() + "  col->" + event.getTablePosition().
                            getColumn() + " newRow?->" + newRow);

            if (!event.getNewValue().isEmpty()) {
                maxRowNum = event.getTablePosition().getRow() + 1; // addRowBelow (maxRowNum); }
                System.out.println("Row->" + maxRowNum + "  " + event.getNewValue());
                newRow = false;
            } else if (!event.getNewValue().isEmpty() && newCol) {
                newCol = false;
            }
        }
    });
}

void addRowBelow() {
    matData.add(new CommonDataClass(
            TC1.getText(), TC2.getText(), TC3.getText(),
            TC4.getText(), TC5.getText(), TC6.getText()
    ));
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

我也将CommonDataClass作为

package testmatrix;
import javafx.beans.property.*;

public class CommonDataClass {

private final SimpleStringProperty myDataCOM1;
private final SimpleStringProperty myDataCOM2;
private final SimpleStringProperty myDataCOM3;
private final SimpleStringProperty myDataCOM4;
private final SimpleStringProperty myDataCOM5;
private final SimpleStringProperty myDataCOM6;

public CommonDataClass(String myStr1, String myStr2, String myStr3, String myStr4, String myStr5, String myStr6) {
    this.myDataCOM1 = new SimpleStringProperty(myStr1);
    this.myDataCOM2 = new SimpleStringProperty(myStr2);
    this.myDataCOM3 = new SimpleStringProperty(myStr3);
    this.myDataCOM4 = new SimpleStringProperty(myStr4);
    this.myDataCOM5 = new SimpleStringProperty(myStr5);
    this.myDataCOM6 = new SimpleStringProperty(myStr6);
}

public String getData(int index) {
    String retEntity = null;
    switch (index) {
        case 0:
            retEntity = myDataCOM1.get();
            break;
        case 1:
            retEntity = myDataCOM2.get();
            break;
        case 2:
            retEntity = myDataCOM3.get();
            break;
        case 3:
            retEntity = myDataCOM4.get();
            break;
        case 4:
            retEntity = myDataCOM5.get();
            break;
        case 5:
            retEntity = myDataCOM6.get();
            break;
    }
    return retEntity;
}

public void setData(String myStr, int index) {
    switch (index) {
        case 0:
            myDataCOM1.set(myStr);
            break;
        case 1:
            myDataCOM2.set(myStr);
            break;
        case 2:
            myDataCOM3.set(myStr);
            break;
        case 3:
            myDataCOM4.set(myStr);
            break;
        case 4:
            myDataCOM5.set(myStr);
            break;
        case 5:
            myDataCOM6.set(myStr);
            break;
    }
}

public StringProperty dataNameProperty(int index) {
    StringProperty strProp = null;

    switch (index) {
        case 0:
            strProp = myDataCOM1;
            break;
        case 1:
            strProp = myDataCOM2;
            break;
        case 2:
            strProp = myDataCOM3;
            break;
        case 3:
            strProp = myDataCOM4;
            break;
        case 4:
            strProp = myDataCOM5;
            break;
        case 5:
            strProp = myDataCOM6;
            break;
    }
    return strProp;
}

}

现在我可以输入一行数据了。之后,如果按Enter键,光标将转到下一行,同时删除上一行。我认为dataNameProperty有问题。

请帮忙。 感谢

0 个答案:

没有答案