簡體   English   中英

在JTable上動態調整行高

[英]Adjusting row height dynamically on JTable

我在Stackoverflow上發現了類似的問題,但是由於某種原因,當我嘗試實現建議的內容時,出現了一個奇怪的異常。

因此,我試圖動態調整3列中的某些高度。

public class AcquisitionTechniquesPanel extends JPanel {
    private static final long serialVersionUID = -3326535610858334494L;

    public static final int SIZE_OF_TABLE = 8;

    private final JTable table;
    private JCheckBox acquisitionTechniquesDone;

    private Object[][] tableData;
    private final String[] columnNames;

    public AcquisitionTechniquesPanel() {
        this.columnNames = new String[] { ApplicationStrings.ID, ApplicationStrings.TYPE, "Foo", "Bar", "Biz", "Baz", "Boz", ApplicationStrings.NO_OF_AR_S };
        this.table = new JTable(tableData, columnNames);

        initGUI();
    }


    public void initGUI() {
      table.setColumnSelectionAllowed(
      table.setDragEnabled(false);
      table.setOpaque(true);
      table.getTableHeader().setReorderingAllowed(false);
      table.setModel(new DefaultTableModel());
      JScrollPane scrollPane = new JScrollPane(table);
      scrollPane.setPreferredSize(new Dimension(800, 320));

      SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                table.getColumnModel().getColumn(2).setCellRenderer(new VariableRowHeightRenderer());
                table.getColumnModel().getColumn(3).setCellRenderer(new VariableRowHeightRenderer());
                table.getColumnModel().getColumn(4).setCellRenderer(new VariableRowHeightRenderer());
            }
        });
    }

public static class VariableRowHeightRenderer extends JLabel implements TableCellRenderer {
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setText(String.valueOf(value));
            if(getPreferredSize().height > 1)
                table.setRowHeight(row, getPreferredSize().height);
            return this;
        }
    }
}

現在,當我運行這段代碼時,由於某種原因,我得到了java.lang.ArrayIndexOutOfBoundsException: 2 >= 0我在代碼table.getColumnModel().getColumn(2).setCellRenderer(new VariableRowHeightRenderer());上得到了這個異常table.getColumnModel().getColumn(2).setCellRenderer(new VariableRowHeightRenderer()); 這很奇怪,因為表應該有8列。

有人知道我在做什么錯嗎?

請注意,我只顯示相關代碼

問題來自這里:

table.setModel(new DefaultTableModel());

您在JTable上設置了一個空模型,因此覆蓋了使用JTable構造函數創建的隱式模型。 只需刪除該行,您應該擁有8列。

順便說一句,您無需將調用包裝在invokeLater中。

以下是SwingX的TableUtilies的摘錄,其中提供了高度調整功能。 你用它像

this.table = new JTable(tableData, columnNames);
// initial sizing
TableUtilites.setPreferredRowHeights(table);
TableModelListener l = new TableModelListener() {
     public void tableChanged(...) {
         // dynamic sizing on changes
         SwingUtilities.invokeLater() {
              public void run() {
                  TableUtilities.setPreferredRowHeights(table);
              }
         };
     }
}; 
table.getModel().addTableModelListener(l);

TableUtilities的實用程序方法:

/**
 * Returns the preferred height for the given row. It loops
 * across all visible columns and returns the maximal pref height of
 * the rendering component. Falls back to the table's base rowheight, i
 * f there are no columns or the renderers
 * max is zeor.<p>
 * 
 * @param table the table which provides the renderers, must not be null
 * @param row the index of the row in view coordinates
 * @return the preferred row height of
 * @throws NullPointerException if table is null.
 * @throws IndexOutOfBoundsException if the row is not a valid row index
 */
public static int getPreferredRowHeight(JTable table, int row) {
    int pref = 0;
    for (int column = 0; column < table.getColumnCount(); column++) {
        TableCellRenderer renderer = table.getCellRenderer(row, column);
        Component comp = table.prepareRenderer(renderer, row, column);
        pref = Math.max(pref, comp.getPreferredSize().height);
    }
    return pref > 0 ? pref : table.getRowHeight();
}

/**
 * 
 * @param table the table which provides the renderers, must not be null
 * @param row the index of the row in view coordinates
 * @throws NullPointerException if table is null.
 * @throws IndexOutOfBoundsException if the row is not a valid row index
 */
public static void setPreferredRowHeight(JTable table, int row) {
    int prefHeight = getPreferredRowHeight(table, row);
    table.setRowHeight(row, prefHeight);
}

/**
 * Sets preferred row heights for all visible rows. 
 * 
 * @param table the table to set row heights to
 * @throws NullPointerException if no table installed.
 */
public static void setPreferredRowHeights(JTable table) {
    // care about visible rows only
    for (int row = 0; row < table.getRowCount(); row++) {
        setPreferredRowHeight(table, row);
    }
}
  • 刪除,禁用或更改代碼行表table.setColumnSelectionAllowed(

  • 假設ApplicationStrings.XXX是返回String值(或"" )的全局變量

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM