How to edit data with dynamic TableView with dynamic column in JAVAFX


Question

Today This is the demo to show data from CSV for DAT file without make custom class on tableView in JavaFX 2.0. I call this TableView as Dynamic TableView because the tableview automatically manages the columns and rows.
On my research about the editable on tableView we must have a custom class and implement it to tableView to show as this demo ==> http://docs.oracle.com/javafx/2/ui_controls/table-view.htm

But in this case I can not do it because we don't know how many column example with csv file or .dat file.... I want to do editable on this tableView in this case by add TextField into TableCell. How does it do without make custom class (because you do not how many column ...), and if it must make custom class then how about the design of custom class for this case?

Could you please help me?

    private void getDataDetailWithDynamic() {
    tblView.getItems().clear();
    tblView.getColumns().clear();
    tblView.setPlaceholder(new Label("Loading..."));
        //            @Override



            try {
                File aFile = new File(txtFilePath.getText());
                InputStream is = new BufferedInputStream(new FileInputStream(aFile));
                Reader reader = new InputStreamReader(is, "UTF-8");

                BufferedReader in = new BufferedReader(reader);

                final String headerLine = in.readLine();
                final String[] headerValues = headerLine.split("\t");
                        for (int column = 0; column < headerValues.length; column++) {
                            tblView.getColumns().add(
                                    createColumn(column, headerValues[column]));
                        }

                // Data:

                String dataLine;
                while ((dataLine = in.readLine()) != null) {
                    final String[] dataValues = dataLine.split("\t");
                            // Add additional columns if necessary:
                            for (int columnIndex = tblView.getColumns().size(); columnIndex < dataValues.length; columnIndex++) {
                                tblView.getColumns().add(createColumn(columnIndex, ""));
                            }
                            // Add data to table:
                            ObservableList<StringProperty> data = FXCollections
                                    .observableArrayList();
                            for (String value : dataValues) {
                                data.add(new SimpleStringProperty(value));
                            }
                            tblView.getItems().add(data);
                }
            } catch (Exception ex) {
                System.out.println("ex: " + ex.toString());
            }


    for(int i=0; i<tblView.getColumns().size(); i++) {
        TableColumn col = (TableColumn)tblView.getColumns().get(i);
        col.setPrefWidth(70);
    }
}

private TableColumn createColumn(
        final int columnIndex, String columnTitle) {
    TableColumn column = new TableColumn(DefaultVars.BLANK_CHARACTER);
    String title;
    if (columnTitle == null || columnTitle.trim().length() == 0) {
        title = "Column " + (columnIndex + 1);
    } else {
        title = columnTitle;
    }


            Callback<TableColumn, TableCell> cellFactory = new Callback<TableColumn, TableCell>() {
                @Override
                public TableCell call(TableColumn p) {

                    System.out.println("event cell");
                    EditingCellData cellExtend = new EditingCellData();
                    return cellExtend;
                }
            };

    column.setText(title);
    column.setCellValueFactory(cellFactory);
    return column;
}

Thanks for your reading.

1
4
10/3/2013 2:54:41 AM

This is the best way to resolve it ==> https://forums.oracle.com/message/11216643#11216643

I'm really thank for your reading about that.

Thanks

0
10/4/2013 3:16:16 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon