簡體   English   中英

Java JTable 設置列寬

[英]Java JTable setting Column Width

我有一個 JTable,我在其中設置列大小如下:

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(400);

這工作正常,但是當表格最大化時,我在最后一列的右側獲得空白空間。 調整大小時是否可以將最后一列調整到窗口的末尾?

我在文檔中找到了AUTO_RESIZE_LAST_COLUMN屬性,但它不起作用。

編輯: JTable位於JScrollPane其首選大小已設置。

如果在最后一列調用setMinWidth(400)而不是setPreferredWidth(400)什么?

JTable的 JavaDoc 中,非常仔細地閱讀doLayout()的文檔。 以下是一些選擇位:

當由於調整封閉窗口的大小而調用該方法時,resizingColumn 為空。 這意味着調整大小已發生在 JTable 的“外部”,並且無論此 JTable 的自動調整大小模式如何,更改(或“增量”)都應分布到所有列。

這可能就是AUTO_RESIZE_LAST_COLUMN沒有幫助你的原因。

注意:當 JTable 調整列的寬度時,它絕對尊重它們的最小值和最大值。

這表示您可能希望為除最后一列之外的所有列設置 Min == Max,然后在最后一列上設置 Min = Preferred,並且要么不設置 Max,要么為 Max 設置一個非常大的值。

使用JTable.AUTO_RESIZE_OFF ,表不會為您更改任何列的大小,因此它將采用您的首選設置。 如果您的目標是讓列默認為您的首選大小,除了讓最后一列填充窗格的其余部分,您可以選擇使用JTable.AUTO_RESIZE_LAST_COLUMN autoResizeMode,但與TableColumn.setMaxWidth()使用時它可能最有效TableColumn.setMaxWidth()而不是 TableColumn.setPreferredWidth() 除了最后一列。

一旦您對AUTO_RESIZE_LAST_COLUMN確實有效感到滿意,您就可以試驗TableColumn.setMaxWidth()TableColumn.setMinWidth()

JTable.AUTO_RESIZE_LAST_COLUMN定義為“在所有調整大小操作期間,僅對最后一列應用調整”這意味着您必須在代碼末尾設置autoresizemode ,否則 setPreferredWidth() 不會影響任何內容!

所以在你的情況下,這將是正確的方法:

table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(400);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

使用這個方法

public static void setColumnWidths(JTable table, int... widths) {
    TableColumnModel columnModel = table.getColumnModel();
    for (int i = 0; i < widths.length; i++) {
        if (i < columnModel.getColumnCount()) {
            columnModel.getColumn(i).setMaxWidth(widths[i]);
        }
        else break;
    }
}

或者擴展JTable類:

public class Table extends JTable {
    public void setColumnWidths(int... widths) {
        for (int i = 0; i < widths.length; i++) {
            if (i < columnModel.getColumnCount()) {
                columnModel.getColumn(i).setMaxWidth(widths[i]);
            }
            else break;
        }
    }
}

進而

table.setColumnWidths(30, 150, 100, 100);

閱讀 Kleopatra 的評論(她第二次建議看一下javax.swing.JXTable ,現在很抱歉我第一次沒有看:))我建議你點擊鏈接

對於同樣的問題,我有這個解決方案:(但我建議你按照上面的鏈接)在調整表格大小時,將表格列的寬度縮放到當前表格的總寬度。 為此,我使用全局整數數組作為(相對)列寬):

private int[] columnWidths=null;

我使用這個函數來設置表格列寬:

public void setColumnWidths(int[] widths){
    int nrCols=table.getModel().getColumnCount();
    if(nrCols==0||widths==null){
        return;
    }
    this.columnWidths=widths.clone();

    //current width of the table:
    int totalWidth=table.getWidth();
    
    int totalWidthRequested=0;
    int nrRequestedWidths=columnWidths.length;
    int defaultWidth=(int)Math.floor((double)totalWidth/(double)nrCols);

    for(int col=0;col<nrCols;col++){
        int width = 0;
        if(columnWidths.length>col){
            width=columnWidths[col];
        }
        totalWidthRequested+=width;
    }
    //Note: for the not defined columns: use the defaultWidth
    if(nrRequestedWidths<nrCols){
        log.fine("Setting column widths: nr of columns do not match column widths requested");
        totalWidthRequested+=((nrCols-nrRequestedWidths)*defaultWidth);
    }
    //calculate the scale for the column width
    double factor=(double)totalWidth/(double)totalWidthRequested;

    for(int col=0;col<nrCols;col++){
        int width = defaultWidth;
        if(columnWidths.length>col){
            //scale the requested width to the current table width
            width=(int)Math.floor(factor*(double)columnWidths[col]);
        }
        table.getColumnModel().getColumn(col).setPreferredWidth(width);
        table.getColumnModel().getColumn(col).setWidth(width);
    }
    
}

設置數據時我調用:

    setColumnWidths(this.columnWidths);

在更改時,我將 ComponentListener 設置為表的父級(在我的情況下,JScrollPane 是我的表的容器):

public void componentResized(ComponentEvent componentEvent) {
    this.setColumnWidths(this.columnWidths);
}

請注意,JTable 表也是全局的:

private JTable table;

在這里我設置了監聽器:

    scrollPane=new JScrollPane(table);
    scrollPane.addComponentListener(this);
fireTableStructureChanged();

將默認調整大小行為 如果在您設置列調整大小屬性后在代碼中的某處調用此方法,則所有設置都將被重置。 這種副作用可以間接發生。 Fe 作為鏈接數據模型的結果,在設置屬性后以調用此方法的方式更改。

不需要該選項,只需將最后一列的首選寬度設為最大值,它將占用所有額外空間。

table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(Integer.MAX_INT);

此代碼適用於沒有 setAutoResizeModes 的我。

        TableColumnModel columnModel = jTable1.getColumnModel();
        columnModel.getColumn(1).setPreferredWidth(170);
        columnModel.getColumn(1).setMaxWidth(170);
        columnModel.getColumn(2).setPreferredWidth(150);
        columnModel.getColumn(2).setMaxWidth(150);
        columnModel.getColumn(3).setPreferredWidth(40);
        columnModel.getColumn(3).setMaxWidth(40);

使用此代碼。 它對我有用。 我考慮了 3 列。 更改代碼的循環值。

TableColumn column = null;
for (int i = 0; i < 3; i++) {
    column = table.getColumnModel().getColumn(i);
    if (i == 0) 
        column.setMaxWidth(10);
    if (i == 2)
        column.setMaxWidth(50);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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