Select entire column/s with checkbox and display it in a new table - javafx

I'm creating a JavaFX application which enables user to select columns from a ResultSet and display the selected columns to a new table.
I want to select the columns through a check box and it would be better if the whole selected column will be highlighted.
I can do this in Java easily because Java has getSelectedColumns() method but I have no idea on how to do it in JavaFX since I started trying this only few days ago.
in Java, it goes like this:
int[] colIndices2 = table.getSelectedColumns();
int colCount = table.getSelectedColumnCount();
int rowCount = table.getRowCount();
for(int i=0; i<colIndices2.length; i++){
colNames.addElement(table.getColumnName(colIndices2[i]));
}
for(int i=0; i<rowCount; i++){
Vector<Object> row = new Vector<>(colCount);
for (int j=0; j<colIndices2.length; j++){
row.addElement(table.getValueAt(i,colIndices2[j]));
}
colData.addElement(row);
}
DefaultTableModel model = new DefaultTableModel(colData, colNames);
table2.setModel(model);
//table - populated with data from database
//table2 - table containing the selected columns from table1
I want to convert these codes into JavaFX but I'm having a hard time.

This should get you started...
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SelectTest extends Application
{
#Override
public void start(Stage primaryStage)
{
TableView<LineItem> table = new TableView<>();
table.setItems(FXCollections.observableArrayList(new LineItem()));
TableColumn<LineItem, String> column1 = new TableColumn<>("Test1");
column1.setCellValueFactory(cellData -> cellData.getValue().string1Property());
column1.setEditable(true);
CheckBox selectCol1 = new CheckBox();
column1.setGraphic(selectCol1);
column1.setSortable(false);
selectCol1.setOnAction((ActionEvent e) ->
{
selectColumn(column1, selectCol1.isSelected());
table.refresh();
});
table.getColumns().add(column1);
TableColumn<LineItem, String> column2 = new TableColumn<>("Test2");
column2.setCellValueFactory(cellData -> cellData.getValue().string2Property());
column2.setEditable(true);
CheckBox selectCol2 = new CheckBox();
column2.setGraphic(selectCol2);
column2.setSortable(false);
selectCol2.setOnAction((ActionEvent e) ->
{
selectColumn(column2, selectCol2.isSelected());
table.refresh();
});
table.getColumns().add(column2);
TableColumn<LineItem, String> column3 = new TableColumn<>("Test3");
column3.setCellValueFactory(cellData -> cellData.getValue().string3Property());
column3.setEditable(true);
CheckBox selectCol3 = new CheckBox();
column3.setGraphic(selectCol3);
column3.setSortable(false);
selectCol3.setOnAction((ActionEvent e) ->
{
selectColumn(column3, selectCol3.isSelected());
table.refresh();
});
table.getColumns().add(column3);
StackPane root = new StackPane();
root.getChildren().add(table);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
private static void selectColumn(TableColumn column, boolean select)
{
if (select)
{
column.setStyle("-fx-background-color: rgba(3, 169, 244, 0.5)");
} else
{
column.setStyle(null);
}
}

Related

Detecting button click of button added to javafx listview

I am very new to Java so please be patient with me. I have successfully added buttons, labels and even a progress bar to a listview cell. I need to be able to detect when one of the buttons has been clicked. Adding controls to listview content I managed to get from a couple of posts here the code i am using is shown below
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class ListViewDemo extends Application {
public static class lvCell extends VBox {
Label labelName = new Label();
Label labelPath = new Label();
Label labelElapse = new Label();
Button buttonPlayPause = new Button();
Button buttonStop = new Button();
ImageView ivStop = new ImageView();
ImageView ivPlay = new ImageView();
Pane buttonSpacer = new Pane();
Pane progressBarSpacer = new Pane();
HBox hbDetail = new HBox();
HBox hbProgress = new HBox();
ProgressBar pbProgress = new ProgressBar();
File filePlay;
File fileStop;
double prefWidth = 10;
double prefHeight = 10;
lvCell(String labelText) {
super();
labelName.setText(labelText);
labelName.setMaxWidth(Double.MAX_VALUE);
labelPath.setMaxWidth(0);
labelPath.setText("Path");
pbProgress.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(labelName, Priority.ALWAYS);
HBox.setHgrow(pbProgress, Priority.ALWAYS);
HBox.setMargin(labelName, new Insets(5, 0, 0, 0));
HBox.setMargin(pbProgress, new Insets(0, 0, 0, 0));
labelPath.setVisible(false);
buttonSpacer.setPrefSize(prefWidth, prefHeight);
labelElapse.setPrefSize(50, prefHeight);
labelElapse.setText("Time");;
progressBarSpacer.setPrefSize(prefWidth * 6, prefHeight);
filePlay = new File("src/image/play.png");
fileStop = new File("src/image/stop.png");
Image imagePlay = new Image(filePlay.toURI().toString());
Image imageStop = new Image(fileStop.toURI().toString());
ivPlay.setImage(imagePlay);
ivStop.setImage(imageStop);
ivPlay.setFitHeight(prefHeight);
ivPlay.setFitWidth(prefWidth);
ivStop.setFitHeight(prefHeight);
ivStop.setFitWidth(prefWidth);
buttonPlayPause.setGraphic(ivPlay);
buttonStop.setGraphic(ivStop);
buttonPlayPause.setMaxSize(prefWidth, prefHeight);
buttonStop.setMaxSize(prefWidth, prefHeight);
pbProgress.setMaxHeight(2);
pbProgress.setPrefHeight(2);
hbDetail.getChildren().addAll(buttonPlayPause, buttonStop, buttonSpacer, labelName, labelPath);
hbProgress.getChildren().addAll(progressBarSpacer, pbProgress, labelElapse);
this.getChildren().addAll(hbDetail, hbProgress);
}
}
public Parent createContent() {
BorderPane layout = new BorderPane();
List < lvCell > list = new ArrayList < > ();
for (int i = 0; i < 10; i++) {
list.add(new lvCell("Item " + i));
}
ListView < lvCell > listView = new ListView < lvCell > ();
ObservableList < lvCell > myObservableList = FXCollections.observableList(list);
listView.setItems(myObservableList);
layout.setCenter(listView);
return layout;
}
#Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent()));
stage.setWidth(300);
stage.setHeight(200);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
The screen looks like this:
Any help will be greatly appreciated.
Thanks in advance, and wishing you a peaceful journey.
Yas
This is not a class well designed to put into a ListView. An object used as item in a ListView should contain data; the ListCell implementation produced by the cellFactory is responsible for determining the visual representation of the data in the ListView. This way you avoid the creation of nodes for every object reducing the memory footprint, which is exactly what ListView is designed for.
Simplified example
The data here contains just the progress and some text; it's displayed in a ProgressBar and the text of the cell; an additional button in the cell allows increasing the progress by 0.25 for each click and removing any items reaching a progress of 1.
Data class
public class Data {
private final DoubleProperty progress = new SimpleDoubleProperty();
private final String text;
public Data(String text) {
this.text = text;
}
public double getProgress() {
return progress.get();
}
public void setProgress(double value) {
progress.set(value);
}
public String getText() {
return text;
}
public ObservableValue<? extends Number> progressProperty() {
return progress;
}
}
ListView code
ListView<Data> listView = new ListView<>(someData);
listView.setCellFactory(l -> new ListCell<Data>() {
private final ProgressBar progressBar = new ProgressBar();
private final Button button = new Button("increase");
private final HBox content = new HBox(progressBar, button);
{
button.setOnAction(evt -> {
Data item = getItem();
int index = getIndex();
double progress = item.getProgress() + 0.25;
item.setProgress(progress);
if (progress >= 1) {
getListView().getItems().remove(index);
}
});
}
#Override
protected void updateItem(Data item, boolean empty) {
super.updateItem(item, empty);
progressBar.progressProperty().unbind();
if (item == null) {
setGraphic(null);
setText("");
} else {
setGraphic(content);
setText(item.getText());
progressBar.progressProperty().bind(item.progressProperty());
}
}
});

JavaFX8: Problems with TreeView style

I have this wierd problem when trying to CSS style a TreeView.
This TreeView is Filterable to show only the items which I'm looking for.
This is how it looks when the Tree is shown without any filtering
And here's how it looks like, when there's a filtering active
I wanted to have all expanded nodes to have a colored background (green in this example).
So I added a Stylesheet containing the following code
.search-tree .tree-cell:expanded {
-fx-background-color: green;
-fx-text-fill: white;
}
But as you can see, the empty cells in the 2nd image are still colored even there's no content in it.
I also checked the component with ScenicView and indeed the pseudoClassState is still 'expanded'.
Does anyone have a idea why this empty cell is still in this state?
Here's the sample code that I used:
import java.io.File;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TreeTest extends Application {
TreeView<Object> t;
private TextField searchField;
private TreeViewHelper helper;
#Override
public void start(Stage s) throws Exception {
helper = new TreeViewHelper();
t = new TreeView<>();
t.getStyleClass().add("search-tree");
TreeItem rootItem = new TreeItem("");
TreeItem cars = new TreeItem("Cars");
cars.getChildren().addAll(helper.getCars());
cars.setExpanded(true);
TreeItem buses = new TreeItem("Buses");
buses.getChildren().addAll(helper.getBuses());
buses.setExpanded(true);
rootItem.setExpanded(true);
rootItem.getChildren().add(cars);
rootItem.getChildren().add(buses);
t.setRoot(rootItem);
t.setShowRoot(false);
VBox root = new VBox();
root.getChildren().add(t);
searchField = new TextField();
searchField.textProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> prop, String lastValue, String v) {
scanItems();
}
});
root.getChildren().add(searchField);
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(new File("style.css").toURI().toURL().toExternalForm());
s.setScene(scene);
s.show();
}
void scanItems()
{
t.getRoot().getChildren().clear();
TreeItem cars = new TreeItem("Cars");
helper.getCars().stream().filter(e -> e.getValue().toString().toLowerCase().contains(searchField.getText().toLowerCase()))
.forEach(item -> cars.getChildren().add(item));
t.getRoot().getChildren().add(cars);
cars.setExpanded(true);
TreeItem buses = new TreeItem("Buses");
helper.getBuses().stream().filter(e -> e.getValue().toString().toLowerCase().contains(searchField.getText().toLowerCase()))
.forEach(item -> buses.getChildren().add(item));
t.getRoot().getChildren().add(buses);
buses.setExpanded(true);
}
public static void main(String[] args) {
Application.launch( args );
}
class TreeViewHelper
{
public TreeViewHelper()
{
}
// This method creates an ArrayList of TreeItems (Products)
public ArrayList<TreeItem> getProducts()
{
ArrayList<TreeItem> products = new ArrayList<TreeItem>();
TreeItem cars = new TreeItem("Cars");
cars.getChildren().addAll(getCars());
cars.setExpanded(true);
TreeItem buses = new TreeItem("Buses");
buses.getChildren().addAll(getBuses());
buses.setExpanded(true);
products.add(cars);
products.add(buses);
return products;
}
// This method creates an ArrayList of TreeItems (Cars)
ArrayList<TreeItem> getCars()
{
ArrayList<TreeItem> cars = new ArrayList<TreeItem>();
TreeItem ferrari = new TreeItem("Ferrari");
TreeItem porsche = new TreeItem("Porsche");
TreeItem ford = new TreeItem("Ford");
TreeItem mercedes = new TreeItem("Mercedes");
cars.add(ferrari);
cars.add(porsche);
cars.add(ford);
cars.add(mercedes);
return cars;
}
// This method creates an ArrayList of TreeItems (Buses)
ArrayList<TreeItem> getBuses()
{
ArrayList<TreeItem> buses = new ArrayList<TreeItem>();
TreeItem gm = new TreeItem("GM");
TreeItem vw = new TreeItem("VW");
TreeItem man = new TreeItem("MAN");
TreeItem volvo = new TreeItem("Volvo");
buses.add(gm);
buses.add(man);
buses.add(volvo);
buses.add(vw);
return buses;
}
}
}
Edit: I have this issue on MacOSX 10.11 using Java 1.8.0_77b3
I think I found a solution.
By simply adding following style and defining the empty state
.search-tree .tree-cell:empty {
-fx-background-color: transparent;
-fx-text-fill: white;
}
it was working as expected.

change value in combobox ["example"] to "example" in javafx

i am using a combobox to get data from the database on stage.show(), so far i can retrieve the data and also implement my changelistener on the combobox.
the problem is that i am getting ["example"] from the database instead of "example". its my first time of using javafx and don;t know how the output is supposed to look like, but this one is strange to me.
below is a screenshot of it and also my code
[http://i.stack.imgur.com/QWawZ.png]
package libman;
import java.sql.*;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.stage.*;
import javafx.scene.control.*;
import javafx.scene.text.*;
import javafx.collections.*;
/**
/**
*
* #author kels
*/
public class BorrowMenu extends Application {
private ObservableList<ObservableList> data;
DBOperator login;
ResultSet rs;
Statement stat;
private String getval;
#Override
#SuppressWarnings({"static-access", "Convert2Lambda"})
public void start(Stage primaryStage){
//define the UI elements
Label lblID = new Label("Name: ");
Label lblTitle = new Label("Book Title: ");
Label lblauthor = new Label("Author: ");
Label lblissue = new Label("Issue Date: ");
Label lblreturn = new Label("Return Date: ");
ComboBox title = new ComboBox();
TextField txtid = new TextField();
TextField txtitle = new TextField();
TextField txtauthor = new TextField();
TextField txtissue = new TextField();
TextField txtreturn = new TextField();
//set prompt text
txtid.setPromptText("Enter Borrower's Name");
txtitle.setPromptText("Enter Book Title");
txtauthor.setPromptText("Enter Author's Name");
txtissue.setPromptText("Enter Issue Date");
txtreturn.setPromptText("Enter Return Date");
title.setPromptText("Fills books from database");
//ToolTip ttip = new ToolTip("Back Menu");
Button btn = new Button("Borrow Book");
Button btnexit = new Button("Menu>>");
btnexit.setTooltip(new Tooltip("Back to Menu"));
//set the gripane to add in components
GridPane gridpane = new GridPane();
gridpane.setPadding(new Insets(20));
gridpane.setHgap(5);
gridpane.setVgap(5);
//set components
gridpane.setHalignment(lblID, HPos.RIGHT);
gridpane.add(lblID, 0,0);
gridpane.setHalignment(txtid, HPos.RIGHT);
gridpane.add(txtid,1,0);
gridpane.setHalignment(lblTitle, HPos.RIGHT);
gridpane.add(lblTitle, 0,1);
gridpane.setHalignment(title, HPos.RIGHT);
gridpane.add(title, 1,1);
gridpane.setHalignment(lblauthor, HPos.RIGHT);
gridpane.add(lblauthor, 0,2);
gridpane.setHalignment(txtauthor, HPos.RIGHT);
gridpane.add(txtauthor, 1,2);
gridpane.setHalignment(lblissue, HPos.RIGHT);
gridpane.add(lblissue, 0,3);
gridpane.setHalignment(txtissue, HPos.RIGHT);
gridpane.add(txtissue, 1,3);
gridpane.setHalignment(lblreturn, HPos.RIGHT);
gridpane.add(lblreturn, 0,4);
gridpane.setHalignment(txtreturn, HPos.RIGHT);
gridpane.add(txtreturn, 1,4);
gridpane.setHalignment(btn, HPos.RIGHT);
gridpane.add(btn, 1,5);
gridpane.setHalignment(btnexit, HPos.RIGHT);
gridpane.add(btnexit, 2,5);
//display the values from db to combobox on windows launch
data = FXCollections.observableArrayList();
primaryStage.setOnShowing(new EventHandler<WindowEvent>(){
#Override
public void handle(WindowEvent event){
try{
login = new DBOperator();
stat = login.getStatement();
rs=stat.executeQuery("SELECT * FROM BOOKDB");
while(rs.next()){
ObservableList<String> row =FXCollections.observableArrayList();
row.add(rs.getString("Title"));
// row.add(rs.getString("Author"));
data.add(row);
}
title.setItems(data);
rs.close();
}
catch(SQLException ex){
System.out.println("Driver Not Found!!!" + ex);
System.exit(0);
}
}
});
//add the gridpane to the stackpane
StackPane root = new StackPane();
root.getChildren().add(gridpane);
//title.setO
Scene scene = new Scene(root, 380,220);
primaryStage.setTitle("Borrow Menu");
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
//System.out.println(row);
//activate the combo listener at selection
title.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>(){
#Override
public void changed(ObservableValue<? extends Object> observable, Object oldvalue, Object newvalue){
System.out.println(newvalue.toString());
// txtauthor.setText(newvalue.toString());
}
});
}
public static void main(String [] args){
launch(args);
}
}
Please help thanks
You are getting the correct result for what you are actually doing: ObservableList<ObservableList> data bundles two collections.
For each item of the comboBox you will have a collection, so that's why you see "[ ]" when it is rendered.
This minimum sample reproduces your case:
#Override
public void start(Stage primaryStage) {
ComboBox title = new ComboBox();
StackPane root = new StackPane();
root.getChildren().add(title);
ObservableList<ObservableList> data = FXCollections.observableArrayList();
List<String> test = Arrays.asList("Test", "Example");
for(String s : test) {
ObservableList<String> row =FXCollections.observableArrayList();
row.add(s);
data.add(row);
}
title.setItems(data);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
with the same result you have:
You need to simplify your collection: ObservableList<String> data.
#Override
public void start(Stage primaryStage) {
ComboBox<String> title = new ComboBox();
StackPane root = new StackPane();
root.getChildren().add(title);
ObservableList<String> data = FXCollections.observableArrayList();
List<String> test = Arrays.asList("Test", "Example");
for(String s : test) {
data.add(s);
}
title.setItems(data);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
And you will have the expected result:
EDIT
In the case you want to add several items from the database to each item of the combo, you just need to provide a way to render the data, using ComboBox.setCellFactory(), so you can override the default method that produces the string [item1, item2, ... ].

how to keep space between images in scrollpane

I need to space up between images in a scrollpane.I'm adding an imageview and a button to a Vbox.Then that vbox to a gridpane.gridpane is added to scrollpane.However,scrollpane gets overcrowded.I've used gridpane.setPadding(), but no effect.
here's my code:
File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
for (File file1 : filelist1) {
filelist2.add(file1);
}
btnar=new ArrayList<>();
for (int i = 0; i < filelist2.size(); i++) {
downloadbtn=new Button("Download");
btnar.add(downloadbtn);
}
System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(50, 50, 50, 50));
gridpane.setHgap(50);
gridpane.setVgap(50);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridpane.getColumnConstraints().add(columnConstraints);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
image = new Image(filelist2.get(i).toURI().toString());
pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
vb = new VBox();
vb.getChildren().addAll(pic,btnar.get(i));
gridpane.add(vb, imageCol, imageRow);
GridPane.setMargin(pic, new Insets(2,2,2,2));
imageCol++;
// To check if all the 3 images of a row are completed
if (imageCol > 2) {
// Reset Column
imageCol = 0;
// Next Row
imageRow++;
}
}
here' how my stage looks like when images are added
Your code looks fine to me. I have tried your code with a folder filled with images. I wrapped the GridPane on a ScrollPane and gave the ScrollPane a defined size. The space between ImageViews are as per the HGap and VGap provided. Here is a image to second my findings.
Note
If you are looking for gaps between ImageView in a GridPane use setHgap() and setVgap()
setPadding() is used to set the margin around the grid. You can ofcourse use a mix of both as I have done below
Please find this MCVE to support you find your mistake
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LoadingImages extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
ScrollPane scrollPane = new ScrollPane();
GridPane gridpane = new GridPane();
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setPrefSize(500, 500);
scrollPane.setContent(gridpane);
Scene scene = new Scene(scrollPane);
primaryStage.setScene(scene);
primaryStage.show();
File file = new File("Path to folder with images");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
for (File file1 : filelist1) {
filelist2.add(file1);
}
List btnar=new ArrayList<>();
for (int i = 0; i < filelist2.size(); i++) {
Button downloadbtn=new Button("Download");
btnar.add(downloadbtn);
}
System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(50, 50, 50, 50));
gridpane.setHgap(50);
gridpane.setVgap(50);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridpane.getColumnConstraints().add(columnConstraints);
int imageCol = 0;
int imageRow = 0;
for (int i = 0; i < filelist2.size(); i++) {
System.out.println(filelist2.get(i).getName());
Image image = new Image(filelist2.get(i).toURI().toString());
ImageView pic = new ImageView();
pic.setFitWidth(130);
pic.setFitHeight(130);
pic.setImage(image);
VBox vb = new VBox();
vb.getChildren().addAll(pic,(Button)btnar.get(i));
gridpane.add(vb, imageCol, imageRow);
GridPane.setMargin(pic, new Insets(2,2,2,2));
imageCol++;
// To check if all the 3 images of a row are completed
if (imageCol > 2) {
// Reset Column
imageCol = 0;
// Next Row
imageRow++;
}
}
}
public static void main(String[] args) {
launch(args);
}
}

Java "Could Not Serialize the Data"

I'm trying to get my clipboard to receive some custom data in a drag and drop. The custom data is another java type. This other type does implement serializable, so I'm really not sure why this isn't working. Any ideas are appreciated!
imgView.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
ClipboardContent content = new ClipboardContent();
content.put(dataFormat, RHSIconizedToken.this);
Dragboard db = imgView.startDragAndDrop(TransferMode.ANY);
db.setContent(content);
event.consume();
}
});
To retrieve this object later I'm using:
RHSIconizedToken replacementRHSiToken = (RHSIconizedToken) db.getContent(RHSIconizedToken.getDataFormat());
I'm getting the following error, but the RHSIconizedToken does implement Serializable
java.lang.IllegalArgumentException: Could not serialize the data
GetDataFormat returns the DataFormat Object that is used in the put argument in the first code example.
That's because your object is not serializable.
Indeed, it's not because it implements Serializable that it is Serializable.
Look deeper inside the exception, you might find something like this
Caused by: java.io.NotSerializableException: javafx.beans.property.SimpleObjectProperty
Maybe making some fields transient will help.
If your drag object isn't serializable, save it in a global variable during the drag. Here's a JavaFx (Java8 with lambdas) example with draggable tabs that go bewteen panes within the same JVM.
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.DataFormat;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class DraggingTabPane extends Application {
private static final DataFormat TAB_TYPE = new DataFormat("nonserializableObject/tab");
private static Tab dndTab;// global for drag-n-drop of non-serializable type
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
TabPane tabPane1 = createDndTabPane();
TabPane tabPane2 = createDndTabPane();
VBox root = new VBox(10);
root.getChildren().addAll(tabPane1, tabPane2);
final Random rng = new Random();
for (int i=1; i<=8; i++) {
final Tab tab = createDraggableTab("Tab "+i);
final StackPane pane = new StackPane();
int red = rng.nextInt(256);
int green = rng.nextInt(256);
int blue = rng.nextInt(256);
String style = String.format("-fx-background-color: rgb(%d, %d, %d);", red, green, blue);
pane.setStyle(style);
final Label label = new Label("This is tab "+i);
label.setStyle(String.format("-fx-text-fill: rgb(%d, %d, %d);", 256-red, 256-green, 256-blue));
pane.getChildren().add(label);
pane.setMinWidth(600);
pane.setMinHeight(250);
tab.setContent(pane);
if (i<=4) {
tabPane1.getTabs().add(tab);
} else {
tabPane2.getTabs().add(tab);
}
}
primaryStage.setScene(new Scene(root, 600, 600));
primaryStage.show();
}
public TabPane createDndTabPane() {
final TabPane tabPane = new TabPane();
tabPane.setOnDragOver(event -> {
if (event.getDragboard().hasContent(TAB_TYPE)
&& dndTab.getTabPane() != tabPane) {// && different from source location
event.acceptTransferModes(TransferMode.MOVE);
event.consume();
}
});
tabPane.setOnDragDropped(event -> {
if (event.getDragboard().hasContent(TAB_TYPE)
&& dndTab.getTabPane() != tabPane) {// && different from source location
dndTab.getTabPane().getTabs().remove(dndTab);
tabPane.getTabs().add(dndTab);
event.setDropCompleted(true);
event.consume();
}
});
return tabPane;
}
private Tab createDraggableTab(String text) {
final Tab tab = new Tab();
final Label label = new Label(text);
tab.setGraphic(label);
label.setOnDragDetected(event -> {
Dragboard dragboard = label.startDragAndDrop(TransferMode.MOVE);
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.put(TAB_TYPE, 1);
dndTab = tab;
dragboard.setContent(clipboardContent);
event.consume();
});
return tab ;
}
}

Resources