Here is code: Contoller class:
#FXML private TableView<User> table;
#FXML private TableColumn<User,String> view;
method() {.....
` view.setCellValueFactory(
new PropertyValueFactory("DUMMY"));
Callback<TableColumn<User, String>, TableCell<User, String>> cellFactory
= new Callback<TableColumn<User, String>, TableCell<User, String>>() {
public TableCell call( TableColumn<User, String> param) {
final TableCell<User, String> cell = new TableCell<User, String>() {
Button btn = new Button("View");
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
setText(null);
} else {
btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
User user = getTableView().getItems().get(getIndex());
System.out.println(user.username
+ " " + user.name);
}
});
setGraphic(btn);
setText(null);
}
}
};
return cell;
}
};
view.setCellFactory(cellFactory);
//add observablelist
table.getItems().setAll(datas);
}`
I have borrowed code from here: How to add button in JavaFX table view
Indeed I have nothing do view in my View column.
Compiler complains that I cannot use parameterless new PropertyValueFactory<>("DUMMY")) in jdk 1.7
Moreover why I cannot use such expression for button column #FXML private TableColumn view?
I also should note that I changed original code with lambda (possible just in 1.8).
Related
This code has a TavbleView in it, the final column has buttons to act on the column, view & clear. the clear button should only be present or enabled (either would be fine) if the given row is in a known state. Here is the code to always display both buttons, it is coded right now that when the "canClear" property is false, no action is taken:
private void addActionsToTable() {
TableColumn<searchResults, Void> actionColumn = new TableColumn("Action");
actionColumn.setCellFactory(col -> new TableCell<searchResults, Void>() {
private final HBox container;
{
Button viewBtn = new Button("View");
Button clearBtn = new Button("Clear");
viewBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
searchResults data = getTableView().getItems().get(getIndex());
gotoView(data.getLogNo());
}
});
clearBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
searchResults data = getTableView().getItems().get(getIndex());
String logNo = data.getLogNo();
String serialNumber = data.getSerial();
Boolean canClear = data.getCanClear();
if(canClear)
{
// Take action that has been cut for simplicity
}
}
});
container = new HBox(5, viewBtn, clearBtn);
}
#Override
public void updateItem(Void item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
setGraphic(container);
}
}
});
SearchTable.getColumns().add(actionColumn);
actionColumn.setPrefWidth(175);
}
What need to happen so that the Clear button is either disabled or not displayed when data.getCanClear() is false?
Assuming your searchResults (sic) class has a BooleanProperty canClearProperty() method:
actionColumn.setCellFactory(col -> new TableCell<searchResults, Void>() {
private final HBox container;
private final Button clearButton ;
{
Button viewBtn = new Button("View");
clearBtn = new Button("Clear");
viewBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
searchResults data = getTableView().getItems().get(getIndex());
gotoView(data.getLogNo());
}
});
clearBtn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
searchResults data = getTableView().getItems().get(getIndex());
String logNo = data.getLogNo();
String serialNumber = data.getSerial();
Boolean canClear = data.getCanClear();
if(canClear)
{
// Take action that has been cut for simplicity
}
}
});
container = new HBox(5, viewBtn, clearBtn);
}
#Override
public void updateItem(Void item, boolean empty) {
clearButton.disableProperty().unbind();
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
clearButton.disableProperty().bind(
getTableView().getItems().get(getIndex())
.canClearProperty().not());
setGraphic(container);
}
}
});
As the title states, I'm trying to enable/disable a button within a table row based upon a boolean within that table row's data. Here's my code so far:
col.setCellFactory(new Callback<TableColumn<ExampleRow, String>, TableCell<ExampleRow, String>>() {
#Override
public TableCell call(final TableColumn<ExampleRow, String> param){
final Button btn = new Button("Save");
final TableCell<ExampleRow, String> cell = new TableCell<ExampleRow, String>(){
#Override
public void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if(empty){
setGraphic(null);
setText(null);
} else {
btn.setPrefWidth(col.getWidth());
btn.setPadding(Insets.EMPTY);
btn.setOnAction(event -> {
});
setGraphic(btn);
setText(null);
}
}
};
ExampleRow row = (ExampleRow)cell.getTableRow().getItem(); //NPE here
btn.setDisable(!row.hasChanged());
return cell;
}
});
Unfortunately my code breaks on the fifth from the bottom line. If I exclude that line and change the line below to btn.setDisable(true) it works wonderfully. What can I do to disable this button based upon the data in which the button resides?
You aren't using the item anyways, so you could just make it a Boolean and use the value of the changed property. This allows you to enable/disable the button in the updateItem method:
Example:
public static class Item {
private final BooleanProperty changed = new SimpleBooleanProperty();
public final boolean isChanged() {
return this.changed.get();
}
public final void setChanged(boolean value) {
this.changed.set(value);
}
public final BooleanProperty changedProperty() {
return this.changed;
}
}
#Override
public void start(Stage primaryStage) {
TableView<Item> table = new TableView();
table.getItems().addAll(new Item(), new Item(), new Item());
TableColumn<Item, Boolean> column = new TableColumn<>();
column.setCellValueFactory(cd -> cd.getValue().changedProperty());
column.setCellFactory(col -> new TableCell<Item, Boolean>() {
final Button btn = new Button("Save");
{
btn.setOnAction(evt -> {
Item item = (Item) getTableRow().getItem();
item.setChanged(false);
});
}
#Override
protected void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setGraphic(null);
} else {
btn.setDisable(!item);
setGraphic(btn);
}
}
});
table.getColumns().add(column);
Button btn = new Button("change");
btn.setOnAction((ActionEvent event) -> {
Item item = table.getSelectionModel().getSelectedItem();
if (item != null) {
item.setChanged(true);
}
});
VBox root = new VBox(btn, table);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
BTW: TableView uses the cellFactory to create the cells. The item, table and tableRow properties are updated later. Therefore retrieving any of those values in the cellFactory's call method itself makes no sense, since none of those values have been assigned at that time.
I am using an ObservableList to control the items and every time I delete an item, the TableView removes it from the datasource. But the view is not being updated as I'm still seeing all the items. The only difference is that the removed item can not be selected anymore.
Similar problem: javafx listview and treeview controls are not repainted correctly
In the following the code:
final TableColumn<TMachineType, String> testerType = new TableColumn<TMachineType, String>(
bundle.getString("table.testerType"));
testerType
.setCellValueFactory(new PropertyValueFactory<TMachineType, String>(
"testerType"));
testerType
.setCellFactory(new Callback<TableColumn<TMachineType, String>, TableCell<TMachineType, String>>() {
#Override
public TableCell<TMachineType, String> call(
final TableColumn<TMachineType, String> param) {
final TableCell<TMachineType, String> cell = new TableCell<TMachineType, String>() {
#Override
protected void updateItem(final String item,
final boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if (empty || item == null) {
setGraphic(null);
setText(null);
}
if (!empty) {
final TMachineType row = (TMachineType) getTableRow().getItem();
textProperty().bind(
row.testerTypeProperty());
}
}
};
return cell;
}
});
TMachineType class:
private final SimpleStringProperty testerType = new SimpleStringProperty();
#Column(name = "TESTER_TYPE")
public String getTesterType() {
return testerType.get();
}
public void setTesterType(String testerType) {
this.testerType.set(testerType);
}
public StringProperty testerTypeProperty() {
return testerType;
}
Observable List:
1. DB entities:
final Query q = em.createQuery("SELECT t FROM TMachineType t");
final List resultList = q.getResultList();
2. Obs. list setup:
ObservableList<TMachineType> observableList;
...
observableList = FXCollections.observableArrayList(resultList);
tableMachineType.setItems(observableList);
FXCollections.sort(observableList);
Row removal:
#FXML
private void handleOnRemove(final ActionEvent event) {
final ObservableList<TMachineType> selectedIndices = tableMachineType
.getSelectionModel().getSelectedItems();
final String infoTxt = selectedIndices.size() + " "
+ bundle.getString("message.records_removed");
final List<TMachineType> deleteBuffer = new ArrayList<TMachineType>();
deleteBuffer.addAll(selectedIndices);
for (final TMachineType selectedIdx : deleteBuffer) {
selectedIdx.manufacturerProperty().unbind();
selectedIdx.testerTypeProperty().unbind();
deleted.add(selectedIdx);
observableList.remove(selectedIdx);
// tableMachineType.getItems().remove(selectedIdx);
}
..
}
removing the cellFactory fix this problem ! YEHU :)
testerType
.setCellFactory(new Callback<TableColumn<TMachineType, String>, TableCell<TMachineType, String>>() {
#Override
public TableCell<TMachineType, String> call(
final TableColumn<TMachineType, String> param) {
final TableCell<TMachineType, String> cell = new TableCell<TMachineType, String>() {
#Override
protected void updateItem(final String item,
final boolean empty) {
super.updateItem(item, empty);
textProperty().unbind();
if (empty || item == null) {
setGraphic(null);
setText(null);
}
if (!empty) {
final TMachineType row = (TMachineType) getTableRow().getItem();
textProperty().bind(
row.testerTypeProperty());
}
}
};
return cell;
}
});
I creating project for my job. I need to use tableview and user need to check data going to database.
I was found how to add checkbox to TableView from this post
https://stackoverflow.com/a/7973514/3037869
This is working for me but my TableView now looking
How to fix this checkbox spam?
My code
public class ProductPSController extends BorderPane{
#FXML public TableColumn<ProductPS, String> produkt;
#FXML public TableColumn<ProductPS, String> symbol;
#FXML public TableColumn<ProductPS, String> atrybuty;
#FXML public TableColumn<ProductPS, Integer> id;
#FXML public TableColumn<ProductPS, Integer> stock;
#FXML public TableColumn<ProductPS, Integer> count;
#FXML public TableColumn<ProductPS, Integer> price;
#FXML public TableColumn<ProductPS, Boolean> checkbox;
#FXML public TableView <ProductPS> tab;
#FXML public BorderPane produkty;
public ObservableList<ProductPS> data = FXCollections.observableArrayList();
public ProductPSController()
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("product.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
id.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
stock.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
produkt.prefWidthProperty().bind(tab.widthProperty().multiply(0.20));
symbol.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
atrybuty.prefWidthProperty().bind(tab.widthProperty().multiply(0.30));
count.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
price.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
tab.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
setProduct();
}
public void setProduct()
{
id.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id"));
symbol.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("symbol"));
stock.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id_stock"));
produkt.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("product_name"));
atrybuty.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("attributes"));
count.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("count"));
Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>> booleanCellFactory =
new Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>>() {
#Override
public TableCell<ProductPS, Boolean> call(TableColumn<ProductPS, Boolean> p) {
return new BooleanCell();
}
};
price.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("price"));
checkbox.setCellValueFactory(new PropertyValueFactory<ProductPS, Boolean>("checkbox"));
checkbox.setCellFactory(booleanCellFactory);
checkbox.setEditable(true);
try(Connection c = MysqlConnect.getConnection())
{
String SQL = "";
ResultSet rs = c.createStatement().executeQuery(SQL);
while(rs.next()){
data.add(new ProductPS(rs.getInt("id_product"),rs.getInt("id_stock_available"),rs.getString("name"),rs.getString("atrybuty"),rs.getInt("quantity"),rs.getFloat("price"),rs.getString("reference")));
}
c.close();
} catch (SQLException e) {
System.out.println(e.toString());
// TODO Auto-generated catch block
}
for(int i=0; i<data.size(); i++) {
if(i+1<data.size() && data.get(i).getAttributes().length()==0 && data.get(i).getId()==data.get(i+1).getId() ){data.remove(i);}
}
tab.setItems(data);
}
class BooleanCell extends TableCell<ProductPS, Boolean> {
private CheckBox checkBox;
public BooleanCell() {
checkBox = new CheckBox();
checkBox.setDisable(false);
checkBox.selectedProperty().addListener(new ChangeListener<Boolean> () {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(isEditing())
{
commitEdit(newValue == null ? false : newValue);
}
}
});
this.setGraphic(checkBox);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}
#Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
checkBox.setDisable(false);
checkBox.requestFocus();
}
#Override
public void cancelEdit() {
super.cancelEdit();
checkBox.setDisable(true);
}
public void commitEdit(Boolean value) {
super.commitEdit(value);
checkBox.setDisable(true);
}
#Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
checkBox.setSelected(item);
}
}
}
}
Thx for help and say what i done bad.
In BooleanCell, you need to set the graphic to checkBox if the cell is not empty, and set it to null if it is empty.
Remove the line
this.setGraphic(checkBox);
from BooleanCell's constructor, and change updateItem(...) to:
#Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}
After messing around with Netbeans and Scenebuilder for a while I'm stuck at a problem I can't quite understand. I use a custom cellfactory to bind a doubleclick event to the cells in my tableview. But when I set the cellfactory and a cellValueFactory only the custom cellFactory has an effect.
I'm trying to populate a tableview with data from a number of objects and bind a double click event to the cells of the first column. Populating is not the problem, I just used
idNumber.setCellValueFactory(new PropertyValueFactory<LiveStock, String>("idNumber"));
status.setCellValueFactory(new PropertyValueFactory<LiveStock, String>("status"));
I then googled around to figure out how to bind a doubleclick event to the cells of the table and found javafx, TableView: detect a doubleclick on a cell
amongst others...
I defined a custom cellFactory like this:
Callback<TableColumn<LiveStock, String>, TableCell<LiveStock, String>> cellFactory =
new Callback<TableColumn<LiveStock, String>, TableCell<LiveStock, String>>() {
#Override
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<LiveStock, String>() {};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 2) {
System.out.println("double clicked!");
TableCell c = (TableCell) event.getSource();
System.out.println("Livestock ID: " + c.getId());
}
}
});
return cell;
}
I removed the update and toString methods just to see if they where the reason I ran in to problems.
So I tried
idNumber.setCellFactory(cellFactory);
idNumber.setCellValueFactory(new PropertyValueFactory<LiveStock, String>("idNumber"));
This results in my cells beeing empty, but having the double click binding
any ideas?
My LiveStock class looks like this:
package projekt1.fx;
import javafx.beans.property.SimpleStringProperty;
public class LiveStock {
private final int idNumber;
private final SimpleStringProperty ownerID;
private SimpleStringProperty status;
private double lat;
private double longd;
public LiveStock(int idNumber, String ownerID) {
this.idNumber = idNumber;
this.ownerID = new SimpleStringProperty(ownerID);
this.status = new SimpleStringProperty("ok");
}
public int getIdNumber() {
return this.idNumber;
}
// public void setIdNumber(int number) {
// this.idNumber = number;
// }
public String getOwnerID(){
return ownerID.get();
}
public void setOwnerID(String id){
ownerID.set(id);
}
public String getStatus(){
return status.get();
}
public void setStatus(String st){
status.set(st);
}
}
The cellfactory now looks like this:
Callback<TableColumn<LiveStock, String>, TableCell<LiveStock, String>> cellFactory =
new Callback<TableColumn<LiveStock, String>, TableCell<LiveStock, String>>() {
#Override
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<LiveStock, String>() {
#Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// setText("HELLO WORLD!");
setText(empty ? null : getString());
}
};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() == 2) {
System.out.println("double clicked!");
TableCell c = (TableCell) event.getSource();
System.out.println("Livestock ID: " + c.getId());
togglePopup(null);
}
}
});
return cell;
}
};
Documentation of Cell API says:
Because by far the most common use case for cells is to show text to a
user, this use case is specially optimized for within Cell. This is
done by Cell extending from Labeled. This means that subclasses of
Cell need only set the text property, rather than create a separate
Label and set that within the Cell. ...
The current source code of Cell constructor sets the text to null:
public Cell() {
setText(null);
...
}
The subclass IndexedCell and sub-subclass TableCell, both of them don't set the text of Labeled.
The text is set by default cell factory of TableColumn in source code.
public static final Callback<TableColumn<?,?>, TableCell<?,?>> DEFAULT_CELL_FACTORY = new Callback<TableColumn<?,?>, TableCell<?,?>>() {
#Override public TableCell<?,?> call(TableColumn<?,?> param) {
return new TableCell() {
#Override protected void updateItem(Object item, boolean empty) {
if (item == getItem()) return;
super.updateItem(item, empty);
if (item == null) {
super.setText(null);
super.setGraphic(null);
} else if (item instanceof Node) {
super.setText(null);
super.setGraphic((Node)item);
} else {
super.setText(item.toString());
super.setGraphic(null);
}
}
};
}
};
However by defining your own cell factory that creates new TableCell but does not set the text in its overriden updateItem() method, will be resulting an empty (=null) column cell text. So yes the reason of the problem was removing updateItem method, which calls setText(...) internally.
EDIT:
Specify the generic types explicitly for TableColumns as,
TableColumn<LiveStock, Integer> idNumber = new TableColumn<LiveStock, Integer>("ID No");
This will avoid type mismatches or wrong type castings.
Then the cell factory callback for your use case will be
Callback<TableColumn<LiveStock, Integer>, TableCell<LiveStock, Integer>> cellFactory =
new Callback<TableColumn<LiveStock, Integer>, TableCell<LiveStock, Integer>>() {
public TableCell<LiveStock, Integer> call(TableColumn<LiveStock, Integer> p) {
TableCell<LiveStock, Integer> cell = new TableCell<LiveStock, Integer>() {
#Override
public void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
setText((item == null || empty) ? null : item.toString());
setGraphic(null);
}
};
cell.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getClickCount() > 1) {
System.out.println("double clicked!");
TableCell c = (TableCell) event.getSource();
System.out.println("Cell text: " + c.getText());
}
}
});
return cell;
}
};
What is changed?
The type of idNumber in LiveStock is int. By defining new TableColumn<LiveStock, Integer> we say this is a column from LiveStock row for its attribute idNumber which has a type int, but the generic types must be a reference type, it cannot be TableCell<LiveStock, int> so we define TableCell<LiveStock, Integer>. The thumb of rule: row item class's attribute type should match the second generic type parameter of TableColumn and due to this the parameter of TableCell also.
getString method is defined in the referenced answer link mentioned by you. But it is just a user defined method, not mandatory to use it.