简体   繁体   English

设置JTable列的原型值(用于自动宽度计算)

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

either this doesn't exist, or I'm not thinking/searching correctly because it's late... 或者这不存在,或者我没有正确思考/搜索,因为它已经晚了...

I want to set a JTable column width in Swing based on a prototype value for the largest string I expect (or know) to exist in a particular column. 我想根据我希望(或知道)存在于特定列中的最大字符串的原型值,在Swing中设置JTable列宽。 I don't know the # of pixels since I don't necessarily know the font at compile time. 我不知道像素数,因为我不一定知道编译时的字体。

Is there a way to set a prototype value for column width purposes, the way there is for row height purposes? 有没有办法为列宽目的设置原型值,行高的目的是什么? If so, how? 如果是这样,怎么样?

You can try the following code: 您可以尝试以下代码:

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

Have you tried creating a JLabel at run time and using its size to resize your table? 您是否尝试在运行时创建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 extended JXTable/Column support setting prototypes for initial column width sizing, you can do so either after the columns have been created SwingX扩展了JXTable / Column支持设置初始列宽大小的原型,您可以在创建列之后执行此操作

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

or by implementing a custom ColumnFactory which configures the columns on creation 或者通过实现自定义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);

Instead of creating a Label, get the actual component from the TableCellRenderer and test the size on that: 不是创建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();

This is a single column example, you'll need to repeat this to get the size for each column (and also set it). 这是一个单列示例,您需要重复此操作以获取每列的大小(并设置它)。 See http://www.exampledepot.com/egs/javax.swing.table/PackCol.html for an example of this. 有关此示例,请参阅http://www.exampledepot.com/egs/javax.swing.table/PackCol.html

If you don't know the font at compile time then the width of any JTable column is always going to be unknown. 如果在编译时不知道字体,那么任何JTable列的宽度总是未知的。 As a sanity check, open a text document and play with different fonts whilst keeping the point size constant. 作为完整性检查,打开文本文档并使用不同的字体进行播放,同时保持点大小不变。 The length of what is written varies on a font-by-font basis but the height doesn't. 所写内容的长度因字体而异,但高度不同。

The height of a JTable row should be able to be determined for any font size (in points) as it is a defined standard . 应该能够确定任何字体大小(以磅为单位)的JTable行的高度,因为它是定义的标准 It might take a bit of experimenting, though, given that JTable probably gives a bit of spacing inbetween cells. 但是,考虑到JTable可能在单元格之间提供一些间距,可能需要一些实验。

If you can't guarantee neither the font size nor the font itself at compile time then I'm interested in what answers other people come up with :) 如果你不能在编译时保证字体大小和字体本身,那么我对其他人提出的答案感兴趣:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM