Java Tutorial/Swing/JTable Column — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:44, 31 мая 2010

Содержание

Add a column without affecting existing columns

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model = (DefaultTableModel) table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());
    // Ensure that auto-create is off
    if (table.getAutoCreateColumnsFromModel()) {
      throw new IllegalStateException();
    }
    col.setHeaderValue("Col3");
    table.addColumn(col);
    model.addColumn("Col3",  new Object[] { "v3" });
  }
}





Add a column with values.

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    // Add a column with values.
    model.addColumn("Col2", new Object[] { "v2" });
  }
}





Appending a Column to a JTable Component using DefaultTableModel

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
  }
}





Change a cell in the 2nd visible column

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    table.setValueAt("New Value", 0, 0);
  }
}





Change a cell in the 3rd column in the model

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    table.getModel().setValueAt("New Value", 0, 0);
  }
}





Change Column Widths

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class ResizeColumnWidth {
  public static void main(String args[]) {
    final Object rowData[][] = { 
        { "1", "one",  "I" },
        { "2", "two",  "II" },
        { "3", "three", "III" }};
    final String columnNames[] = { "#", "English", "Roman" };
    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);

    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    
    TableColumn column = null;
    for (int i = 0; i < 3; i++) {
        column = table.getColumnModel().getColumn(i);
        if (i == 2) {
            column.setPreferredWidth(100); //sport column is bigger
        } else {
            column.setPreferredWidth(50);
        }
    }    
    
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Changing the Name of a Column in a JTable Component

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
    model.addColumn("Col2");
    table.getColumnModel().getColumn(0).setHeaderValue("New Name");
    table.getTableHeader().resizeAndRepaint();
  }
}





Converts a column index in the model to a visible column index

import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
  }
  public int toView(JTable table, int mColIndex) {
    for (int c = 0; c < table.getColumnCount(); c++) {
      TableColumn col = table.getColumnModel().getColumn(c);
      if (col.getModelIndex() == mColIndex) {
        return c;
      }
    }
    return -1;
  }
}





Converts a visible column index to a column index in the model.

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) {
  }
  public int toModel(JTable table, int vColIndex) {
    if (vColIndex >= table.getColumnCount()) {
      return -1;
    }
    return table.getColumnModel().getColumn(vColIndex).getModelIndex();
  }
}





Disable autoCreateColumnsFromModel

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    // Disable autoCreateColumnsFromModel
    table.setAutoCreateColumnsFromModel(false);
  }
}





Disable auto resizing, Set the first visible column to 100 pixels wide

import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    // 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);    
    TableColumn col = table.getColumnModel().getColumn(0);
    int width = 100;
    col.setPreferredWidth(width);
  }
}





Displaying an Icon in a Column Head of a JTable Component

import java.awt.ruponent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
    model.addColumn("Col2");
    table.getTableHeader().getColumnModel().getColumn(1).setHeaderRenderer(new IconHeaderRenderer());
    table.getColumnModel().getColumn(1).setHeaderValue(new TextAndIcon("Col2", new ImageIcon("icon.gif")));
  }
}
class TextAndIcon {
  TextAndIcon(String text, Icon icon) {
    this.text = text;
    this.icon = icon;
  }
  String text;
  Icon icon;
}
class IconHeaderRenderer extends DefaultTableCellRenderer {
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
      boolean hasFocus, int row, int column) {
    if (table != null) {
      JTableHeader header = table.getTableHeader();
      if (header != null) {
        setForeground(header.getForeground());
        setBackground(header.getBackground());
        setFont(header.getFont());
      }
    }
    if (value instanceof TextAndIcon) {
      setIcon(((TextAndIcon) value).icon);
      setText(((TextAndIcon) value).text);
    } else {
      setText((value == null) ? "" : value.toString());
      setIcon(null);
    }
    setBorder(UIManager.getBorder("TableHeader.cellBorder"));
    setHorizontalAlignment(JLabel.CENTER);
    return this;
  }
}





Get column count

import java.util.Enumeration;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
  }
  public TableColumn[] getColumnsInView(JTable table) {
    TableColumn[] result = new TableColumn[table.getColumnCount()];
    // Use a for loop
    for (int c = 0; c < table.getColumnCount(); c++) {
      result[c] = table.getColumnModel().getColumn(c);
    }
    return result;
  }
}





Get the columns from TableColumnModel in the order that they appear in the view

import java.util.Enumeration;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
  }
  public TableColumn[] getColumnsInView(JTable table) {
    TableColumn[] result = new TableColumn[table.getColumnCount()];
    // Use an enumeration
    Enumeration e = table.getColumnModel().getColumns();
    for (int i = 0; e.hasMoreElements(); i++) {
      result[i] = (TableColumn) e.nextElement();
    }
    return result;
  }
}





Insert a new column to a table

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });
    
    Vector data = model.getDataVector();
    Vector row = (Vector) data.elementAt(1);
    int mColIndex = 0;
    List colData = new ArrayList(table.getRowCount());
    for (int i = 0; i < table.getRowCount(); i++) {
      row = (Vector) data.elementAt(i);
      colData.add(row.get(mColIndex));
    }
    // Append a new column with copied data
    model.addColumn("Col3", colData.toArray());
    
    JFrame f = new JFrame();
    f.setSize(300, 300);
    f.add(new JScrollPane(table));
    f.setVisible(true);
  }
}





Locking the Width of a Column in a JTable Component

import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    TableColumn col = table.getColumnModel().getColumn(0);
    col.setMinWidth(100);
    col.setMaxWidth(100);
    col.setPreferredWidth(100);
  }
}





Move the last visible column so it becomes the first visible column

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    int rows = 10;
    int cols = 5;
    JTable table = new JTable(rows, cols);
    table.moveColumn(table.getColumnCount() - 1, 0);
  }
}





Packing a Column of a JTable Component according to the header text

import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Main {
  public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for(int i=0;i<table.getColumnCount();i++){
      DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
      TableColumn col = colModel.getColumn(i);
      int width = 0;
      TableCellRenderer renderer = col.getHeaderRenderer();
      if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
      }
      Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false,
          false, 0, 0);
      width = comp.getPreferredSize().width;
      col.setPreferredWidth(width+2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
  }
  public static void main(String[] argv) {
    new Main();
  }
}





Packing a Column of a JTable Component according to the row data

import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Main {
  public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (int i = 0; i < table.getColumnCount(); i++) {
      DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
      TableColumn col = colModel.getColumn(i);
      int width = 0;
      TableCellRenderer renderer = col.getHeaderRenderer();
      for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, i);
        Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i),
            false, false, r, i);
        width = Math.max(width, comp.getPreferredSize().width);
      }
      col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
  }
  public static void main(String[] argv) {
    new Main();
  }
}





Remove the first visible column without removing the underlying data

import java.util.Enumeration;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new MyDefaultTableModel();
    JTable table = new JTable(model);
    table.setModel(model);
    model.addColumn("Col1");
    model.addColumn("Col2");
    model.addColumn("Col3");
    model.addRow(new Object[] { "v1" });

    table.removeColumn(table.getColumnModel().getColumn(0));
    }
}
class MyDefaultTableModel extends DefaultTableModel {
  public Vector getColumnIdentifiers() {
    return columnIdentifiers;
  }
}





Resizable JTable Column

ModesDescriptionAUTO_RESIZE_ALL_COLUMNSAdjusts all column widths proportionally.AUTO_RESIZE_LAST_COLUMNAdjusts the rightmost column width only to give or take space as required by the column currently being altered.AUTO_RESIZE_NEXT_COLUMNIf you"re reducing the width of a neighboring column, the neighboring column will grow to fill the unused space. If you"re increasing the width of a column, the neighboring column will shrink.AUTO_RESIZE_OFFTurns off the user"s ability to resize columns. The columns can still be resized programmatically.AUTO_RESIZE_SUBSEQUENT_COLUMNSAdjusts the width by proportionally altering (default) columns displayed to the right of the column being changed.



import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class ResizeTable {
  public static void main(String args[]) {
    final Object rowData[][] = { 
        { "1", "one",  "I" },
        { "2", "two",  "II" },
        { "3", "three", "III" }};
    final String columnNames[] = { "#", "English", "Roman" };
    final JTable table = new JTable(rowData, columnNames);
    JScrollPane scrollPane = new JScrollPane(table);
    String modes[] = { "Resize All Columns", "Resize Last Column", "Resize Next Column",
        "Resize Off", "Resize Subsequent Columns" };
    
    final int modeKey[] = { JTable.AUTO_RESIZE_ALL_COLUMNS, JTable.AUTO_RESIZE_LAST_COLUMN,
        JTable.AUTO_RESIZE_NEXT_COLUMN, JTable.AUTO_RESIZE_OFF,
        JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS };
    
    JComboBox resizeModeComboBox = new JComboBox(modes);
    ItemListener itemListener = new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        JComboBox source = (JComboBox) e.getSource();
        int index = source.getSelectedIndex();
        table.setAutoResizeMode(modeKey[index]);
      }
    };
    resizeModeComboBox.addItemListener(itemListener);
    JFrame frame = new JFrame("Resizing Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(resizeModeComboBox, BorderLayout.NORTH);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





Returns the TableColumn associated with the specified column index in the model

import java.util.Enumeration;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) throws Exception {
  }
  public TableColumn findTableColumn(JTable table, int columnModelIndex) {
    Enumeration e = table.getColumnModel().getColumns();
    for (; e.hasMoreElements();) {
      TableColumn col = (TableColumn) e.nextElement();
      if (col.getModelIndex() == columnModelIndex) {
        return col;
      }
    }
    return null;
  }
}





Returns the visible columns in the order that they appear in the model

import java.util.ArrayList;
import java.util.Collections;
import java.util.ruparator;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) {
  }
  // 
  public TableColumn[] getColumnsInModel(JTable table) {
    List result = new ArrayList();
    for (Enumeration e = table.getColumnModel().getColumns(); e.hasMoreElements();) {
      result.add((TableColumn) e.nextElement());
    }
    Collections.sort(result, new TableColumnComparator());
    return (TableColumn[]) result.toArray(new TableColumn[result.size()]);
  }
}
class TableColumnComparator implements Comparator {
  public int compare(Object a, Object b) {
    TableColumn c1 = (TableColumn) a;
    TableColumn c2 = (TableColumn) b;
    if (c1.getModelIndex() < c2.getModelIndex()) {
      return -1;
    } else if (c1.getModelIndex() == c2.getModelIndex()) {
      return 0;
    } else {
      return 1;
    }
  }
}





Set column width based on cell renderer

import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Main {
  public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for (int i = 0; i < table.getColumnCount(); i++) {
      DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
      TableColumn col = colModel.getColumn(i);
      int width = 0;
      TableCellRenderer renderer = col.getHeaderRenderer();
      for (int r = 0; r < table.getRowCount(); r++) {
        renderer = table.getCellRenderer(r, i);
        Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, i),
            false, false, r, i);
        width = Math.max(width, comp.getPreferredSize().width);
      }
      col.setPreferredWidth(width + 2);
    }
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
  }
  public static void main(String[] argv) {
    new Main();
  }
}





Set table column auto-resize off

import java.awt.ruponent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public class Main {
  public Main() {
    JTable table = new JTable(3, 3);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    for(int i=0;i<table.getColumnCount();i++){
      DefaultTableColumnModel colModel = (DefaultTableColumnModel) table.getColumnModel();
      TableColumn col = colModel.getColumn(i);
      int width = 0;
      TableCellRenderer renderer = col.getHeaderRenderer();
      if (renderer == null) {
        renderer = table.getTableHeader().getDefaultRenderer();
      }
      Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false,
          false, 0, 0);
      width = comp.getPreferredSize().width;
      col.setPreferredWidth(width+2);      
    }    
    JFrame f = new JFrame();
    f.add(new JScrollPane(table));
    f.setSize(300, 300);
    f.setVisible(true);
  }
  public static void main(String[] argv) {
    new Main();
  }
}





Setting the Column Resize Mode of a JTable Component: Disable auto resizing

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    Object[][] cellData = { { "1-1", "1-2" }, { "2-1", "2-2" } };
    String[] columnNames = { "col1", "col2" };
    JTable table = new JTable(cellData, columnNames);
    
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  }
}





Setting the Width of a Column in a JTable Component

import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
  public static void main(String[] argv) {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    // 
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);    
    TableColumn col = table.getColumnModel().getColumn(0);
    int width = 100;
    col.setPreferredWidth(width);
  }
}





Shades every other column yellow

import java.awt.Color;
import java.awt.ruponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class Main {
  public static void main(String[] argv) throws Exception {
    JTable table = new JTable() {
      public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
        Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
        if (vColIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
          c.setBackground(Color.yellow);
        } else {
          c.setBackground(getBackground());
        }
        return c;
      }
    };
  }
}





Shading Rows and Columns in a JTable Component

import java.awt.Color;
import java.awt.ruponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class Main {
  public static void main(String[] argv) {
    JTable table = new JTable() {
      public Component prepareRenderer(TableCellRenderer renderer, int rowIndex, int vColIndex) {
        Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
        if (rowIndex % 2 == 0 && !isCellSelected(rowIndex, vColIndex)) {
          c.setBackground(Color.yellow);
        } else {
          c.setBackground(getBackground());
        }
        return c;
      }
    };
  }
}





Specifying Fixed JTable Columns

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class FixedColumnModel {
  public static void main(String args[]) {
    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };
    final String columnNames[] = { "#", "English", "Roman" };
    final TableModel fixedColumnModel = new AbstractTableModel() {
      public int getColumnCount() {
        return 1;
      }
      public String getColumnName(int column) {
        return columnNames[column];
      }
      public int getRowCount() {
        return rowData.length;
      }
      public Object getValueAt(int row, int column) {
        return rowData[row][column];
      }
    };
    final TableModel mainModel = new AbstractTableModel() {
      public int getColumnCount() {
        return columnNames.length - 1;
      }
      public String getColumnName(int column) {
        return columnNames[column + 1];
      }
      public int getRowCount() {
        return rowData.length;
      }
      public Object getValueAt(int row, int column) {
        return rowData[row][column + 1];
      }
    };
    JTable fixedTable = new JTable(fixedColumnModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    JTable mainTable = new JTable(mainModel);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    ListSelectionModel model = fixedTable.getSelectionModel();
    mainTable.setSelectionModel(model);
    JScrollPane scrollPane = new JScrollPane(mainTable);
    Dimension fixedSize = fixedTable.getPreferredSize();
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedSize);
    viewport.setMaximumSize(fixedSize);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    scrollPane.setRowHeaderView(viewport);
    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
  }
}





the last column is moved to the first position

import javax.swing.JTable;
public class Main {
  public static void main(String[] argv) throws Exception {
    int rows = 3;
    int cols = 3;
    JTable table = new JTable(rows, cols);
    table.getTableHeader().setReorderingAllowed(false);
    table.moveColumn(table.getColumnCount() - 1, 0);
  }
}