JTable how to save the new Row index - java

I have the following Problem.
Let's Say I have a JTable with the folowing Values:
1
2
3
4
5
Now I select "3" and press a button which saves the index of the row and prints "3"
I have another Button let's call it the "next button", when I press it it will print "4"
Now I sort (Or in this Case just randomise) the whole Table and it's now like this:
2
5
4
1
3
When I now press the "next button" I want it to print "1" because it's the value after "4".
How can I manage this to work ?
It already works when I'm not sorting the Table than I can just print the saved row +1...
Is there something like a sort Listener so I can overwrite the saved row with the new value?
Cheers Timothy

I suggest you dive into the world of TableModel. When programming in Swing it is usually best to split the model from the component straight away. Add a column to the table to handle order, but don't show it.
A simpler method, but a bit of a dead end, is to make the the object stored in the table of a type of your own devising. Just override toString to return the display representation.

As i understand that the problem is with the last index , in ur problem that when it's 4 the next one should be 1 . this problem can be done by makeing if condition .
fore example if i suppose u save the index in a varible rowIndex = 4;
if(rowIndex ==jTable.getRowCount()-1)
nextRowinsex = 1 ; // which is the first index

Related

Android GridLayout confusion

I'm trying to use a GridLayout with 2 rows and "4" columns to achieve the below:
Since I figured that I can't make row 2's column 1 span 1½ column, I figured that if I instead used say 8 columns, I could just span each column in row 1 over 2 columns, and then for row 2 column 1 I could span that over 3 columns
However if I try this I get the following result:
How can i get the result of the first one using GridLayout?
EDIT
Doesn't have to be GridLayout, if theres an obvious alternative

Getting data from a csv file to a sqlplus table -> getting this data from sqlplus table to html table skeleton

I am a newbie in programming, so please excuse me if some of the stuff I say sounds stupid or not to the point. I am trying to explain what I have to do as best as I understand. I am looking forward to having guidance from all, and hope it solves my problem.
So the steps which I believe I have to undertake is as follows:
I have a CSV file and I have to load data from CSV file into the SQLPLUS table. The file has 17 columns. Now here comes the tricky part, there is one column which has values which correspond to my column number 2 in different rows. For examples, lets say the cell first row of second column is "FRUIT" and the FIRST SECOND and THIRD row of the 14th column are APPLE, MANGO and ORANGE. all three correspond to the fruit so must be shown in the same row when displaying in the html table.
I want to know, how should I configure my control file so that the apple mangoes and oranges must come in the same row as fruits.
I dont want it just for the display purpose because after this is done, I have to retrieve info from the the (apple orange mango column) for the current row and display it as 3 radio buttons.
I am not sure how well I explained it. But please if anyone has any idea please let me know.
This is a sample table:
double quotes mean one row. so its basically fruits then null below and null below.
"|FRUITS|APPLES|"
"| NULL |ORANGES|"
"| NULL |MANGOES|"
What I want to implement is:
|FRUITS|APPLES,ORANGES,MANGOES(displayed one below the other)|
then take the information from the cell in which apples oranges and mangoes are displayed and display it as dropdown selector.
THANKS>.
What I have tried is uploading the csv through sqloader using the str x0d . It displays the fruits in the same row as fruit, however when I am getting the data from the current cell, I cannot extract apples oranges and mangoes separately.
What I believe is I have to introduce some delimiter and then segregate based on that delimiter.

Selenium WebDriver: Random click a row button

How to click a row in table by randomly?
I know how to using Random class to iterate the List but don't know how to click the button of the specific row after randomize due to xpath different.
Please provide me some idea and guideline on how to solve this.
ps: I got some idea, the button is located at the last column, so i just click on the last column button.
Here is simple way to get random number from 1 to 10
int min=1;
int max=9;
int randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
System.out.println(randomNum);
If we think its row number, then, need to click on last column of that row.
i am imaging last column as td[6] then path will looks like
"//table/tbody/tr["+randomNum+"]/td[6]/button"

Does NatTable need a manual refresh after repaintCell?

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);

Editing a table in JavaFx

I have created a table in JavaFx. I used reflection to populate the table with numbers 1 to 100. This table contains a zone number and a description. There are 100 zones. I want the table to be editable. I have used the following code to make the cells editable.
zonesTable.setEditable(true);
zone.setEditable(true);
zone.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
description.setEditable(true);
description.setCellFactory(TextFieldTableCell.<Zones>forTableColumn());
zone.setCellValueFactory(new PropertyValueFactory<Zones, String>("rZoneNumber"));
description.setCellValueFactory(new PropertyValueFactory<Zones, String>("rDescription"));
for(int i = 0; i < 100; i++){
data.add(new Zones(i + "", ""));
}
zonesTable.setItems(data);
At the moment, this code adds numbers to the zone column and makes the zone and description column editable. However, after I type a value into the column and click the next row, my values that I input into the table disappear. I have no idea why. What do I need to do to cause my typed values to stay visible in the table after I select a different row than the one I am editing? Thanks in advance!
However, after I type a value into the column and click the next row,
my values that I input into the table disappear.
It doesn't work because there's a severe, embarrassing bug in JavaFX that Oracle refuses to fix.
The solution for you would be to press enter before you click the next row. But of course you can't request that from your users.
You may find a workaround here.
If you want to upvote the fix and comment on the bug, here's the issue.
Not enough code to determine how you work with the table.
You could tableColumn.setOnEditCommit(new CustomEventHandler)
You could tableColumn.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter() { your logic});
Those still only commit changes in the cell when Enter is pressed. So you will still need your own implementation based on which event you consider to commit edition.

Categories