I have a table that when the page is fully displayed has a row count of 20 rows. However, if there is more than 20, and the user scrolls down additional rows are added. Since it only gets the initial row count, I can never find the element I'm trying to search for.
What I have right now is something along these lines.
boolean isFound = false;
List<WebElement> rowList = webdriver.findElements(By.xpath("//*[contains(#class, 'requestRow')]"));
int rowCount = rowList.size();
String rowNum = null;
for (int i = 1; i <=rowCount+1; i++){
rowNum = Integer.toString(i);
}
Any idea of how I can have the table expanded and capture the new value?
OK, so what I did was create a list based on the current row count.
I then loop through the list incrementing one each time. If the number I'm looking for is found in the table, it does what I need doing. If it gets to a number higher than the current count of rows, I click the load more button, add the new value to my row count, and keep searching.
I have a Jtable that I've loaded with values from a .csv file, but it creates a new row for every instance of 5/13/2013 that shows up in the file, like so:
I'd like to remove all rows with this info from the table, but am not sure how to do so. Any suggestions for me?
Here's my code to add the data to the table, if it helps:
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
tableModel.insertRow(tableModel.getRowCount(), values);
}//end of while block`
To reiterate and be completely clear, I want to remove every row that contains "5/13/2013" from the table completely. And I'm using the deafault table model, by the way.
I ended up applying a for loop and an if statement to get this working for me.
//remove rows with instances of "5/13/2013"
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}//end of if block
}//end of for block
It's worked well for me and has gotten rid of each of those rows. Hopefully this may help someone else out.
while(i < tableModel.getRowCount()) {
//if the value at (i, 0) match the specified value the row will be removed
/*
if the row removed all row will move up and their index will be changed
so you have to add a condition if the value from the table doesn't match
the specified value the iterator i will iterate by one to jump to the next
row
*/
if (((String)tableModel.getValueAt(i, 0)).equals("5/13/2013")) {
tableModel.removeRow(i);
}else {
++i;
}
}
I'm trying to figure out how to iterate through the rows of a JTable and get the cell values if the row is selected (multiple rows can be selected), pass the values to a method then continue iteration. The table rows contain values entered by the user. Rows are added to the table, which is displayed in the UI one by one as the user inputs each entry. The entries consist of an int and 2 doubles. The int identifies the type and the two doubles are added to two running tallies (quantity and volume) for the type, for use elsewhere in the application. If the user selects a row (or multiple rows) and presses Delete, the rows are deleted from the table. The values of the deleted rows also need to be deducted from the running tallies. For deleting the rows, I am assigning the selected rows to an array and iterating through it to delete each row.
int[] selectedRows = entryTable.getSelectedRows();
if (selectedRows.length > 0) {
for(int i = selectedRows.length - 1; i >= 0; i--) {
entryTable.removeRow(selectedRows[i]); } }
If it is possible to get the cell values during this iteration, that would be ideal but after extensive searching, I have not yet found a way to do so. Any other way would be fine as long as the end result is the same. Any thoughts on the most efficient way to accomplish this would be appreciated.
Well you can try like this.
public void myMethod(JTable entryTable) {
DefaultTableModel model = (DefaultTableModel) entryTable.getModel();
if (entryTable.getRowCount() > 0) {
if (entryTable.getSelectedRowCount() > 0) {
int selectedRow[] = entryTable.getSelectedRows();
for (int i : selectedRow) {
int id = Integer.parseInt(entryTable.getValueAt(i, 0).toString());
double val1 = Double.parseDouble(entryTable.getValueAt(i, 1).toString());
double val2 = Double.parseDouble(entryTable.getValueAt(i, 2).toString());
model.removeRow(i);
}
}
}
}
I have a jTable and I want to delete selected rows:
for( int i=0; i<selectedRows.length ; i++){
defaultablemodel.removeRow( selectedRows[i] - i);
int viewRowIndex = jTableMyTable.convertRowIndexToView(i); <<--- convert it here?
}
I searched around and most suggestions whenever delete rows, we should convert row index to view. Is the above correctly implemented?
I hit this problem because after i sorted my rows, i am unable to delete the row sometimes. but after sorting again i am able to delete a row. So i believe I need to use this convertRowIndexToView method.
You should actually be using
convertRowIndexToModel - Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
You have your selected rows, which are the selected indices in the view. So you need to convert those indices to the model indices before deleting.
When you use convertRowIndexToView, you're actually doing the opposite, trying to convert the model index to the table index.
" Is the above correctly implemented?"
First thing, when you want to remove rows this way, you should be traversing the indices backwards, from the last row up. The reason is that when you remove a row, the indices change. So you loop should go backwards like
int [] selectedRows = table.getSelectedRows();
for (int i = selectedRows.length - 1; i >= 0; i--) {
int viewIndex = selctedRows[i];
int modelIndex = table.convertRowIndexToModel(viewIndex);
model.removeRow(modelIndex);
}
The point is you want to remove the highest index first and work your way down to the lower indices.
I want to calculate total value in index [0][2], previous value is 0, but I have replaced and display it with the calculation result “1/y”, and when I calculate total value, value that summed is previous value that containing “0”, this my code,
//observation table
//0 1 2 3 4 5 6
titleColumn = new Object[]{"Time (Second)","Medicine", "1/y","x2", "X/Y", "Y^", "Error"};
//0 1 2 3 4 5 6
allData = new Double[][] {{1.0,1.02,0.0,0.0,0.0,0.0,0.0},
{2.0,0.667,0.0,0.0,0.0,0.0,0.0},
{3.0,0.367,0.0,0.0,0.0,0.0,0.0},
{4.0,0.278,0.0,0.0,0.0,0.0,0.0},
{5.0,0.237,0.0,0.0,0.0,0.0,0.0},
{6.0,0.187,0.0,0.0,0.0,0.0,0.0},
{7.0,0.155,0.0,0.0,0.0,0.0,0.0},
{8.0,0.156,0.0,0.0,0.0,0.0,0.0},
{9.0,0.142,0.0,0.0,0.0,0.0,0.0},
{10.0,0.111,0.0,0.0,0.0,0.0,0.0},
{11.0,0.12,0.0,0.0,0.0,0.0,0.0},
{12.0,0.097,0.0,0.0,0.0,0.0,0.0},
{13.0,0.099,0.0,0.0,0.0,0.0,0.0},
{14.0,0.089,0.0,0.0,0.0,0.0,0.0},
{15.0,0.079,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0}};
tableModelObservation = new DefaultTableModel(allData, titleColumn);
tableObservation.setModel(tableModelObservation);
int row,column,inputRow,inputColumn;
//index [0][2] was replaced with calculation 1/y
row = 0;
column = 1;
inputRow = 0;
inputColumn = 2;
double onePerY = 0;
for(int a=0;a<allData.length;a++){
onePerY = 1/allData[row][column];
//is this the way to use getValueAt() and for indicating that the dependent cell has been change ?
tableObservation.getModel().getValueAt(row, column);
tableObservation.firePropertyChange("1/y", inputRow, inputColumn);
tableObservation.getModel().setValueAt(onePerY, inputRow, inputColumn);
inputRow++;
row++;
}
//calculation total 1/y, summing is still using previous value "0"
row = 0;
column = 2;
inputRow = 15;
inputColumn = 2;
double totalOnePerY = 0;
for (int a=0;a<allData.length;a++){
totalOnePerY += allData[row][column];
row++;
}
//displaying result in row index[15] and column index[2]
tableObservation.getModel().setValueAt(totalOnePerY, inputRow, inputColumn);
this column values ”1/y” after calculation process
What should I do, to be able to summing it using new value ?
all the assistance that you gave, I would appreciate it, thank you
As shown in DependentColumn, you can calculate derived values in your implementation of getValueAt(). If a column on which the value depends is editable, be sure to fire an event indicating that the dependent cell has changed.
Addendum: I’ve added my code with getValueAt(), but the result is the same.
Absent your sscce I'm guessing: I see that you access the allData array after you use it to construct and update the DefaultTableModel. I'm not sure about the goal of the exercise, but you probably wan't to update the array before doing so. DefaultTableModel uses Vector internally, and it ignores the array after construction completes.
You can try to use EnvelopeTableMode which wraps original model and can provide grouping functions (SUM, COUNT, AVG...).