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

ArrayOutOfBounds exception when trying to parse Double to String
Can methods be referred to as functions in comments [closed]
Positioning button on the TableActivity from the code
Infinispan Clustering applications on 2 servers
How to use a random array number and a timer in java?
How can I write a method to intersperse two arrays of different lengths?
Returning null values in DAO file
Using foreach loop in JSF
How to send message using SMACK API and GTALK?
LDAP search bootstrap example
Unable To Increase UrlImageView Size! (Android)
How to make custom ListView to open other activities when clicking list item?
Executing PHPUnit_Selenium
Array in android [closed]
Prevent instantiate of specific appender
Finding Odd Numbers of Divisors

Categories

HOME
compiler-construction
jdo
vbscript
blogger
netbeans
mfc
layout
spring-cloud-stream
paw-app
electronics
upload
awesome-wm
metatrader4
apache-cayenne
leiningen
hex-editors
vaadin7
transformation
interop
amazonsellercentral
tokenize
tar
chromium-embedded
icloud-api
internet-explorer-9
subdomains
quote
tasklet
stormpath
create-table
perlin-noise
openoffice.org
language-concepts
mixture-model
sharefile
ssjs
gulp-sourcemaps
isbn
password-encryption
dotcover
rainbowtable
zip4j
errordocument
paxos
domain-model
mu
knockout-components
dtexec
cudafy.net
flow-control
query-performance
objective-c-swift-bridge
abcpdf9
firmata
url-masking
autorest
wapiti
tmuxinator
browser-bugs
tableau-online
event-bubbling
unity5.2.3
p2
directoryservices
nsmutabledictionary
rake-task
tween
java.util.concurrent
phalanger
typeof
oracle-warehouse-builder
mbr
c18
farseer
ms-project-server-2010
jplaton
cos
back-stack
html4
comaddin
drools-planner
bitsharp
onsubmit
qtkit
uiviewanimation-curve
enter
ohm
code-cleanup
punbb
subscript
php-parser
pydot
infobox
psi
f#-powerpack
cinema-4d
wise
mozilla-prism
firefox-5
zend-tool
defensive-programming
mdac
multiple-languages
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