user3366184
user3366184

Reputation:

Modify Entire Column(s) in JTable

I have a JTable which is formed by importing data from a text file. It is a huge table with about 522 columns and thousands of rows. Many cells in this table are empty as well. Now, I want to be able to apply some mathematical operations to the data available in certain columns. So right now, I can select multiple columns, but I dont know how to go about getting these values. I know I'll need an array of arrays where I can store the value from the table columns and then modify each value based on my algorithm. Right now, to start it off simply, I just want to be able to print out the values in the select column (just one to keep it simple) but I cant do that, I get the value of a particular cell printed 2-4 times. My testing code is as follows: For Selection entire columns, I am using this code:

public static void selectWholeColumn(final JTable table)
{
 final JTableHeader header = table.getTableHeader(); 
 header.addMouseListener(new MouseAdapter() 
 {  
        public void mouseClicked(MouseEvent e) 
        {  
            int col = header.columnAtPoint(e.getPoint());   
            if(header.getCursor().getType() == Cursor.E_RESIZE_CURSOR) 
            {
                e.consume();  
            }
            else 
            {   
                table.setColumnSelectionAllowed(true);
                table.setRowSelectionAllowed(false);
                table.clearSelection();
                table.setColumnSelectionInterval(col,col);
                table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);                  

            }  
        } 

    }); 

} 

In my GUI I have a button which when clicked fires a backend class and this method which takes in a JTable as a parameter is executed to print out the values of all rows in the selected Column:

public void filterData(final JTable table)
{
    TableModel model = table.getModel();
     table.setCellSelectionEnabled(true);
    table.setColumnSelectionAllowed(true);
    table.setRowSelectionAllowed(false);
        ListSelectionModel cellSelectionModel = table.getSelectionModel();
        cellSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        cellSelectionModel.addListSelectionListener(new ListSelectionListener() 
        {
          public void valueChanged(ListSelectionEvent e)
          {
            String selectedData = null;

            int[] selectedRow = table.getSelectedRows();
            int[] selectedColumns = table.getSelectedColumns();

            for (int i = 0; i < selectedColumns.length; i++) 
            {
              for (int j = 0; j < selectedRow.length; j++) 
              {
                selectedData = (String) table.getValueAt(selectedRow[i], selectedColumns[j]);
              }
            }
            System.out.println("Selected: " + selectedData);
          }

        });

Any suggestions as to how I could print or basically get the values of all rows in a selected Column or columns, so that I can modify the data in them at once?

Thank you!

Upvotes: 0

Views: 646

Answers (1)

trashgod
trashgod

Reputation: 205785

Looping though the TableModel and using getValueAt() in a ListSelectionListener is a reasonable approach, although your listener will be invoked for each change. It's not clear where you're having trouble; you can

  • Concatenate the selected values using StringBuilder.

  • Accumulate the selected values in a List<String>.

  • Maintain the selection in your TableModel, as shown here using Set<Integer>.

  • Operate on the selection in the ItemHandler of a TableCellRenderer, as shown here.

Upvotes: 2

Related Questions