java


Dynamic addition of column in TableView and editing cells using javafx


I have changed as per advice of #James_D. I have to add columns dynamically and enter data in cells by hand (not reading a file) using TableView. Other questions at this site and elsewhere nicely demonstrate csv data file reading from file and pasting in the TableView which is not what I want.
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);
}
}
Also I am giving the CommonDataClass as
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;
}
}
Now I can enter data for one row. After that if I press enter then cursor goes to next row while deleting the previous row. I think dataNameProperty is faulty.
Please help.
Thanks

Related Links

closing MQ connection
Java: catching specific Exceptions
How to find where the Startup folder is with Java?
Getting a JavaObject out of a JSObject
Extracting JSP page content
How to validate object serialization mechanism?
Calling MySQL database using Java
Is there an easily hackable open-source Tivo media server?
What is portability? How is java more portable than other languages?
Using the content handler API (JSR 211) to open applications
How do I write superscript word for checkbox text in java?
Is there an option in eclipse like “view servlet” in netbeans for jsp files?
Finding how a large Java code works
Integer won't increment?
Java has a 39G core dump
Increment a Integer's int value?

Categories

HOME
hook
activiti
ngrx
onedrive
google-oauth
maven-3
yum
flyway4
mouse
android-youtube-api
metatrader4
node-pdfkit
jplayer
quickfix
decimal
serverless-framework
tokenize
centos6.5
gitignore
google-cloud-endpoints-v2
y86
stacked
swift3.0.2
ansible-playbook
tasker
sfdc
quadratic-programming
http-live-streaming
node-gyp
skeleton-css-boilerplate
rainbowtable
filepicker
dandelion
jlink
tizen-native-app
mplayer
recursive-datastructures
brightcove
worker-thread
query-performance
smart-table
gnome-shell-extensions
angular-cache
grails-tomcat-plugin
infix-notation
home-directory
dstu2-fhir
clang-static-analyzer
connect-by
testng-dataprovider
pyke
historian
tmuxinator
method-parameters
vstest.console.exe
sortedlist
p2
clipperlib
inmobi
nstableviewcell
websocket4net
elliptic-curve
chronoforms
wsdl-2.0
tinn-r
gdata-api
tridion-worldserver
osql
buildr
whoosh
window-management
hamachi
xmemcached
blitz++
icanhaz.js
jquery-ui-droppable
rescale
ncqrs
thunderbird-lightning
zend-tool
temporal-database
.net-1.0
windows-live-messenger

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App