Home > Enterprise >  Is it possible to move a part of a row of a JTable?
Is it possible to move a part of a row of a JTable?

Time:06-09


I want to move down a row of a table one position down except for one column. My table is something like this:

1 | a | aa
2 | b | bb
3 | c | cc

And I want it to be like this:
1 | b | bb
2 | a | aa
3 | c | cc

I tried to move it using moveRow() but the problem is that it moves my entire row? Does somebody know how to do it?
Thanks!

CodePudding user response:

For your example, one approach would be to define a javax.swing.table.TableModel that returns the row index for the first column, and fields from row records for the other columns. Then, moving part of the row in the table just means moving a full row in the underlying data model.

  class MyTableModel extends AbstractTableModel {
      //...
      public Object getValueAt(int row, int col) { 
          Object value;
          switch( col ) {
              case 0: value = (1 col);
              // other cases are fields from rows.
          }
      }

Other options include:

  • Moving rows, then moving some fields back.
  • Representing the table data model as a two-dimensional array, and moving only desired cells.
  • Related