JTable column width

浪子不回头ぞ 提交于 2019-12-12 00:28:35

问题


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.

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

回答1:


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


来源:https://stackoverflow.com/questions/17256219/jtable-column-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!