I am trying to display three images in sequence with some scale transformation to each of them(images of 1,2,3). I am using ScaleTransition on single instance of imageview, after each successive transformation the imageView height get scaled which is not resetting
My Code is
public void start(Stage primaryStage) {
final IntegerProperty intProperty = new SimpleIntegerProperty(0);
final Image[] array = {
(new Image(WaitEffect.class.getResource("1.png")
.toExternalForm())),
(new Image(WaitEffect.class.getResource("2.png")
.toExternalForm())),
(new Image(WaitEffect.class.getResource("3.png")
.toExternalForm())) };
Group root = new Group();
Scene scene = new Scene(root, 400, 400, Color.BLACK);
StackPane pane = StackPaneBuilder.create()
.layoutX(scene.getWidth() / 2).layoutY(scene.getHeight() / 2)
.build();
final ImageView imageView = new ImageView();
pane.getChildren().add(imageView);
Timeline timeline = new Timeline();
int count = 0;
KeyValue value = new KeyValue(intProperty, count++);
KeyFrame frame = new KeyFrame(Duration.millis(2000),
new EventHandler<ActionEvent>() {
int co = 0;
#Override
public void handle(ActionEvent paramT) {
imageView.setImage(array[co]);
ScaleTransition st = ScaleTransitionBuilder.create()
.byX(2).byY(2).node(imageView)
.duration(Duration.millis(500)).build();
st.play();
imageView.setFitHeight(150);
imageView.setFitWidth(150);
imageView.resize(150, 150);
co++;
}
}, value);
timeline.getKeyFrames().add(frame);
timeline.setCycleCount(3);
timeline.play();
root.getChildren().addAll(pane);
primaryStage.setScene(scene);
primaryStage.show();
}`
ImageView doesn't reset it's properties after setting new Image. You need to reset it yourself, e.g. by setting from properties of ScaleTransition:
ScaleTransition st = ScaleTransitionBuilder.create()
.fromX(1).fromY(1)
.byX(2).byY(2).node(imageView)
.duration(Duration.millis(500)).build();
Related
in attachments i have put an image of my project , in essence I want to draw in a canvas different kind of shape like a line,circle,rectangle ,triangle ecc when i click the relative button, I'm new on the javafx programming , and for now what i have understand it is that the canvas can only modify in the main Thread of the GUI ,and this is the problem , because when i handle the button event "SetonAction" i need a reference of the main canvas to draw line or cirlce ecc. I have try with a new Thread but without success.
If anyone Know a way to do this , i really appricieted.Thanks in adviceenter image description here
For me it was difficult resolve this issue, so I have done a little simplyfy code to understand how the things works , take a look to this:
public class TE1 extends Application {
private Pane root;
private Pane left;
private StackPane right;
private StackPane drawcontainer;
private CustomizedButton btn;
private CustomizedButton btn1;
private Canvas Drawtable;
private SplitPane divisor;
private double x;
private double y;
private double to_x = 0;
private double to_y = 0;
private int line_no = 1;
#Override
public void start(Stage primaryStage) {
root = new StackPane();
Drawtable = new Canvas(400,400);
Drawtable.setWidth(400);
Drawtable.setHeight(400);
GraphicsContext gc = Drawtable.getGraphicsContext2D();
gc.setFill(Color.CADETBLUE);
gc.fillRect(0, 0, 800, 850);
left = new Pane();
left.setMinSize(400,400);
left.setStyle("-fx-background-color:#d7d6d5;");
right= new StackPane();
right.setAlignment(Pos.CENTER);
// right.setStyle("-fx-background-color: #FFFFFF;");
right.setMinSize(400, 400);
divisor = new SplitPane();
//root.addEventFilter(DrawEvent.DRAW_TYPE,new DrawEventHandler());
//root.addEventHandler(DrawEvent.DRAW_TYPE,new DrawEventHandler());
Drawtable.addEventFilter(DrawEvent.DRAW_TYPE,new DrawEventHandler());
// right.getChildren().add(Drawtable);
btn = new CustomizedButton();
btn1= new CustomizedButton();
btn.Set_identity("linea");
btn1.setMinSize(50,50);
btn.setMinSize(50,50);
btn.setMaxSize(50, 50);
btn.setLayoutX(100);
btn.setLayoutY(100);
btn1.setLayoutX(100);
btn1.setLayoutY(180);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
this.fireEvent();
}
public void fireEvent()
{
Runnable task = ()-> runTask();
Thread background = new Thread(task);
background.setDaemon(true);
background.start();
}
public void runTask()
{
while(btn.isFocused()){
System.out.println("SONO ATTIVO ");
Drawtable.setOnMousePressed((event) ->setFromPos(event));
System.out.println("x vale :"+x+"y vale: "+y);
Drawtable.setOnMouseDragged((event)->
{
right.getChildren().remove(0);
Canvas temp_canvas = new Canvas(400, 400);
GraphicsContext gc = temp_canvas.getGraphicsContext2D();
gc.setFill(Color.BLUEVIOLET);
// setToPos(event);
// drawLine(gc);
right.getChildren().add(0,temp_canvas);
});
Drawtable.setOnMouseReleased((event) -> {
final Canvas new_line = new Canvas(400, 400);
final GraphicsContext gc = new_line.getGraphicsContext2D();
setToPos(event);
drawLine(gc);
//final new stright line
right.getChildren().add(line_no++,new_line);
});
}
}
});
right.getChildren().addAll( new Canvas(), Drawtable);
left.getChildren().add(btn);
left.getChildren().add(btn1);
divisor.getItems().addAll(left,right);
root.getChildren().addAll(divisor);
Scene scene = new Scene(root, 800, 850);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void setFromPos(MouseEvent event) {
this.x = event.getX();
this.y = event.getY();
}
private void setToPos(MouseEvent event) {
this.to_x = event.getX();
this.to_y = event.getY();
}
private void drawLine(GraphicsContext gc) {
gc.setFill(Color.RED);
gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(x, y, to_x, to_y);
}
}
Thanks in advice
Using multi-threading here is not necessary and not a good idea either.
Registering event handlers is not a long-running operation and reassigning anonymus classes with the same logic over and over again won't change a thing. Furthermore those modifications need to happen from the JavaFX application thread.
Another issue that should be mentioned is that you're recreating the Canvas during the mouse gesture. You should avoid doing this and instead reuse the existing Canvas. Stacking one Canvas per line should also be avoided. If you need a canvas for the currently drawn line and one for the completed lines that's 2 Canvases. Using Lines would be simpler to implement btw.
Also I recommend ToggleButton for this kind of task. This allows you to access a state independend of the focus. Instead of querying the state repeatedly you should listen to the property (whether you're using selected or focused to add/remove listeners):
private Pane root;
private Pane left;
private StackPane right;
private Button btn1;
private Canvas Drawtable;
private Canvas drawingCanvas;
private GraphicsContext drawingContext;
private SplitPane divisor;
#Override
public void start(Stage primaryStage) {
root = new StackPane();
Drawtable = new Canvas(400, 400);
Drawtable.setWidth(400);
Drawtable.setHeight(400);
// second Canvas for drawing stacked on top of the other canvas
drawingCanvas = new Canvas(Drawtable.getWidth(), Drawtable.getHeight());
drawingContext = drawingCanvas.getGraphicsContext2D();
left = new Pane();
left.setMinSize(400, 400);
left.setStyle("-fx-background-color:#d7d6d5;");
right = new StackPane(Drawtable, drawingCanvas);
right.setAlignment(Pos.CENTER);
right.setStyle("-fx-background-color: #FFFFFF;");
right.setMinSize(400, 400);
divisor = new SplitPane();
ToggleButton btn = new ToggleButton();
btn1 = new Button();
btn.setText("linea");
btn1.setMinSize(50, 50);
btn.setMinSize(50, 50);
btn.setMaxSize(50, 50);
btn.setLayoutX(100);
btn.setLayoutY(100);
btn1.setLayoutX(100);
btn1.setLayoutY(180);
btn.selectedProperty().addListener(new ChangeListener<Boolean>() {
private double x;
private double y;
private void drawLine(GraphicsContext gc, double endX, double endY) {
gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(x, y, endX, endY);
}
private final EventHandler<MouseEvent> pressedHandler = (event) -> {
x = event.getX();
y = event.getY();
};
private final EventHandler<MouseEvent> draggedHandler = (event) -> {
// remove old content & draw new content
drawingContext.clearRect(0, 0, drawingCanvas.getWidth(), drawingCanvas.getHeight());
drawLine(drawingContext, event.getX(), event.getY());
};
private final EventHandler<MouseEvent> releasedHandler = (event) -> {
// clear canvas for line drawing
drawingContext.clearRect(0, 0, drawingCanvas.getWidth(), drawingCanvas.getHeight());
// draw line on background canvas
final GraphicsContext gc = Drawtable.getGraphicsContext2D();
drawLine(gc, event.getX(), event.getY());
};
#Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
// add/remove event handlers based on toggle state of the button
if (newValue) {
drawingCanvas.addEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler);
drawingCanvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, draggedHandler);
drawingCanvas.addEventHandler(MouseEvent.MOUSE_RELEASED, releasedHandler);
} else {
drawingCanvas.removeEventHandler(MouseEvent.MOUSE_PRESSED, pressedHandler);
drawingCanvas.removeEventHandler(MouseEvent.MOUSE_DRAGGED, draggedHandler);
drawingCanvas.removeEventHandler(MouseEvent.MOUSE_RELEASED, releasedHandler);
}
}
});
left.getChildren().add(btn);
left.getChildren().add(btn1);
divisor.getItems().addAll(left, right);
root.getChildren().addAll(divisor);
Scene scene = new Scene(root, 800, 850);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
i have succesfully in some way , i have modify the code like this:
public class TE1 extends Application {
private Pane root;
private Pane left;
private StackPane right;
private StackPane drawcontainer;
private CustomizedButton btn;
private CustomizedButton btn1;
private Canvas Drawtable;
private SplitPane divisor;
private double x;
private double y;
private double to_x = 0;
private double to_y = 0;
private int line_no = 1;
private Thread background;
#Override
public void start(Stage primaryStage) {
root = new StackPane();
Drawtable = new Canvas(400,400);
Drawtable.setWidth(400);
Drawtable.setHeight(400);
GraphicsContext gc = Drawtable.getGraphicsContext2D();
left = new Pane();
left.setMinSize(400,400);
left.setStyle("-fx-background-color:#d7d6d5;");
right= new StackPane();
right.setAlignment(Pos.CENTER);
right.setStyle("-fx-background-color: #FFFFFF;");
right.setMinSize(400, 400);
divisor = new SplitPane();
btn = new CustomizedButton();
btn1= new CustomizedButton();
btn.Set_identity("linea");
btn1.setMinSize(50,50);
btn.setMinSize(50,50);
btn.setMaxSize(50, 50);
btn.setLayoutX(100);
btn.setLayoutY(100);
btn1.setLayoutX(100);
btn1.setLayoutY(180);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
this.fireEvent();
}
public void fireEvent()
{
Runnable task = ()-> runTask();
Thread background = new Thread(task);
background.setDaemon(true);
background.start();
}
public void runTask()
{
while(btn.isFocused()){
System.out.println("SONO ATTIVO ");
Drawtable.setOnMousePressed((event) ->setFromPos(event));
System.out.println("x vale :"+x+"y vale: "+y);
Drawtable.setOnMouseDragged((event)->
{
right.getChildren().remove(0);
Canvas temp_canvas = new Canvas(400, 400);
GraphicsContext gc = temp_canvas.getGraphicsContext2D();
setToPos(event);
drawLine(gc);
right.getChildren().add(0,temp_canvas);
});
Drawtable.setOnMouseReleased((event) -> {
final Canvas new_line = new Canvas(400, 400);
final GraphicsContext gc = new_line.getGraphicsContext2D();
setToPos(event);
drawLine(gc);
//final new stright line
right.getChildren().add(line_no++,new_line);
});
}
System.out.println("Focused btn state :"+btn.isFocused());
background.interrupt();
}
});
right.getChildren().addAll( new Canvas(), Drawtable);
left.getChildren().add(btn);
left.getChildren().add(btn1);
divisor.getItems().addAll(left,right);
root.getChildren().addAll(divisor);
Scene scene = new Scene(root, 800, 850);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void setFromPos(MouseEvent event) {
this.x = event.getX();
this.y = event.getY();
}
private void setToPos(MouseEvent event) {
this.to_x = event.getX();
this.to_y = event.getY();
}
private void drawLine(GraphicsContext gc) {
gc.setFill(Color.RED);
gc.setStroke(Color.BLACK);
gc.setLineWidth(1);
gc.strokeLine(x, y, to_x, to_y);
}
}
but now i can't understand the reason that when the button isn't focused dosen't stop
draw line on the canvas..any suggestion??
Issue:
I needed to implement Spinner for RTL languages such that text is aligned to right and down arrow towards left.I am able to acheive this alignment but the arrow is pointing upwards.
I do not want to use setRotation since it's available only from API level 11.
PFB the code I am using:
//Creating spinner
Spinner s=new RTLSpinner(this);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataAdapter);
//Fetching the background drawable of Spinner
NinePatchDrawable drawable=(NinePatchDrawable)(((StateListDrawable)s.getBackground()).getCurrent());
//Converting it into Bitmap
Bitmap bmpOriginal = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpOriginal);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
//Rotating the image
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(180, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
s.setBackgroundDrawable(new BitmapDrawable(bmResult));
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int textViewResourceId,
List<String> textArray) {
super(context, textViewResourceId, textArray);
}
public TextView getView(int position, View convertView, ViewGroup parent) {
TextView viewObj = (TextView) super.getView(position, convertView,
parent);
TextView optionView = (TextView) viewObj
.findViewById(android.R.id.text1);
optionView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
return viewObj;
}
}
Thanks in Advance!
On my JavaFX table, when I click on a row, it selects that row. Now when I click for the second time on same row which was previously selected, I want to deselect that particular row. Is it possible ? Please share some example code, if its possible.
Below piece of code worked for this requirement.
tableView.setRowFactory(new Callback<TableView<Person>, TableRow<Person>>() {
#Override
public TableRow<Person> call(TableView<Person> tableView2) {
final TableRow<Person> row = new TableRow<>();
row.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
final int index = row.getIndex();
if (index >= 0 && index < tableView.getItems().size() && tableView.getSelectionModel().isSelected(index) ) {
tableView.getSelectionModel().clearSelection();
event.consume();
}
}
});
return row;
}
});
Used the same Person class from oracle's table view example. Original answer was given by #James_D in oracle's forum.
Basically you can choose anything invalid as the index. Generally -1 is preferred
table.getSelectionModel().select(-1);
which calls the int select. Alternative:
table.getSelectionModel().select(null);
which calls the object select
if you want to see the whole code used/confirm for this
public class Main extends Application {
#SuppressWarnings("unchecked")
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
TableView<Person> table = new TableView<Person>();
stage.setTitle("Table View Sample");
stage.setWidth(300);
stage.setHeight(500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("Test Name");
firstNameCol.setCellValueFactory(new PropertyValueFactory<Person, String>("name"));
table.getColumns().addAll(firstNameCol);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table);
table.itemsProperty().get().add(new Person("Hans"));
table.itemsProperty().get().add(new Person("Dieter"));
((Group) scene.getRoot()).getChildren().addAll(vbox);
table.getSelectionModel().select(-1);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
public class Person {
final StringProperty name = new SimpleStringProperty();
Person(String name) {
this.name.set(name);
}
public StringProperty nameProperty() { return this.name; }
}
}
I was wondering if JavaFX included a way to make the accordion or titled panes horizontal. I can't find anything, but I thought I should ask. Essentially, the end goal is to have a sidebar that can expand to reveal a tree view. Here are pictures of my intention:
Collapsed
Expanded
There is no standard horizontal orientation TitledPane in JavaFX 2.2.
You can create a feature request for one in the JavaFX issue tracker.
Implementing your own horizontal TitledPane is pretty easy.
Here is a demo of a similar thing just using animation on a standard Pane.
Further explanations of the techniques involved are in Sai's blog post: Sliding in JavaFX (It’s all about clipping).
/** Animates a node on and off screen to the left. */
class SideBar extends VBox {
/** #return a control button to hide and show the sidebar */
public Button getControlButton() { return controlButton; }
private final Button controlButton;
/** creates a sidebar containing a vertical alignment of the given nodes */
SideBar(final double expandedWidth, Node... nodes) {
getStyleClass().add("sidebar");
this.setPrefWidth(expandedWidth);
// create a bar to hide and show.
setAlignment(Pos.CENTER);
getChildren().addAll(nodes);
// create a button to hide and show the sidebar.
controlButton = new Button("Collapse");
controlButton.getStyleClass().add("hide-left");
// apply the animations when the button is pressed.
controlButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
// create an animation to hide sidebar.
final Animation hideSidebar = new Transition() {
{ setCycleDuration(Duration.millis(250)); }
protected void interpolate(double frac) {
final double curWidth = expandedWidth * (1.0 - frac);
setPrefWidth(curWidth);
setTranslateX(-expandedWidth + curWidth);
}
};
hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
setVisible(false);
controlButton.setText("Show");
controlButton.getStyleClass().remove("hide-left");
controlButton.getStyleClass().add("show-right");
}
});
// create an animation to show a sidebar.
final Animation showSidebar = new Transition() {
{ setCycleDuration(Duration.millis(250)); }
protected void interpolate(double frac) {
final double curWidth = expandedWidth * frac;
setPrefWidth(curWidth);
setTranslateX(-expandedWidth + curWidth);
}
};
showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent actionEvent) {
controlButton.setText("Collapse");
controlButton.getStyleClass().add("hide-left");
controlButton.getStyleClass().remove("show-right");
}
});
if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED) {
if (isVisible()) {
hideSidebar.play();
} else {
setVisible(true);
showSidebar.play();
}
}
}
});
}
}
there you go:
#Override
public void start(Stage stage) throws Exception {
Label label1 = new Label("label 1");
label1.setRotate(90);
TitledPane pane1 = new TitledPane("titled pane 1", label1);
pane1.setAlignment(Pos.CENTER);
Label label2 = new Label("label 2");
label2.setRotate(90);
TitledPane pane2 = new TitledPane("titled pane 2", label2);
pane2.setAlignment(Pos.CENTER);
Accordion accordion = new Accordion();
accordion.setRotate(270);
accordion.getPanes().add(pane1);
accordion.getPanes().add(pane2);
HBox mainPane = new HBox(accordion);
accordion.prefWidthProperty().bind(mainPane.heightProperty());
accordion.prefHeightProperty().bind(mainPane.widthProperty());
stage.setTitle("Horizontal Accordion");
stage.setScene(new Scene(mainPane, 800, 600));
stage.show();
}
Perhaps JavaFx don't provide horizontal TitledPane, but what you can do is rotate your TitledPane to 90 degree and rotate the node which you want to set in it's content to 270 degree, and you are done.
Here is a code sample for you.
TitledPane titledPane = new TitledPane();
BorderPane borderPane = new BorderPane();
borderPane.setCenter(new Label("My Label")); //Or your tree view
borderPane.setRotate(270);
titledPane .setContent(borderPane);
Just add following line to accordion and your done.
accordion.setRotate(270);
I have used a tableView in JavaFX2.2. I have a column where I have kept values which are updated when the user clicks on a button. These values are dynamically getting populated and till this part its working fine. However, when I scroll down the table to see the other values in the table, the cell data changes. Can you please suggest what I need to do to get this problem resolved?
Here is the code for the table cell that I am dynamically populating and is getting changed on scrolling down the table.
Callback<TableColumn, TableCell> cellFactoryField = new Callback<TableColumn, TableCell>() {
#Override
public TableCell call(final TableColumn param) {
final Button button = new Button("Select Field");
final TableCell cell = new TableCell() {
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
// label.setText("Here");
if (empty) {
// System.out.println("table cell inside updateitem = "+item);
// setGraphic(null);
}
else
{
}
}
};
button.setOnAction(new EventHandler<ActionEvent>() {
private CheckBoxTreeItem<String> checkBoxTreeItem;
private CheckBoxTreeItem<String> nodeFieldName;
private CheckBoxTreeItem<String> nodeFieldName2;
private CheckBoxTreeItem<String> nodeFieldName3;
private Stage stage = new Stage();
#Override public void handle(ActionEvent e)
{
CheckBoxTreeItem<String> rootItem =
new CheckBoxTreeItem<String>("Tables");
rootItem.setExpanded(true);
final TreeView tree = new TreeView(rootItem);
tree.setEditable(true);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
{
checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample Table" );//+ (i+1));
rootItem.getChildren().add(checkBoxTreeItem);
nodeFieldName = new CheckBoxTreeItem<String>("Field Name1");
nodeFieldName2 = new CheckBoxTreeItem<String>("Field Name2");
nodeFieldName3 = new CheckBoxTreeItem<String>("Field Name3");
checkBoxTreeItem.getChildren().addAll(nodeFieldName, nodeFieldName2, nodeFieldName3);
}
tree.setRoot(rootItem);
tree.setShowRoot(true);
StackPane root = new StackPane();
root.getChildren().add(tree);
Button selectButton = new Button("Select");
HBox hbox = new HBox();
hbox.getChildren().add(selectButton);
hbox.setAlignment(Pos.CENTER);
selectButton.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent t) {
final ArrayList<String> selectedValues = new ArrayList<String>();
// System.out.println("Selected tree items are : ");
if(checkBoxTreeItem.isSelected())
selectedValues.add(checkBoxTreeItem.getValue());
if(nodeFieldName.isSelected())
selectedValues.add(nodeFieldName.getValue());
if(nodeFieldName2.isSelected())
selectedValues.add(nodeFieldName2.getValue());
if(nodeFieldName3.isSelected())
selectedValues.add(nodeFieldName3.getValue());
stage.hide();
for(int i = 0; i<selectedValues.size();i++)
{
if(i == selectedValues.size()-1)
selectedVals += selectedValues.get(i);
else
selectedVals += selectedValues.get(i)+",";
}
fieldNameChosen = true;
if(fieldNameChosen)
cell.setGraphic(new Label(selectedVals));
else
cell.setGraphic(button);
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(root);
borderPane.setBottom(hbox);
stage.setScene(new Scene(new Group(borderPane)));
stage.show();
}
});
if(!(cell.getGraphic() instanceof Label))
cell.setGraphic(button);
return cell;
}
};
fieldName.setCellFactory(cellFactoryField);
I am getting a similar problem for another field where I need to show values from another table dynamically. Below is the code I have used.
final int k = 0;
value.setCellValueFactory(new Callback<CellDataFeatures<ObservableList, String>, ObservableValue<String>>() {
int noOfDataCells = k;
public ObservableValue<String> call(CellDataFeatures<ObservableList, String> param)
{
TableColumn column = param.getTableColumn();
int size = 0;
if(tableView1.getItems()!=null)
size = ((ObservableList) tableView1.getItems().get(0)).size();
String valueFromData = "";
if(noOfDataCells<size)
{
valueFromData = String.valueOf(((ObservableList) tableView1.getItems().get(0)).get(noOfDataCells));
}
else if(noOfDataCells == size)
{
noOfDataCells = 0;
valueFromData = String.valueOf(((ObservableList) tableView1.getItems().get(0)).get(noOfDataCells));
}
else if (noOfDataCells>size)
{
valueFromData = "";
}
noOfDataCells++;
//TODO SET THE VALUE IN THE MODEL
// ((MetaTag) column.getTableView().getItems().get(
// .getIndex())).setFieldName(selectedVals);
return new SimpleStringProperty(valueFromData);
}
});
There are next problem:
You are using table editing functionality without implementing required API
You update cells in your factory, but not a data by which Table is backed up by
Cells are being destroyed on scroll out (for performance reason) and recreated back, so all your changes become destroyed.
See next tutorial on correct approach to TableView editing: http://docs.oracle.com/javafx/2/ui_controls/table-view.htm
Also I've created a small program which uses your TreeCheckBox stage to update Table by double-click on cells. I've marked with comments most important places.
public class TableCellEditing extends Application {
private void init(Stage primaryStage) {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 400, 200));
// you didn't provided data which your Tables use so example will work with Person class
final ObservableList<Person> data = FXCollections.observableArrayList(
new Person("click to edit", "Smith"),
new Person("", "Johnson"),
new Person("", "Williams1"),
new Person("", "Williams2"),
new Person("", "Williams3"),
new Person("", "Williams4"),
new Person("", "Williams5"),
new Person("", "Jones"),
new Person("", "Brown"),
new Person("", "Brown2"));
TableView tableView = new TableView();
tableView.setItems(data);
// make table editable
tableView.setEditable(true);
TableColumn lastNameCol = new TableColumn();
lastNameCol.setText("Last");
lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
TableColumn firstNameCol = new TableColumn();
firstNameCol.setText("First");
// here you connect data list with table column
firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
// here you specify that your cells are special and provide editing hooks
firstNameCol.setCellFactory(new Callback<TableColumn, TableCell>() {
#Override
public TableCell call(final TableColumn param) {
final TableCell cell = new TableCell() {
#Override
// this method is called on editable tables by double click
public void startEdit() {
super.startEdit();
// here we create new Stage to select items from tree
new CheckBoxTreeStage(this).show();
}
#Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
if (isEditing()) {
setText(null);
} else {
// this is the place where we update data by chosen value
setText(getItem().toString());
setGraphic(null);
}
}
}
};
return cell;
}
});
tableView.getColumns().addAll(firstNameCol, lastNameCol);
tableView.setFocusTraversable(false);
root.getChildren().add(tableView);
}
// I've extracted your stage to a separate class for better readability
private static class CheckBoxTreeStage extends Stage {
private CheckBoxTreeItem<String> checkBoxTreeItem;
private CheckBoxTreeItem<String> nodeFieldName;
private CheckBoxTreeItem<String> nodeFieldName2;
private CheckBoxTreeItem<String> nodeFieldName3;
public CheckBoxTreeStage(final TableCell cell) {
CheckBoxTreeItem<String> rootItem =
new CheckBoxTreeItem<String>("Tables");
rootItem.setExpanded(true);
final TreeView tree = new TreeView(rootItem);
tree.setEditable(true);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
{
checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample Table");//+ (i+1));
rootItem.getChildren().add(checkBoxTreeItem);
nodeFieldName = new CheckBoxTreeItem<String>("Field Name1");
nodeFieldName2 = new CheckBoxTreeItem<String>("Field Name2");
nodeFieldName3 = new CheckBoxTreeItem<String>("Field Name3");
checkBoxTreeItem.getChildren().addAll(nodeFieldName, nodeFieldName2, nodeFieldName3);
}
tree.setRoot(rootItem);
tree.setShowRoot(true);
StackPane root = new StackPane();
root.getChildren().add(tree);
Button selectButton = new Button("Select");
HBox hbox = new HBox();
hbox.getChildren().add(selectButton);
hbox.setAlignment(Pos.CENTER);
selectButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
final ArrayList<String> selectedValues = new ArrayList<String>();
// System.out.println("Selected tree items are : ");
if (checkBoxTreeItem.isSelected()) {
selectedValues.add(checkBoxTreeItem.getValue());
}
if (nodeFieldName.isSelected()) {
selectedValues.add(nodeFieldName.getValue());
}
if (nodeFieldName2.isSelected()) {
selectedValues.add(nodeFieldName2.getValue());
}
if (nodeFieldName3.isSelected()) {
selectedValues.add(nodeFieldName3.getValue());
}
hide();
String selectedVals = "";
for (int i = 0; i < selectedValues.size(); i++) {
if (i == selectedValues.size() - 1) {
selectedVals += selectedValues.get(i);
} else {
selectedVals += selectedValues.get(i) + ",";
}
}
boolean fieldNameChosen = true;
if (fieldNameChosen) {
cell.commitEdit(selectedVals);
} else {
cell.cancelEdit();
}
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(root);
borderPane.setBottom(hbox);
setScene(new Scene(new Group(borderPane)));
}
};
public static class Person {
private StringProperty firstName;
private StringProperty lastName;
private Person(String fName, String lName) {
this.firstName = new SimpleStringProperty(fName);
this.lastName = new SimpleStringProperty(lName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public StringProperty lastNameProperty() {
return lastName;
}
}
#Override
public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}