在JTable中设置列的大小

时间:2011-04-27 19:02:23

标签: java swing jtable column-width

我的课程扩展了 JTable ,我有类实现 TableModel DefaultActionTableModel

public class RingTable extends JTable {

    private static final long serialVersionUID = -2828916773705542169L;
    private RingTableModel model;

    public RingTable () {
    }

    public RingTable (List<String> columns) { // NO_UCD
        model = new RingTableModel(columns);
        setModel(model);
    }

    public RingTable (List<String> columns, List<TableRing> tableRings) {
        model = new RingTableModel(columns, tableRings);
        setModel(model);
    }

    /**
     * Dodaje novu vrstu u tabelu sa zadatim indeksom
     * @param i
     */
    public void addRow(int i) {
        model.addRow(i);
    }


    public boolean addRow(TableRing tableRing) { // NO_UCD
        return model.addRow(tableRing);
    }

    /**
     * Uklanja vrstu iz tabele koja ima indeks kao prosledjeni parametar
     * @param i
     * @return
     */
    private boolean removeRow(int i) {
        model.removeRow(i);
        return true;
    }

    /**
     * Uklanja sleektovane vrste iz tabele
     */
    public void removeSelectedRows() {
        int[] indices = getSelectedRows();
        Arrays.sort(indices);
        removeRowSelectionInterval(0, getRowCount()-1);
        for (int i = indices.length-1; i >= 0; i--)
            removeRow(indices[i]);
    }

    /**
     * Uklanja sve vrste iz tabele
     */
    public void removeAllRows() { // NO_UCD
        while (model.getRowCount() > 0) {
                removeRow(0);
        }
    }

}

 public class RingTableModel implements TableModel, DefaultActionTableModel {

        private List<String> columns; // = new LinkedList<String>();
        private List<TableRing> tableRings;


        /**
         * 
         */
        public RingTableModel() {
        }

        /**
         * 
         */
        public RingTableModel(List<String> columns) {
            this(columns, new LinkedList<TableRing>());
        }

        /**
         * 
         */
        public RingTableModel(List<String> columns, List<TableRing> tableRings) {
            this.columns = columns;
            this.tableRings = tableRings;
        }



        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#addTableModelListener(javax.swing.event.TableModelListener)
         */
        public void addTableModelListener(TableModelListener l) {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getColumnClass(int)
         */
        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
                case 0: // Index
                    return Integer.class;
                case 1: // Name
                    return String.class;
                case 2: // Show Dampers
                case 3: // Show Inputs
                case 4: // Show Outputs
                    return Boolean.class;
                case 5: // Host
                    return String.class;
                default:
                    return Boolean.class;
            }
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getColumnCount()
         */
        public int getColumnCount() {
            return (columns!=null)?columns.size():0;
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getColumnName(int)
         */
        public String getColumnName(int columnIndex) {
            if (columns == null)
                return null;
            if (columns.size() <= columnIndex)
                return null;
            if (columnIndex < 0)
                return null;
            return columns.get(columnIndex);
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getRowCount()
         */
        public int getRowCount() {
            return (tableRings == null)?0:tableRings.size();
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#getValueAt(int, int)
         */
        public Object getValueAt(int rowIndex, int columnIndex) {
            if (tableRings == null)
                return null;
            if (rowIndex < 0 || rowIndex >= tableRings.size())
                return null;
            if (columnIndex < 0 || columnIndex >= columns.size())
                return null;
            TableRing tableRing = tableRings.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    return rowIndex+1;
                case 1:
                    return tableRing.getName();
                case 2:
                    return tableRing.getShowDampers();
                case 3:
                    return tableRing.getShowInputs();
                case 4:
                    return tableRing.getShowOutputs();
                case 5:
                    return tableRing.getHost();
                default:
    //              TODO HARDCODE! broj kolona 
    //              return tableRing.getTableAlarms().contains(RootData.getRootData().getEditableRingView().getTableAlarms().get(columnIndex-6));
                    if (tableRing.getTableAlarms().containsKey(columnIndex-RingTablePanel.COLUMN_NUMBER + 1)) {
                        return tableRing.getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER + 1);
                    }
                    return false;
            }
        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#isCellEditable(int, int)
         */
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            switch (columnIndex) {
                case 0:
                    return false;
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                    return true;
                default:
                    return true;
            }

        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#removeTableModelListener(javax.swing.event.TableModelListener)
         */
        public void removeTableModelListener(TableModelListener l) {
            // TODO Auto-generated method stub

        }

        /* (non-Javadoc)
         * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
         */
        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (tableRings == null)
                return ;
            if (aValue == null) {
                return;
            }
            if (rowIndex < 0 || rowIndex >= tableRings.size())
                return ;
            if (columnIndex < 0 || columnIndex >= columns.size())
                return ;
            TableRing tableRing = tableRings.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    break;
                case 1:
                    tableRing.setName((String)aValue);
                    break;
                case 2:
                    tableRing.setShowDampers((Boolean)aValue);
                    break;
                case 3:
                    tableRing.setShowInputs((Boolean)aValue);
                    break;
                case 4:
                    tableRing.setShowOutputs((Boolean)aValue);
                    break;
                case 5:
                    tableRing.setHost((String)aValue);
                    break;
                default:
    //              HARDCODE! broj kolona 
    //              TableAlarm tableAlarm = RootData.getRootData().getEditableRingView().getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER);
    //              if (tableAlarm != null && tableRing.getTableAlarms().containsKey(tableAlarm.getId())) {
    //                  tableRing.getTableAlarms().remove(tableAlarm);
    //              } else if (tableAlarm != null && tableRing.getTableAlarms().size() < RootData.getRootData().getMaxNoOfAlarms()){
    //                  tableRing.getTableAlarms().put(tableAlarm.getId(), true);
    //              } else {
    //                  Actions.showErrorDialog(RootData.getRootData().getResource("Error.TooManyAlarms", "Too many alarms selected in ring! Maximum is:") + " " + RootData.getRootData().getMaxNoOfAlarms(), RootData.getRootData().getResource("Error.Default", "Error"));
    //              }
    //              return ;
                    if (tableRing.getTableAlarms().containsKey(columnIndex-RingTablePanel.COLUMN_NUMBER + 1)) {
                        tableRing.getTableAlarms().put(columnIndex-RingTablePanel.COLUMN_NUMBER + 1, !tableRing.getTableAlarms().get(columnIndex-RingTablePanel.COLUMN_NUMBER + 1));
                    } else {
                        tableRing.getTableAlarms().put(columnIndex-RingTablePanel.COLUMN_NUMBER + 1, true);
                    }

                    return;
            }
        }


        // Non-interface methods

        /**
         * This method adds a new column to the table, if no columns were defined, a new list is created
         */
        private void addColumn(String columnName) {
            if (columns == null)
                columns = new LinkedList<String>();
            columns.add(columnName);
        }

        /**
         * This method creates a new ring row
         * 
         * @return <b>true</b> - always
         */
        public void addRow(int i) {
            tableRings.add(i, new TableRing(""));
        }


        public boolean addRow(TableRing tableRing) {
            return tableRings.add(tableRing);
        }


        /**
         * This method removes a single row from the table
         * @param i the index of the row to be removed 
         */
        public void removeRow(int i) {
            tableRings.remove(i);
        }

        public void setColumns(List<String> columns) {
            this.columns = columns;

            int noAlarms = RootData.getRootData().getEditableRingView().getTableAlarms().size();
            for (int i=1; i<=noAlarms; i++) {
                addColumn("A_" + i);
            }

        }


    }

如何为列“A_number”(A_1,A_2 ......列表中)设置100px的大小?

1 个答案:

答案 0 :(得分:8)

来自:http://download.oracle.com/javase/tutorial/uiswing/components/table.html

要自定义初始列宽,可以在每个表的列上调用setPreferredWidth。这将设置列的首选宽度及其近似相对宽度。例如,将以下代码添加到SimpleTableDemo使其第三列比其他列更大:

TableColumn column = null;
for (int i = 0; i < 5; i++) {
    column = table.getColumnModel().getColumn(i);
    if (i == 2) {
        column.setPreferredWidth(100); //third column is bigger
    } else {
        column.setPreferredWidth(50);
    }
}

因此,遍历您的列列表,并为您想要设置的每个列设置首选宽度为100。