2

I am making a dialog for the purpose of selecting multiple file paths. My dialog consists of two panels. One for buttons such as "Add" and "Remove", and a second panel containing a JTable wrapped in a scrollPane. The table has only one column. The cells of the table are not editable directly. When a user selects a file using a JFileChooser, the full path of that file will be added to the table. Although my dialog is resizeable, I still need a horizontal scroll behavior in the event that the file path is longer than the user's screen is wide.

I have researched the combination of resizeable table and horizontal scroll bar. That is similar, but not my issue. The typical scroll behavior is that the columns are scrolled, not the contents of the columns. I need the contents of a single column to scroll horizontally.

4

2 に答える 2

4

複数の列をスクロールするか、単一の列のみをスクロールするかは関係ありません。基本的な問題は、水平スクロールバーを最初に取得することです:-)

微調整するネジは2つあります。-テーブルのresizeModeを設定して水平スクロールを有効にします。デフォルトでは、テーブルのサイズを常にscrollPaneのサイズに合わせます。つまり、スクロールしません。-列の幅をコンテンツに合わせてサイズ変更します。

次のような擬似コードにマップするコアJTable内

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// on receiving a TableModelEvent which might increase the column width
// calculate new width by measuring pref of the renderer
int newWidth = ... 
// set it as pref of the column 
table.getColumnModel().getColumn(0).setPreferredWidth(newWidth);

欠点は、resizeModeがない場合は、常に列のサイズを決定する必要があるということです。列の幅がscrollPaneよりも小さく、末尾に空の領域があります。

JXTable(SwingXプロジェクトの一部)は、テーブルのprefWidtsが親の幅よりも小さい限り、使用可能な水平方向のスペースを埋め、必要に応じて水平方向のスクロールバーを表示する追加のサイズ設定モードをサポートします。

table.setHorizontalScrollEnabled(true);
// on receiving a TableModelEvent which might increase the column width
// tell the table to re-evaluate 
table.packColumn(0);
于 2012-08-30T06:31:09.483 に答える
1

テーブル操作に関する私の特定の質問に対処するため、クレオパトラの答えを正解として選択しました。根本的な問題を別の方法で解決することになったので、この回答を追加します。

単一の列テーブルではなく、JListを使用してファイルパスを表すことを選択しました。私がJTableを使用したかった唯一の本当の理由は、テーブルに並んだ行の外観と、JListに慣れていないためです。DefaultListCellRendererを拡張して、JListの外観を編集する方法を発見しました。外観の編集について知ったので、JListの自然なサイズ変更とスクロール動作により、JListは私のニーズにはるかに自然に適合しました。

于 2012-09-11T14:35:54.630 に答える