Example usage for javax.swing JViewport setMaximumSize

List of usage examples for javax.swing JViewport setMaximumSize

Introduction

In this page you can find the example usage for javax.swing JViewport setMaximumSize.

Prototype

@BeanProperty(description = "The maximum size of the component.")
public void setMaximumSize(Dimension maximumSize) 

Source Link

Document

Sets the maximum size of this component to a constant value.

Usage

From source file:FixedColumnModel.java

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;
        }//from   w w w.ja v  a2 s. c  o m

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