简体   繁体   English

JTable列宽

[英]JTable column width

This should be easy to solve, but I just do not see it. 这应该很容易解决,但是我看不到。 The following JPanel simply contains a ScrollPane (containing a Table) and a Button. 下面的JPanel仅包含一个ScrollPane(包含一个表)和一个Button。

I need to know the actual column widths of the table. 我需要知道表格的实际列宽。 Clicking the button does show the correct values, but the internal call just outputs 75 for every column (the default value). 单击该按钮确实显示了正确的值,但是内部调用只为每列输出75(默认值)。 How can I get the correct results in the code here? 如何在此处的代码中获得正确的结果?

public MyPanel() { //JPanel

        setLayout(new BorderLayout());

        this.setBounds(0, 0, 1000, 250);

        table = new JTable(5,6);

        JScrollPane sp = new JScrollPane(table);

        this.add(sp, BorderLayout.CENTER);          


        JButton b = new JButton("Test");
            b.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    displayWidths(); //DOES WORK!
                }
            });
         this.add(b, BorderLayout.SOUTH);

         displayWidths(); //DOES NOT WORK!
}

private void displayWidths() {
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn column = table.getColumnModel().getColumn(i);
            System.out.println("Width of column " + i + " : " + column.getWidth());
        }
}

The call from the button works, because your panel/table is realized (visible on screen). 由于您的面板/表格已实现(在屏幕上可见),因此通过按钮进行的​​呼叫有效。 The inline version does not, because your table isn't realized yet. 内联版本没有,因为您的表尚未实现。

edit 编辑

Now with tested code: 现在带有经过测试的代码:

public class TestGetColumnWidths {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Columns");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

                final JTable table = new JTable(5, 6);
                table.getTableHeader().addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        super.componentResized(e);
                        displayColumnWidths(table.getTableHeader());
                    }
                });
                JPanel panel = new JPanel(new BorderLayout());
                panel.add(new JScrollPane(table));

                frame.add(panel);
                //frame.pack();
                frame.setSize(1000, 250);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

            private void displayColumnWidths(JTableHeader header) {
                TableColumnModel model = header.getColumnModel();

                for (int i = 0; i < model.getColumnCount(); i++) {
                    TableColumn column = model.getColumn(i);
                    System.err.println("column.getWidth(): " + column.getWidth());
                }
            }
        });
    }
}

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

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