JTable列宽 [英] JTable column width

查看:82
本文介绍了JTable列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易解决,但是我看不到.下面的JPanel仅包含一个ScrollPane(包含一个表)和一个Button.

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.

我需要知道表格的实际列宽. 单击该按钮确实显示正确的值,但是内部调用只为每列输出75(默认值).如何在此处的代码中获得正确的结果?

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

修改

现在具有经过测试的代码:

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

这篇关于JTable列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆