Specifying Fixed JTable Columns : JTable Column « Swing « Java Tutorial






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








14.62.JTable Column
14.62.1.Resizable JTable ColumnResizable JTable Column
14.62.2.Specifying Fixed JTable ColumnsSpecifying Fixed JTable Columns
14.62.3.Change Column WidthsChange Column Widths
14.62.4.Setting the Width of a Column in a JTable Component
14.62.5.Shading Rows and Columns in a JTable Component
14.62.6.Shades every other column yellow
14.62.7.Converts a visible column index to a column index in the model.
14.62.8.Insert a new column to a table
14.62.9.Converts a column index in the model to a visible column index
14.62.10.Returns the visible columns in the order that they appear in the model
14.62.11.Get the columns from TableColumnModel in the order that they appear in the view
14.62.12.Get column count
14.62.13.Returns the TableColumn associated with the specified column index in the model
14.62.14.Setting the Column Resize Mode of a JTable Component: Disable auto resizing
14.62.15.Locking the Width of a Column in a JTable Component
14.62.16.Appending a Column to a JTable Component using DefaultTableModel
14.62.17.Add a column with values.
14.62.18.Disable autoCreateColumnsFromModel
14.62.19.Add a column without affecting existing columns
14.62.20.Remove the first visible column without removing the underlying data
14.62.21.Move the last visible column so it becomes the first visible column
14.62.22.the last column is moved to the first position
14.62.23.Packing a Column of a JTable Component according to the header text
14.62.24.Packing a Column of a JTable Component according to the row data
14.62.25.Set column width based on cell renderer
14.62.26.Set table column auto-resize off
14.62.27.Changing the Name of a Column in a JTable Component
14.62.28.Disable auto resizing, Set the first visible column to 100 pixels wide
14.62.29.Change a cell in the 2nd visible column
14.62.30.Change a cell in the 3rd column in the model
14.62.31.Displaying an Icon in a Column Head of a JTable Component