Does NatTable need a manual refresh after repaintCell? - java

I have the following code snippet.
int index = getEventList().indexOf(myObj);
SelectionLayer selectionLayer = getGlazedListsGridLayer()
.getBodyLayerStack().getSelectionLayer();
getNatTable().repaintCell(0,selectionLayer.getRowIndexByPosition(index));
When I run the code shown above, the affected cells only get repainted after I click on the table displayed in the GUI. If I comment out that code and use getNatTable().refresh(); it repaints without me having to first click on the table.
Is there a way to have a cell repainted without having to click on the table displayed in the GUI? I would hate to have to call refresh() for a large table where this code may be executed many times.

No you don't need to perform some additional step in order to trigger the repainting. The issue in your code is the usage of wrong index values. You have a grid so column 0 is the row header from the NatTable perspective. I suppose you want to redraw the first column of the body, which is index 1 from the NatTable point of view. Also the row index is incorrect, as you calculate the index in the SelectionLayer but actually need the index in the table, which is at minimum +1 if you only have a column header row.
Actually your code should work by adding 1 on the index if you have one row header column and one column header row in the grid.
getNatTable().repaintCell(1, selectionLayer.getRowIndexByPosition(index) + 1);

Related

Horizontal scrolling in selenium web driver till element is not visible

I have a website that has a long horizontal table. I have Xpath that extracts column headers of a table that stretches more than the screen width (3X of the screen width). Now I have to extract all column heads with their respective index as my table has show columns filters from where user can select or deselect the rows. So I need to dynamically fetch the columns with the index. Now I when I try to fetch the column heads for the first time then system fetch me the column heads till it is visible on the first screen without scroll but now when the system tries to get the column head which is not visible on chrome then code returns a null value.
I have tried to get the total number of columns and checked that scrolling right by 8 columns will do my job but then with this, I am not able to reuse this function on other web tables where there are lesser columns as the system does not find the i+8 column and shows the exception.
I tried to scroll right every time by one column when I get column head as blank. But with this, my code has to check one if condition each time when it does get blank and this is making my method very slow which I can't afford as I need to get at least 35 columns and that too more than 2-3 time in bigger methods.
Now, what I know here is I know which is the last column head was visible to me so I need to scroll right till column head which I had is not visible to me on a screen which other way means I have scrolled maximum to right without hiding the column which I haven't covered. How can I do it?
This is what I tried for this scrolling by 8 columns by seeing the general width of columns. This is working for me but restricting the reusability of function.
if (colHead.equalsIgnoreCase("")) {
WebElement elementToBeScrolled1 = driver.findElement(By.xpath("//th["+i+"+8]"));
By elementToBeScrolled = getByLocator(elementToBeScrolled1);
do {
keyPress(Keys.ARROW_RIGHT);
} while (waitForElement(elementToBeScrolled, 5, WaitType.invisibilityOfElementLocated));
colHead = driver.findElement(By.xpath("//th[" + i + "]"))
.getText();

How to change JTable column width depending on data?

How would I resize the column for it to just fit the largest string in that column. For example in one column I might have a word like "Monday" and I want this column to be shorter than the "Wednesday" column.
The data changes often as it shows the next 3 days, so I would want Java to automatically change it depending on the input data rather than me having to manually set the column width.
My JTable is within a JScrollPane if that changes anything
Hope that makes sense. Please point me in the right direction.

Setting Header Row in Itext Table

I am creating a table using itext. Now while setting header row, if I set table.setheaderrows(2) then it sets first 2 rows as the header. But in my case I want only row no. 2 (not row number 1) to be reprinted while table is extended on the next page.
Is there any way to achieve this?
If you only work with a single PdfPTable, you can't define the second row as the header row that needs to be repeated. The trick is to use two PdfPTable instances with the same widths for the columns. The first one would be a single row table for the first header row, the second one would start with the header row that needs to be repeated. If you add two tables to a document, one right after the other, they are glued to each other and nobody will notice that it's not a single table.

How can delete/hide the mid row(5)th row before it listed in jtable

Here i have an 1-10 row is listed in jtable i want to delete/hide the 5th row before it listed in jtable.
i set the rowheight but it affected the cellselection.Is there any way to hide/delete the row without affected the normal flow code?
If i remove the row it will throws ArrayIndexoutofBoundException.
in my project executed means one gui open in that gui listed the some string. In here we can add the more string via Add Button on popup Button
Here what i need is i have to hide the particular string. That string is placed on 1st row.
i need to hide the string from end user.
now u hope understand.
You can use the JTable row filtering support in order to hide certain rows without deleting them from the model. Also see this: How can I filter rows in a JTable?
You can eliminate rows in the table by calling the removeRow() method. If you want to just hide it instead of elimintaing it you need to customize the JTable's model to meet your specs on what to display.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
using DefaultTableModel with JTable you should be able to use model.removeRow(int row) function to remove A row from JTable. There is no way to hide a row based on index as much as i know. However, If you need to hide and re-show mechanism you need to save the row prior to delete it and Save the removedRow in a ArrayList to re-use them.. Something as follows:
List<Vector>deletedRows = new ArrayList<>();
Vector removingRow = (Vector) model.getDataVector().get(5);
deletedRows.add(removingRow);
model.removeRow(5);

How can I hide a row in my JTable?

I'm using Eclipse Indigo SR1 with JDK 1.7, on Windows 7 Pro.
I've written a desktop app, Swing based.
My app includes a JTable; it shows many records of type T, one row per record.
The table model points to Vector vect, named vect, containing all data to be shown in the JTable.
The app includes a combo, named sele, showing three values: 0, 1, 2.
When sele = 0, every record of vect has to be visible in the JTable.
When sele = 1, the JTable has to show only vect records having odd row index and all records with even row index mustn't be visible. Viceversa, when sele = 2.
So, here's my question: how can I make a row not visible in the JTable ?
I can't use the table model, because it points to vect that contains "all" data.
I tried a table cell renderer, but it seems that you can set the color of a cell, but you can't set it not visible or modify its size.
I've tried another way: if r is the row index, and I want that row to be not visible, I write table.setRowHeight(r,0), but this instruction throws an exception, the height can't be set to zero.
I could solve the problem by splitting the data, dividing vect in two, but I don't like that.
Does anybody have an idea ?
thanx in advance,
William
PS: someone told me to create a filtering TableModel that wraps the existing TableModel. The filtering model would be sensitive to the filtering criteria and fire the appropriate methods (TableDataChanged) when the filter was changed. The getRowCount method would return the filtered count. The getValueAt method would map the filtered row to the actual row in the underlying TableModel.
Mah, perhaps it's a good idea, but frankly I'm not able to understand it...
Use a TableRowSorter - which is:
An implementation of RowSorter that provides sorting and filtering using a TableModel. ..
See How to Use Tables & especially Sorting and Filtering for more info.

Categories