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);
}
}
}
}
Related
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 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 make a 2D ArrayList, with the number of columns already specified but not the rows.
For example, I want to create a table with 26 columns and 0 or 1 columns at first, then after each loop of doing something else, the number of rows will increase along with that loop.
When I increase the number of rows (length of an ArrayList of ArrayLists), I also want all 26 arrays to increase as well. What is the syntax for it?
And how would I index into, or add a new item into a specific location - say array[2][3] = item?
BTW this is a DFSA table converted from a NFSA table
You could have a list of lists, essentially something like so:
List<List<String>> table = new ArrayList<List<String>>();
Then add the 26 columns:
for(int i = 0; i < 26; i++)
{
table.add(new ArrayList<String>());
}
You can then have a method, called, say, addToColumn(int column, String value, List<List<String>> table) in which you do something like so:
for(int i = 0; i < table.size(); i++)
{
if(i == column)
{
table.get(i).add(value);
}
else
{
table.get(i).add("");
}
}
This should allow you to have lists which grow together. Of course, my assumption in the above is that you will be entering one element at a time.
Alternatively, you can do something like so:
public void addToColumns(Map<int, String> data, List<List<String>> table)
{
for(int key : data.keyset())
{
table.get(key).add(data.get(key));
}
for(int i = 0; i < table.size(); i++)
{
if(!data.containsKey(i))
{
table.get(i).add("");
}
}
}
The above algorithm should allow you to add items to multiple columns, while filling the rest up with empty strings. This should allow you to end up with rows of equal length. Also, the map will be used to store a key-value pair where the key is the column number, and the value will be whatever string you would like to throw in there. This will allow you to populate your table one row at a time.
You can simply create an array by giving only the number of rows:
int[][] array = new int[4][];
Now you may treat your array as the transpose of what you have defined so if you want to enter an element at 3rd column of 2nd row you can enter as transpose i.e.
array[3][2]=5;
I have a program that is written to work in conjunction with a JTable and a few other Swing elements in order to display and model a backend for a game. The function is supposed to get the first row selected and the last row selected and store them in an array rows at index 0 and 1 respectively. Thanks for your help and hope to understand what is going on here.
public int[] getRows(JTable table) {
rows = new int [2];
rows[0] = table.getSelectedRow();
rowCount = table.getSelectedRowCount() - 1;
rows[1] = rows[0] + rowCount;
return rows;
}
JTable.getSelectedRows() can you help you in this case. Just take the first and last index from the array it returns. Note that it can return an empty array.