4

これが存在しないか、遅れているために正しく考えていない/検索していない...

特定の列に存在すると予想される (またはわかっている) 最大の文字列のプロトタイプ値に基づいて、Swing で JTable の列幅を設定したいと考えています。コンパイル時にフォントが必ずしもわからないため、ピクセル数はわかりません。

列幅の目的でプロトタイプ値を設定する方法はありますか? 行の高さの目的のための方法はありますか? もしそうなら、どのように?

4

5 に答える 5

3

次のコードを試すことができます。

/**
 * Sets the preferred width of the columns of a table from prototypes
 * @param table the target table
 * @param prototypes an array of prototypes, {@code null} values will be ignored
 * @param setMaxWidth {@code true} if the maximum column width should also be set
 */
public static void setWidthFromPrototype(JTable table, Object[] prototypes, boolean setMaxWidth) {
if (prototypes.length != table.getColumnCount())
  throw new IllegalArgumentException("The prototypes array should contain exactly one element per table column");
for (int i = 0; i < prototypes.length; i++) {
    if (prototypes[i] != null) {
        Component proto = table.getCellRenderer(0,i)
                .getTableCellRendererComponent(table, prototypes[i], false, false, 0, i);
        int prefWidth = (int) proto.getPreferredSize().getWidth() + 1;
        table.getColumnModel().getColumn(i).setPreferredWidth(prefWidth);
        if (setMaxWidth)
            table.getColumnModel().getColumn(i).setMaxWidth(prefWidth);
    }
}
}
于 2012-03-28T08:38:48.403 に答える
1

SwingX の拡張 JXTable/Column サポートは、最初の列幅のサイズ設定のプロトタイプを設定します。これは、列が作成された後で行うことができます。

for(int col = 0; ....) {
    table.getColumnExt(col).setPrototypeValue(myPrototype[col]
}

または、作成時に列を構成するカスタム ColumnFactory を実装することによって

ColumnFactory factory = new ColumnFactory() {
    @Override
    protected void configureTableColumn(TableModel model, TableColumnExt columnExt) {
        super(...);
        columnExt.setPrototypeValue(myPrototype[columnExt.getModelIndex()];
    }
}
table.setColumnFactory(factory);
table.setModel(myModel);
于 2012-03-28T12:04:53.613 に答える
1

実行時に JLabel を作成し、そのサイズを使用してテーブルのサイズを変更しようとしましたか?

// create a label that will be using the run-time font
JLabel prototypeLabel = new JLabel("Not Applicable")

// get the labels preferred sizes
int preferredWidth = prototypeLabel.getPreferredSize().getWidth();
int preferredHeight = prototypeLabel.getPreferredSize().getHeight();

// set the sizes of the table's row and columns
myTable.setRowHeight(preferredHeight);

for(TableColumn column : myTable.getColumnModel.getColumns()){
   column.setPreferredWidth(preferredWidth);        
}
于 2009-05-09T13:10:09.253 に答える
0

ラベルを作成する代わりに、TableCellRendererから実際のコンポーネントを取得し、そのサイズをテストします。

// create the component that will be used to render the cell
Comp prototype = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, "Not Applicable", false, false, 0, i);

// get the labels preferred sizes
int preferredWidth = comp.getPreferredSize().getWidth();
int preferredHeight = comp.getPreferredSize().getHeight();

これは単一列の例です。これを繰り返して、各列のサイズを取得する(および設定する)必要があります。この例については、http://www.exampledepot.com/egs/javax.swing.table/PackCol.htmlを参照してください。

于 2010-07-13T22:33:16.430 に答える
-1

コンパイル時にフォントがわからない場合、JTable 列の幅は常に不明になります。健全性チェックとして、テキスト ドキュメントを開いて、ポイント サイズを一定に保ちながら、さまざまなフォントを試してみてください。文字の長さはフォントごとに異なりますが、高さは変わりません。

JTable 行の高さは、定義済みの標準であるため、任意のフォント サイズ (ポイント単位) で決定できる必要があります。ただし、JTable がセル間に少しのスペースを与える可能性があることを考えると、少し実験する必要があるかもしれません。

コンパイル時にフォントサイズもフォント自体も保証できない場合は、他の人がどのような答えを出すかに興味があります:)

于 2009-05-08T04:29:47.410 に答える