如何获得文本在 JTable 单元格中的确切 space?

How to get the exact space that text takes inside a cell of a JTable?

我在 JScrollPane 中有一行 header,我应该根据文本的最大宽度设置其宽度。我设法做到了,但我必须在对 setPreferredScrollableViewportSize 的调用中添加 1 才能显示全文。我测量错了吗?或者这个数量是固定的?

这是主要的class。

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;

public class Example {
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                JTable table = new JTable(5, 3);
                table.setPreferredScrollableViewportSize(table.getPreferredSize());
                HeaderColumn ch = new HeaderColumn(5, 1);
                JFrame frame = new JFrame();
                JScrollPane sp = new JScrollPane(table);
                sp.setRowHeaderView(ch);
                frame.add(sp);
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                ch.adjustSize();
            }
        });
    }
}

这是headerclass。请看adjustSize().

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FontMetrics;
import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;


public class HeaderColumn extends JTable{
    private final int INSET = 2;

    public HeaderColumn(int r, int c){
        super(r,c);
    }


    @Override
    public void columnAdded(TableColumnModelEvent e){
        DefaultTableModel m = (DefaultTableModel)getModel();
        int rowCount = m.getRowCount();
        for(int i = 0; i < rowCount; i++){
            m.setValueAt("#" + Integer.toString(i+1), i, 0);
        }
        TableColumn tc = getColumnModel().getColumn(e.getToIndex());
        tc.setCellRenderer(new DefaultTableCellRenderer(){
            @Override
            public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
           {
                setText((value == null) ? "" : value.toString());
                setBorder(BorderFactory.createEmptyBorder(0, INSET, 0, INSET));
                return this;
           }
        });
    }

    public void adjustSize(){
        FontMetrics fm = getGraphics().getFontMetrics(getTableHeader().getFont());
        int maxTextWidth = 0;
        DefaultTableModel m = (DefaultTableModel)getModel();
        int rowCount = m.getRowCount();
        for(int i = 0; i < rowCount; i ++){
            int width = fm.stringWidth(m.getValueAt(i, 0).toString());
            if(width > maxTextWidth){
                maxTextWidth = width;
            }
        }        
        setPreferredScrollableViewportSize(new Dimension(maxTextWidth+INSET*2+1, 0));
    }  
}

I managed to do so but I have to add 1 in my call to setPreferredScrollableViewportSize in order to display the full text. Did I get the measurement wrong? Or is this quantity fixed?

我认为您需要包括从 table 返回的 getIntercellSpacing() 值。

查看 Table Column Adjuster 了解更多信息和确定列宽的示例代码。