How to update table cells with new information using JavaFX? - java

Whenever I click a row, I want to update the table with new information. For some reason when I click it is not doing anything and errors out. Any idea why I can't do this?
public HierarchyItemTreeCellCallback(TreeView<Requirement> treeView, DataModel dataModelIn)
{
super();
treeViewParent = treeView;
dataModel = dataModelIn;
}
#Override
public TreeCell<Requirement> call(TreeView<Requirement> param)
{
TreeCell<Requirement> cell = new HierarchyItemTreeCell();
cell.addEventFilter(MouseEvent.MOUSE_PRESSED,new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
HierarchyTabController hierarchyTabController = new HierarchyTabController(dataModel);
Requirement req1 = new Requirement("ID", "21", 1, REQUIREMENT_TYPE.SOFTWARE);
hierarchyTabController.updateTableRow(req1);
}
});
return cell;
}
Here is the controller logic:
public void updateTableRow(Requirement r)
{
REQUIREMENT_TYPE reqType = REQUIREMENT_TYPE.SOFTWARE;
Requirement req1 = new Requirement("ID", "21", 1, reqType);
ObservableList<Requirement> list = FXCollections.observableArrayList(req1);
name.setCellValueFactory(new PropertyValueFactory<>("name"));
description.setCellValueFactory(new PropertyValueFactory<>("description"));
tableView.getItems().setAll(list);
tableView.setItems(list);
}
This works whenever I do not call the updateTableRow function from the MouseEvent. However, it is not working when I try to call the controller and update the values from the MouseEvent. Any idea what I am doing wrong?

Related

Check/Uncheck CheckboxCell, onclick of a row - GWT

I have a cell table with the first column as checkboxes. My checkboxes have to be checked or unchecked when there is any single click event on the entire row. This is the following code for creating a MultiSelectionModel, creating CheckboxCell and creating a column for cell table.
MultiSelectionModel<Object> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<Object,Boolean> selectedCol = new Column<Object, Boolean>(selectedCell){
#Override
public Boolean getValue(Object object) {
return object.isSelected();
}
};
table.addColumn(selectedCol);
//Single Click Event to Enable/Disable checkbox.
table.addDomHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
Set<Object> selectedItems = selectionModel.getSelectedSet();
for (Object s : selectedItems) {
Window.alert(String.valueOf(s));
selectionModel.setSelected(s, true);
}
}
}, ClickEvent.getType());
I tried to mark a row as checked using "selectionModel.setSelected(s, true)". But it isn’t working, when I clicked on row, the corresponding checkbox is not being checked.
My question is how do I enable/disable checkboxes onclick of an entire row. Is my approach correct. Or Is there any other way to perform this action in GWT.
You are very close to the working solution.
In the selectedCell you should return the value depending on selectionModel:
return selectionModel.isSelected(object);
This way you are using default multi selection model that selects rows by clicking on them. And the checkbox value comes from the selection model. That's it.
See the working example below:
CellTable<String> table = new CellTable<String>();
final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);
CheckboxCell selectedCell = new CheckboxCell();
Column<String, Boolean> selectedCol = new Column<String, Boolean>(selectedCell) {
#Override
public Boolean getValue(String object) {
// return object.isSelected();
return selectionModel.isSelected(object);
}
};
table.addColumn(selectedCol);
table.addColumn(new TextColumn<String>() {
#Override
public String getValue(String object) {
return object;
}
});
List<String> values = new ArrayList<>();
for(int i = 0; i < 10; i++)
values.add("Line " + (i + 1));
table.setRowData(values);
You can use standard Ctrl and Shift keys to control selection.

Wicket, how to get data line index number

When a user clicks on the edit link in a wicket data grid component, a new window would open and they would modify whatever appears on the new form. In the datagrid, there are many rows. How can I get the index number of the row I want to edit? Please see below, the onclick event of the “edit” link.
columnList.add(new AbstractLinkColumn<MyModel>(new Model<String>("Edit")) {
#Override
protected AbstractLink newLink(String componentId, final IModel<MyModel> rowModel) {
return new AjaxLink<String>(componentId, getDisplayModel()) {
#Override
public void onClick(AjaxRequestTarget target) {
ModalWindow myModelWindow = requestForm.getmyModelWindow();
MyPanel panel = new MyPanel(myModelWindow.getContentId(), requestForm
.getModelObject(), myModelWindow, rowModel.getObject(), false, isSetReadOnly);
myModelWindow.setContent(panel);
Ricola.refresh(target, panel);
myModelWindow.show(target);
}
};
}
#Override
protected Label newLinkLabel(String componentId, IModel<MyModel> rowModel) {
return new Label(componentId, getDisplayModel());
}
}.setTooltip(new Model<String>("Click the Edit link to edit the line")));
public class MyColumn extends AbstractColumn {
#Override
public void populateItem(Item item, String componentId, IModel model) {
Item rowItem = item.findParent(Item.class);
int rowIndex = rowItem.getIndex();
...
}
}

JavaFX TableView Context Menu Item shows up when run on Java 8 until update 5 but not on later ones

The code below runs as its name says on Java 8 update 5 but not on later ones:
public class TableViewShowingOnlyAnAppendContextMenuItemIfRowIsEmptyElseDeleteIsIncluded extends Application {
private final TableView<Name> table = new TableView<>();
private final ObservableList<Name> data = FXCollections.observableArrayList(new Name("Jacob"),
new Name("Isabella"), new Name("Ethan"), new Name("Emma"), new Name("Michael"));
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
TableColumn<Name, String> column = new TableColumn<>("Name");
column.setCellValueFactory(new PropertyValueFactory<>("name"));
table.getColumns().add(column);
table.setItems(data);
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().add(new MenuItem("append"));
table.setContextMenu(contextMenu);
table.setRowFactory(tableView -> {
TableRow<Name> row = new TableRow<>();
row.contextMenuProperty().bind(
Bindings.when(Bindings.isNotNull(row.itemProperty()))
.then(showOnlyAppendContextMenuItemIfRowIsEmptyElseIncludeDelete())
.otherwise((ContextMenu) null));
return row;
});
Scene scene = new Scene(table);
stage.setScene(scene);
stage.show();
}
private ContextMenu showOnlyAppendContextMenuItemIfRowIsEmptyElseIncludeDelete() {
ContextMenu rowMenu = new ContextMenu();
ContextMenu tableMenu = table.getContextMenu();
if (tableMenu != null)
rowMenu.getItems().addAll(tableMenu.getItems());
rowMenu.getItems().add(new MenuItem("delete"));
return rowMenu;
}
public static class Name {
private final SimpleStringProperty name;
private Name(String name) {
this.name = new SimpleStringProperty(name);
}
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
} }
Can help me find the error in the code? Or if there is none, is this a regression that should be submitted? As of now, all the PCs in use have 8u5.
Thanks in advance.
This code looks like something I may have posted a while back...
The issue is that using a MenuItem in multiple menus is not really supported. While this isn't explicitly stated in the Javadocs, the fact that MenuItem has a getMenu() method does imply this. The fact that it worked prior to 8u5 is really just luck...
The fix is to create new menu items that are copies of the ones in the table's context menu:
private ContextMenu showOnlyAppendContextMenuItemIfRowIsEmptyElseIncludeDelete() {
ContextMenu rowMenu = new ContextMenu();
ContextMenu tableMenu = table.getContextMenu();
if (tableMenu != null) {
for (MenuItem item : tableMenu.getItems()) {
MenuItem rowItem = new MenuItem(item.getText());
rowItem.setGraphic(item.getGraphic());
rowItem.setOnAction(item.getOnAction());
rowMenu.getItems().add(rowItem);
}
}
rowMenu.getItems().add(new MenuItem("delete"));
return rowMenu;
}
Another approach is to use ControlsFX Actions, so you can maintain a (single) list of actions for the whole table and generate menu items from them for both the table and the rows.

"Cannot find symbol constructor, EventHandler does not take parameters" when setting the action for a button in JavaFX

I am creating a program in JavaFX which lists tasks from a data base and displays a button for each row with allows a user to register the task as claimed in the database. I used the code on this link to help me with the buttons for each row: https://gist.github.com/jewelsea/3081826.
However, after changing the code to fit my program, i am getting an error in relation to setting the action of the cellButton variable. I have also added the class that calls this method, just in case.
CANNOT FIND SYMBOL CONSTRUCTOR, EVENTHANDLER DOES NOT TAKE PARAMETERS..
& METHOD DOES NOT OVERRIDE OR IMPLEMENT A METHOD FROM A SUPERTYPE (I am guessing this error is as a result of the first two errors).
//Define the button cell
private class ButtonCell extends TableCell<task, Boolean> {
final Button cellButton = new Button("Claim");
ButtonCell(){
//Action when the button is pressed
cellButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
// get Selected Item
task currentPerson = (task) ButtonCell.this.getTableView().getItems().get(ButtonCell.this.getIndex());
//remove selected item from the table list
newMan.claimTask(currentPerson.getTaskID());
}
});
}
//Display button if the row is not empty
#Override
protected void updateItem(Boolean t, boolean empty) {
super.updateItem(t, empty);
if(!empty){
setGraphic(cellButton);
}
}
}
x
private TableView createTasksTable() {
TableView tableView = new TableView();
TableColumn<task,String> firstNameCol = new TableColumn<>("Task");
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<task, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<task, String> p) {
// p.getValue() returns the Person instance for a particular TableView row
return new SimpleStringProperty(p.getValue().getName());
}
});
//Insert Button
TableColumn col_action = new TableColumn<>("Action");
tableView.getColumns().add(col_action);
col_action.setCellValueFactory(
new Callback<TableColumn.CellDataFeatures<task, Boolean>,
ObservableValue<Boolean>>() {
#Override
public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<task, Boolean> p) {
return new SimpleBooleanProperty(p.getValue() != null);
}
});
//Adding the Button to the cell
col_action.setCellFactory(
new Callback<TableColumn<task, Boolean>, TableCell<task, Boolean>>() {
#Override
public TableCell<task, Boolean> call(TableColumn<task, Boolean> p) {
return new ButtonCell();
} });
tableView.getColumns().addAll(firstNameCol);
return tableView;
}
You have probably imported the wrong EventHandler. Make sure you have
import javafx.event.EventHandler ;
and not something from awt.

How to get values of grid in GXT?

List<ColumnConfig<Vo, ?>> l = new ArrayList<ColumnConfig<Vo, ?>>();
l.add(numColumn);
l.add(subjectColumn);
l.add(nameColumn);
l.add(dateColumn);
ColumnModel<Vo> cm = new ColumnModel<Vo>(l);
Grid<Vo> grid = new Grid<Vo>(store, cm) {
#Override
protected void onAfterFirstAttach() {
super.onAfterFirstAttach();
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
loader.load();
}
});
}
};
grid.addCellClickHandler(new CellClickHandler() {
#Override
public void onCellClick(CellClickEvent event) {
// TODO Auto-generated method stub
contentPanel.clear();
contentPanel.add(readPanel(contentPanel));
}
});`
When I click on cell, I want to get the data in the cell corresponding.
The current state,
When you click on of the cell, switch to a different view of the structure.
And I succeeded to connect to the database.
However, I want to get the data of cell or row.
How to get values of grid in GXT?
(example Site:http://www.sencha.com/examples/#ExamplePlace:paginggrid)
GXT Grid works with data stores, more precisely it is a ListStore I think. So that, to get Values of the grid either use that store by grid.getStore(), and after that you basically have a collection of the objects in your grid (grid.getStore().getAll() return List), or you can use Grid's SelectionModel to deal with the grid selected item like this:
grid.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<Vo>() {
#Override
public void onSelectionChanged(SelectionChangedEvent<Vo> event) {
if (grid.getSelectionModel().getSelectedItem() != null) {
// Do whatever you want
} else {
}
}
});
I hope it will help.
If you want to get the value of a single cell you can try this inside the cellClickHandler :-
ListGridRecord record = event.getRecord();
int colNum = event.getColNum();
String fieldName=grid.getFieldName(colNum);
String cellValue=record.getAttribute(fieldName);
cellValue will have the desired value.

Categories

Resources