0

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

このコードを実行すると、何らかの理由で 、テーブルに8列が必要なため、奇妙なjava.lang.ArrayIndexOutOfBoundsException: 2 >= 0 コードでこの例外が発生します。table.getColumnModel().getColumn(2).setCellRenderer(new VariableRowHeightRenderer());

誰かが私が間違っていることを知っていますか?

関連するコードのみを表示していることに注意してください

4

3 に答える 3

2

問題はここから来ます:

table.setModel(new DefaultTableModel());

JTableに空のモデルを設定したため、作成した暗黙のモデルをJTableコンストラクターで上書きします。その行を削除するだけで、8列になるはずです。

ところで、invokeLaterで呼び出しをラップする必要はありません。

于 2012-10-03T08:39:55.287 に答える
2

以下は、高さ調整を提供する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);
    }
}
于 2012-10-03T09:11:22.663 に答える
1
  • コード行を削除、無効化、または変更するtable.setColumnSelectionAllowed(

  • 値(または)ApplicationStrings.XXXを返すグローバル変数であると仮定しますString""

于 2012-10-03T08:29:02.143 に答える