質問

これは存在しないか、遅れているため正しく考え/検索していません...

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

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

役に立ちましたか?

解決

実行時に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);        
}

他のヒント

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

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

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

ラベルを作成する代わりに、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 にこの例があります。

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

JTableの行の高さは、定義済みの標準。ただし、JTableがセル間に少し間隔を空けていることを考えると、少し実験が必要かもしれません。

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top