How to remove rows with certain values from Jtable? - java

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

Related

How to get a row count for a table that does not display all rows at once?

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.

Remove JTable row with String containing ceratin letter

Hello I am working on a project with a JTable to join words. In the table words that have been joined together have an # character between them.
I basically want to remove all the rows in the table containing the character #. This is what I have tried so far:
for (int i = 0; i < table.getRowCount(); i++) {
if ((boolean)table.getValueAt(i, 0).equals("\\b[#]+\\b")) {
table.remove(i);
}
}
This code is not working as intended. I would like to know the correct way to write this code. Thank you for any replies in advance.
Your code calls the remove() method inherited from the Container class - you want to manipulate the table model used by your table. Assuming that you're using a DefaultTableModel, you can get the model from the table and use the removeRow() method.
Also, .equals("\\b[#]+\\b") doesn't check for a String containing the '#' character. It checks for an exact match of the text that you specified. You might want to look at the String.contains(...) method.
Remember that the data in a Swing JTable is stored in an underlying TableModel, not in the JTable object itself. Something like this should work.
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i = 0; i < model.getRowCount(); i++) {
if (model.getValueAt(i, 0) != null && model.getValueAt(i, 0).toString().contains("#")) {
model.removeRow(i);
}
}

Removing several blank lines in XLS using Apache POI HSSF with an incrementing loop

I need to remove several lines of an excel xls sheet.
These lines always contain the same first cell thats why i check the first cell of all rows to find these rows
SSFCell myCell = myRow.getCell(0);
myCell.setCellType(Cell.CELL_TYPE_STRING);
String foundString = myCell.getStringCellValue();
if(foundString.equals(searchString)){
foundRows.add(rowCount);
}
rowCount++;
I then go on and "remove" those rows using removeRow which nulls all values
public static void removeRows() {
List<Integer> foundRowsToDelete = new ArrayList<Integer>();
//Copy values to another list
for(int i=0; i<foundRows.size(); i++){
foundRowsToDelete.add(foundRows.get(i));
}
//Delete values from rows, leaving empty rows
while(foundRowsToDelete.size()!=0){
int rowIndex = foundRowsToDelete.get(0);
Row removingRow = mySheet.getRow(rowIndex);
if (removingRow != null) {
mySheet.removeRow(removingRow);
foundRowsToDelete.remove(0);
}
}
//Move empty rows to bottom of the sheet
for(int i = 0; i < mySheet.getLastRowNum(); i++){
if(isRowEmpty(i)){
mySheet.shiftRows(i+1, mySheet.getLastRowNum(), -1);
i--;
}
}
}
I check if they are empty through using the duplicated rowcounter
//Comparision of previously detected empty rows and given row count
public static boolean isRowEmpty(int suspectedRowNumber) {
for(int i=0;i<foundRows.size();i++){
if (suspectedRowNumber == foundRows.get(i)){
foundRows.remove(i);
return true;
}
}
return false;
}
However only the first of these rows gets deleted. The rest will stay empty.
I therefore assume that there is something wrong with some incrementing done by me, but i just can't figure out exactly why.
Thanks for your help in advance.
It's not immediately clear why your code isn't working, but I look at a couple things to debug
Your foundRowsToDelete ArrayList is being populated with values contained in the foundRows Array. Are you sure what you expect to find in foundRows is actually there.
Is there a reason you don't remove the row when initally iterating through the rows in your sheet? Maybe something like this:
Sheet sheet = workbook.getSheetAt(0);
For (Row row : sheet) {
SSFCell myCell = row.getCell(0);
if(myCell.getCellType() == Cell.CELL_TYPE_STRING){
String foundString = myCell.getStringCellValue();
if(foundString.equalsIgnoreCase(searchString){
// why not just remove here?
sheet.removeRow(row);
}
}
}
}

Get cell values of all selected rows in JTable

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

Initialize 2D arrayList with specified number of columns but not rows?

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;

Categories