I have a table phases that has values that reference to a repository table. In the repository each value has an order number.
Normally in SQL, I would join them and then order by the order column. But if I want to do that in Vaadin I have to use the FreeFormQuery to make this join.
The Problem is that I want to allow users to change values in the table phases. So I would need to implement the FreeFormQueryDelegate. Is there a way to make this work by using a standard TableQuery instead of a FreeFormQuery ?
Maybe manually move the rows in a table ?
Vaadin offers some sort options for tables.
You can sort one specific propertyId or you can sort a set of properties.
// sort one container property Id
table.setSortContainerPropertyId(propertyId);
table.setSortAscending(ascending);
table.sort();
// sort several property Ids
table.sort(propertyId[], ascending[]);
NOTE: the code above is no proper code, it rather shows the method bodies.
I used follow code to rearrange the vaadin table rows, when "Move Up" button click, while selecting a table row.
Object selectedItemId = fieldTable.getValue();
if(selectedItemId != null){
List<Object> items = new ArrayList<>(fieldTable.getItemIds());
int index = items.indexOf(selectedItemId);
if(index < 1){
LOGGER.info("Selected Filed is at the top, cannot move up further");
return;
}
//Rearrange itemids for new order
Object upperRow= items.set(index - 1, selectedItemId);
items.set(index, upperRow);
items.forEach(itemId -> {
//Gets the properties of old table item
Item item = fieldTable.getItem(itemId);
String name = (String) item.getItemProperty(Constants.PROPERTYID_FIELD_NAME).getValue();
Button toggleBtn = (Button) item.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).getValue();
//Remove the old table item
fieldTable.removeItem(itemId);
//Insert old table item into the new position index
Item newItem = fieldTable.addItem(itemId);
newItem.getItemProperty(Constants.PROPERTYID_FIELD_NAME).setValue(name);
newItem.getItemProperty(Constants.PROPERTYID_SORT_BUTTON).setValue(toggleBtn);
int position = items.indexOf(itemId);
if(position == index - 1) {
fieldTable.select(itemId);
}
});
}
Related
I am currently using a JTable to display a few patient details. I have a JCheckBox, that when ticked adds a new column to the table and adds the data to the new column. When it is unticked, it should remove the column, sort of like adding extra filters to the table. However, when I tick the box again after unticking it, it duplicates the columns in the table.
I tried to use the fireTableStructureChanged() method, but it would never update the table and so the columns stay there even if the checkbox was unticked. But if I remove it, then when I untick the the checkbox it works, but then the duplication problem comes back. I pasted my actionPerformed() method for when the checkbox is ticked. The first image is of my table before I click my Checkbox. The second is when I click the Checkbox and the column is added. Lastly the third is when I untick the checkbox and tick again. Any help will be much appreciated.
#Override
public void actionPerformed(ActionEvent e)
{
if (gui.getChkCholesterol().isSelected() == true)
{
try {
gui.getRightTableModel().addColumn("Cholesterol");
newColumnIndeces.put("2093-3",gui.getRightTable().getColumnCount()-1);
gui.getRightTableModel().addColumn("Cholesterol Effective Date/Time");
newColumnIndeces.put("2093-3e",gui.getRightTable().getColumnCount()-1);
if(gui.getRightTable().getRowCount()>0)
{
int columnNumberValue = newColumnIndeces.get("2093-3");
int columnNumberDate = newColumnIndeces.get("2093-3e");
for (int i = 0; i<gui.getRightTable().getRowCount(); i++)
{
String value = op.getPatientObservationValue(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
String date = op.getPatientObservationEffectiveDate(gui.getRightTable().getValueAt(i,0).toString(),"2093-3");
gui.getRightTableModel().setValueAt(value, i, columnNumberValue);
gui.getRightTableModel().setValueAt(date, i, columnNumberDate);
}
}
} catch (ParseException parseException) {
parseException.printStackTrace();
}
setCellColourForTable();
timer.schedule(new RefreshTable(), 0, seconds);
}
else
{
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3")));
gui.getRightTable().removeColumn(gui.getRightTable().getColumnModel().getColumn(newColumnIndeces.get("2093-3e")-1));
newColumnIndeces.remove("2093-3");
newColumnIndeces.remove("2093-3e");
timer.cancel();
timer = new Timer();
}
gui.getRightTableModel().fireTableStructureChanged();
}
You add a column to the model by using:
gui.getRightTableModel().addColumn("Cholesterol");
This will notify the view that the data has changed and the table will also be updated.
However, you remove the column from the table using:
gui.getRightTable().removeColumn(…);
This only removes the column from the "table view". The column has not been removed from the DefaultTableModel. So the next time you add a column it just gets added to the end of the DefaultTableModel.
So the solution is to remove the column from the DefaultTableModel, not the table.
Unfortunately there is no removeColumn(…) method.
However because your requirement is to only add/remove columns from the end of the DefaultTableModel you can use:
model.setColumnCount(model.getColumnCount() - 2);
which will effectively remove the last two columns from the model.
The other option is to not keep adding columns to the DefaultTableModel, but instead you can just add/remove TableColumns from the TableColumnModel directly. So this would mean the data for the Cholestoral column would always be in the model, but the view would just not display the columns.
So instead of using the model.addColumn(…) you would use the table.addColumn(…) method.
I want to edit a specific cell in a TableView using JavaFX (for example, row 3, column 5).
I tried this code but it doesn't work.
//Define the string
String s = "myString";
//Define the number
int value = 5;
//Synthesize the item = row
Item item = new Item(s, value);
//Set the i-th item
table.getItems().set(i, item);
Is this what you need? In your example you do not mention any position but the question title does.
table.getTableView().getItems().get(
t.getTablePosition().getRow())
).setLastName(t.getNewValue()
You can find here a complete example.
Just edit the list you are providing to the table to what you want and just call
yourtable.refresh();
I'm trying to color the text of every row in a table depending on one of the columns in the table. I'm having trouble grasping the concept of renderers, and I've tried out several different renderers but don't seem to understand what they do.
I am trying to load the top ten racers from a certain API given to us by our lecturer into the table model, but colouring each row based on the gender of the racer (which is returned by the getCategory() method of a Finisher/Racer object).
FYI, DataTable is an object written by our lecturer. It's basically a 2D array object.
public void showRacers(DefaultTableModel tblModel,
#SuppressWarnings("rawtypes") JList listOfRaces) {
// Clear the model of any previous searches
tblModel.setRowCount(0);
// Initialize an object to the selected city
CityNameAndKey city = (CityNameAndKey) listOfRaces.getSelectedValue();
// Get the runners for this city
DataTable runners = this.getRunners(city);
// Set the column headers
this.setColumnHeaders(tblModel);
// Make an array list of object Finisher
ArrayList<Finisher> finisherList = new ArrayList<Finisher>();
// Make an array that holds the data of each finisher
Object[] finisherData = new Object[6];
// Make a finisher object
Finisher f;
for (int r = 0; r < 10; r++) {
// Assign the data to the finisher object
finisherList.add(f = new Finisher(runners.getCell(r, 0), runners
.getCell(r, 1), runners.getCell(r, 2), runners
.getCell(r, 3), runners.getCell(r, 4), runners
.getCell(r, 5)));
// Add the data into the array
finisherData[0] = f.getPosition();
finisherData[1] = f.getBibNo();
finisherData[2] = f.getTime();
finisherData[3] = f.getGender();
finisherData[4] = f.getCategory();
finisherData[5] = f.getRuns();
// Put it into the table model
tblModel.addRow(finisherData);
}
}
I would greatly appreciate an explanation, rather than just the answer to my question. Guidance to the answer would be great, and some code would be extremely helpful, but please no: "You should have written this: ten lines of code I don't get
Thank you very much! :)
Using a TableCellRenderer will only allow you to color one column. You would have to have one for each column. A much easier approach is to override prepareRenderer(...) in JTable to color an entire row.
See trashgod's answer here or camickr's answer here
Writing GWT application.
I have CellTable and a code that i'v got from google-example code-site.
I need to implement server-side sorting by clicking on table's columns.
My code for that is:
AsyncDataProvider<MYOBJECT> dataProvider = new AsyncDataProvider<MYOBJECT>() {
#Override
protected void onRangeChanged(HasData<MYOBJECT> display) {
final Range range = display.getVisibleRange();
...
int sortingColumnIndex = 0;
boolean isAscending = sortList.get(sortingColumnIndex).isAscending();
// some server-side call here
}
So, how can I know which column an user clicks on ? I.e. Real index of column of header of column or anything for identifying that column user clicked?
I have only HasData display as an event, but it seems is not enough to determinate the column.
Sorting a CellTable server-side
The magic number 0, is the index of the sort column in the sort list, and not the column index from the cell table.
So sortList.get(0).getColumn() gets you the column that the user clicked on. You have to worry about the other columns in the sortList only if you plan to implement sorting on multiple columns.
ColumnSortList sortList = dataTable.getColumnSortList();
Column<?, ?> column = sortList.get(0).getColumn();
Above will give you the required column.
I have a pane with two tables A and B. When a row is selected in A, the content of B should be upated.
My code detects row selections in A fine. But, when the user clicks on the column header to sort rows, it does not seem like this is taken into account in A's table model.
So, I can get the selected row number (which is correct considering the sorting), but when I try to retrieve row field content from A using its table model, it gives me the values as if rows had not been sorted.
How can I retrieve the content of the selected line from the selected row number?
Without any code, it is hard to say for sure what your problem is. However, it sounds like you are mixing up the row indices between the view and the model. You must be very clear about what co-ordinate system you are referring to (view or model) when you have a row number. See the JTable API for the convertRowIndexToModel and convertRowIndexToView methods.
You probably need something like this:
JTable table = ...;
TableModel model = ...;
int viewRow = table.getSelectedRow();
int modelRow = table.convertRowIndexToModel(viewRow);
int viewColumn = table.getSelectedColumn();
int modelColumn = table.convertColumnIndexToModel(viewColumn);
Object cell = model.getValueAt( modelRow, modelColumn );