簡體   English   中英

設置JTable列的原型值(用於自動寬度計算)

[英]setting a prototype value (for auto-width calculation) of a JTable column

或者這不存在,或者我沒有正確思考/搜索,因為它已經晚了...

我想根據我希望(或知道)存在於特定列中的最大字符串的原型值,在Swing中設置JTable列寬。 我不知道像素數,因為我不一定知道編譯時的字體。

有沒有辦法為列寬目的設置原型值,行高的目的是什么? 如果是這樣,怎么樣?

您可以嘗試以下代碼:

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

您是否嘗試在運行時創建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);        
}

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

不是創建Label,而是從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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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