GWT CELLTABLE column's cell value updation - java

I am using gwt2.3
My celltable contains 10 rows, 5 columns.
All cells in 1st row is empty,editable.
Whenever user clicks on column cell lets say 1st row X 3rd column
then user will edit that cell say "xyz".
after that when user click on button: "update column cell" then xyz value set to all cell present in that column.
I am using different celltype in celltable.
How to set/update all cell value in that particular column/page whose 1st cell is edited
Any help or guidance in this matter would be appreciated

Create a FieldUpdater in order to push back the changes to your Domain object. Then in the onClick callback of your button update your List with the values from the first row.
For example for an arbitrary TextInputColumn which takes a MyDTO class (can be any domain object) as the value type you can define following FieldUpdater:
myColumn.setFieldUpdater(new FieldUpdater() {
#Override
public void update(int index, MyDTO object, String value) {
// Push the changes into the MyDTO. At this point, you could send an
// asynchronous request to the server to update the database.
object.someField = value;
// Redraw the table with the new data.
table.redraw();
}
});
You have to set such a FieldUpdater for all 5 columns. (someField is the field in your DTO which you want to update).
Now in the onClick() callback of the button you have to update the actual list. Looks something like that:
update_column_cell.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
//Supose listDataProvider is the instance of your DataSource for your CellTable
List<MyDTO> list = listDataProvider.getList();
// get cell values for the first row (this is for one cell)
newSomeField = list.get(0).someField;
newSomeField2 = list.get(0).someField2;
for (int i = 1;i<list.size();i++) {
MyDTO dto = list.get(i);
if (newSomeField != null && newSomeField.isNotEmpty()) {
dto.someField = newSomeField;
}
if (newSomeField2 != null && newSomeField2.isNotEmpty()) {
dto.someField2 = newSomeField2;
}
}
}
})
This example only handles two fields of your DTO. You will probably have to extend it to cover all 5 fields you are showing as columns in your CellTable

Related

JavaFX How to get all values and sum of specific column from Table View by button?

I have a problem with adding functionality to my program. I'm using JavaFX and Table View. I don't know how can I implement a method witch will get data from specific column.
I try to implement solution from this said:
JavaFX How to get all values of one column from TableView?
But I don't understand exactly how should I send initialized TableColumn class to method totalPaidSaving
And: How to get selected TableCell in JavaFX TableView
I Also found a couple of different solutions using getSelectionModel().getSelectedCells() but in this case I need to use button and some sort of code witch will get data from specific column.
Fragment of MainControl class. Place where the method is called :
addButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Operation operation = new Operation();
operation.addRecord(dateTextField, distanceTextField, lpgAmountTextField, lpgPriceTextField,
petrolAmountTextField, petrolPriceTextField, paidLabel, savingLabel, gasEfficiencyLabel,
contentTable, totalSavingsLabel);
operation.totalPaidSaving(contentTable, totalSavingsLabel);
}
});
A fragment of the configuration of my TableView also in the MainControl class.
#SuppressWarnings("unchecked")
public void configurateTable() {
TableColumn<GasRecords, String> savingColumn = new TableColumn<GasRecords, String>(SAVING_COLUMN);
savingColumn.setCellValueFactory(new PropertyValueFactory<>("saving"));
contentTable.getColumns().addAll(dateColumn, distanceColumn, lpgAmountColumn, lpgPriceColumn, petAmountColumn,
petPriceColumn, paidColumn, savingColumn, gasEfficiencyColumn);
}
A fragment of Operation class:
public void totalPaidSaving(TableView<GasRecords> cT, Label tSL) {
String totalSavingValue = "0";
//the code that calculates the sum of the values ​​of a particular column
tSL.setText(String.format("%.2f zł", totalSavingValue));
}
I know that using String instead of double is bad but for now I would like to solve it in this form.
Link of visualization of me application and example of column i want to sum.
https://zapodaj.net/d8fc7ece3f629.jpg.html
My goal is to, after press the "Add" button application shows me the sum of all savings rows from saving column in specific label. Rest of application is working.
In this ways you can calculate the values of a column. you can get cell value by the index of the column. you have to use contentTable.getColumns().get(7).getCellObservableValue(i).getValue() get the cell value of that column Savings.
int totalSavingValue=0;
for (int i= 0;i<tblDispatchHistory.getItems().size();i++){
total = total+Integer.valueOf(String.valueOf(contentTable.getColumns().get(7).getCellObservableValue(i).getValue()));
}
System.out.println(totalSavingValue);
tSL.setText(totalSavingValue);

Detecting Cell Click On A Table Row That Is Already Selected

I have a JFrame application that contains a JTabel. The underlying table model is my own class that I made that extends AbstractTableModel. The data in the first column is of type boolean and so I want it to show the value in the form of a checkbox. To do this, I added the following code in my table model getColumnClass method:
#Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 0) {
return Boolean.class;
} else if (columnIndex == 1) {
....
....
}
This works fine and my table cell looks like the following which is what I want:
The behavior I now want is that when the user clicks on a cell in column 0 of any row I want to toggle the value of On Sale.
My main class listens to selections in the table by implementing ListSelectionListener and overriding the valueChanged method. Inside the valueChanged method I can check the selected column by calling table.getSelectedColumn and checking to see if it's 0 then getting the object at the selected row and toggling the boolean on sale value.
The problem I am running into is that if a particular row is already selected and the user clicks the cell in column 0 for the selected row the valueChanged method is never called and so therefore I never toggle the value.
My quesion is, how do I detect a cell selection/click on a row that is already selected? Should I use a mouse selection listener then determine the cell that was clicked using the location data from the mouse click event?
Thank you.
Have you tried listening to table change instead ? You would need to implement TableModelListener and to have this :
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
TableModel model = (TableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column);
...// Do something with the data...
}

GWT DataGrid row selection

I have a GWT DataGrid with a multi-selection model and check-boxes to show selection/select/deselect rows. That's all well and good.
But, I also want to have a second, independent selection model. If a user double-clicks on a row, I want to handle that event, and for the event handler to know which row was double-clicked. The double-clicking should not affect the check-box selection.
I tried this:
final SelectionModel<MyRecord> selectionModel = new MultiSelectionModel...
//Yes I need a MultiSelectionModel
dataGrid.addDomHandler(new DoubleClickHandler() {
public void onDoubleClick(DoubleClickEvent event) {
selectionModel.get??? //no suitable getter for double-clicked
}
}, DoubleClickEvent.getType());
But ran into a dead-end when I found now way to get the double-clicked row in the event handler. One way would be to register both a Multi- and Single- selection model, but doubt DataGrid will support that.
Neither can I work out how to get the clicked row from the DoubleClickEvent object.
I have implemented a button cell with a FieldUpdater. This works, but it's not ideal.
Any suggestions?
If I understand correctly you want to get the index of the row.
You could do it like this: (this way you'll get the "Real" index)
AbstractSelectionModel<T> selectionModel = (AbstractSelectionModel<T>)dataGrid.getSelectionModel();
ArrayList<Integer> intList = new ArrayList<Integer>();
List<Row> list = (List<Row>)_dataProvider.getList();
int i = 0;
for(Row row : list)
{
if( selectionModel.isSelected(row) )
intList.add(i);
i++;
}
UPDATE:
To get only the current row you could do that:
datagrid.getKeyboardSelectedRow()
I'm 3 years too late to the party, but I think the more correct solution would be:
dataGrid.addCellPreviewHandler(new CellPreviewEvent.Handler<YOUR_MODEL_TYPE>() {
#Override
public void onCellPreview(final CellPreviewEvent<YOUR_MODEL_TYPE> event) {
if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
int row = event.getIndex();
doStuff(row); // do whatever you need the row index for
}
}
});

Get TableCell content for SQL query

I'm writing an SQL CRUD application and stuck on the delete button problem. I have a table with 4 columns and a delete button in the fifth. To make an SQL query I need to get info from these 4 columns. I can get a row number and a column name, how to get cell content on row/column intersection?
Assuming the fifth column has a custom cell factory that puts the delete button in its custom TableCell, this delete button can own an action like:
btnDelete.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
param.getTableView().getSelectionModel().select(getIndex());
Person item = personTable.getSelectionModel().getSelectedItem();
if (item != null) {
// Logic of deleting current record here
System.out.println(item.getName());
}
}
});
The full sscce, Putting button into the table column more elegantly.

GWT GXT get all the values from a selected row?

I am using GXt GWt and am trying to get the values from a selected row in a tree grid, but can't seem to get it working..
E.g. if the user clicks row, i want to be able to get the values for all the columns in that row.
cm = new ColumnModel(createColumnConfig());
// Create grids based on data (held in stores), and attach listeners for when a row is clicked
final TreeGrid<Build> nightlyResultsGrid = createTreeGrid(nightlyResultsStore);
nightlyResultsGrid.addListener(Events.RowClick, new Listener<BaseEvent>()
{
public void handleEvent(BaseEvent clickEvent)
{
GridEvent gridEvent = (GridEvent)clickEvent;
gridEvent.getModel().get("Platform").toString();
gridEvent.getModel().get("Date").toString();
gridEvent.getModel().get("Build").toString();
}
}
The code above keeps generating an exception..
15:58:09.861 [ERROR] [dashboard] Uncaught exception escaped
java.lang.NullPointerException: null
at com.rory.mep.ui.metrics.client.UserInterface$3.handleEvent(UserInterface.java:109)
at com.extjs.gxt.ui.client.event.BaseObservable.callListener(BaseObservable.java:178)
at com.extjs.gxt.ui.client.event.BaseObservable.fireEvent(BaseObservable.java:86)
at com.extjs.gxt.ui.client.widget.Component.fireEvent(Component.java:456)
at com.extjs.gxt.ui.client.widget.grid.Grid.onClick(Grid.java:811)
at com.extjs.gxt.ui.client.widget.treegrid.TreeGrid.onClick(TreeGrid.java:745)
Any help is much appreciated.
Be sure to read the Javadocs for events when you use them. From Grid's docs:
RowClick : GridEvent(grid, rowIndex, cellIndex, event)
Fires after a row is clicked.
grid : this
rowIndex : the row index
cellIndex : cell index
index : the cell index
event : the dom event
The model isn't available, so getModel() is returning null. Instead, use the rowIndex with the store to get the model.

Categories