如何让JTable中的所有列自动调整为相同的大小?

4
我正在一个项目中使用JTable,我想让表格自动调整所有列的大小,并同时填充包含JScrollPane的宽度。
我不能只设置首选宽度为固定值,因为当用户调整框架大小时,我希望所有列都被调整为相同的大小,但必须仍然填满JScrollPane的100%。
例如,如果JScrollPane的宽度为1000,我有10列,我希望每列的大小都为100。如果JScrollPane缩小到500,则希望每列调整为50。
目前,我只是设置了JTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF),这是结果:
如何简单实现呢?
谢谢。

2
“我怎样才能简单地实现这个?” - 你不能,不是那么简单的。你需要动手去做... - MadProgrammer
您也可以查看表列调整器 - MadProgrammer
@MadProgrammer:感谢你提供的链接和回答。我会采纳这个方案。这样我的手就不会那么脏了 :p - adrien.pain
4个回答

3

当表格的大小发生变化时,您需要一种更新列模型的方法。

这个基本示例使用invaldiate方法来更新其列模型。它还将列设置为“不可调整大小”。

这还覆盖了表格的getScrollableTracksViewportWidth方法,以确保表格自动填充水平空间。这意味着它永远不会显示水平滚动条。

public class SpringTable extends JTable {

    public SpringTable(TableModel dm) {
        super(dm);
        setAutoResizeMode(AUTO_RESIZE_OFF);
    }

    public SpringTable() {
        setAutoResizeMode(AUTO_RESIZE_OFF);
    }

    @Override
    public void doLayout() {
        int width = getWidth();
        int columnCount = getColumnCount();
        int columnSize = width / columnCount;
        for (int index = 0; index < columnCount; index++) {
            TableColumn column = getColumnModel().getColumn(index);
            column.setResizable(false);
            column.setPreferredWidth(width);
        }
        super.doLayout();
    }

    @Override
    public boolean getScrollableTracksViewportWidth() {
        return true;
    }

}

使用doLayout代替invalidate可能更好,但您应该试一下并了解哪种方法更符合您的需求。

运行示例

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

public class TestJTable {

    public static void main(String[] args) {
        new TestJTable();
    }

    public TestJTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                List<Pet> pets = new ArrayList<>(25);
                pets.add(new Pet("Tyrannosauridae", "TYRANNOSAURUS", 20, 35));
                pets.add(new Pet("Dromaeosauridae", "VELOCIRAPTOR", 45, 90));
                pets.add(new Pet("Ceratopsidae", "TRICERATOPS", 15, 30));
                pets.add(new Pet("Stegosauridae", "STEGOSAURUS", 22, 25));
                pets.add(new Pet("Titanosauridae", "MALAWISAURUS", 22, 25));
                pets.add(new Pet("Compsognathidae", "COMPSOGNATHUS", 8, 25));
                pets.add(new Pet("Brachiosauridae", "BRACHIOSAURUS", 8, 25));
                pets.add(new Pet("Diplodocidae", "DIPLODOCUS", 8, 25));

                final PetTableModel model = new PetTableModel(pets);
                final JTable table = new SpringTable(model);

                InputMap im = table.getInputMap(JTable.WHEN_FOCUSED);
                ActionMap am = table.getActionMap();
                im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
                am.put("delete", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int[] indicies = table.getSelectedRows();
                        int[] mapped = new int[indicies.length];
                        for (int index = 0; index < indicies.length; index++) {
                            mapped[index] = table.convertRowIndexToModel(indicies[index]);
                        }
                        model.removePets(mapped);
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class SpringTable extends JTable {

        public SpringTable(TableModel dm) {
            super(dm);
            setAutoResizeMode(AUTO_RESIZE_OFF);
        }

        public SpringTable() {
            setAutoResizeMode(AUTO_RESIZE_OFF);
        }

        @Override
        public void doLayout() {
            int width = getWidth();
            int columnCount = getColumnCount();
            int columnSize = width / columnCount;
            for (int index = 0; index < columnCount; index++) {
                TableColumn column = getColumnModel().getColumn(index);
                column.setResizable(false);
                column.setPreferredWidth(width);
            }
            super.doLayout();
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return true;
        }

    }

    public class PetTableModel extends AbstractTableModel {

        private List<Pet> pets;

        public PetTableModel() {
            pets = new ArrayList<>(25);
        }

        public PetTableModel(List<Pet> pets) {
            this.pets = pets;
        }

        @Override
        public int getRowCount() {
            return pets.size();
        }

        public void removePets(int... indicies) {
            List<Pet> old = new ArrayList<>(indicies.length);
            for (int index : indicies) {
                old.add(pets.get(index));
            }

            for (Pet pet : old) {
                int index = pets.indexOf(pet);
                pets.remove(pet);
                fireTableRowsDeleted(index, index);
            }
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            Class clazz = String.class;
            switch (columnIndex) {
                case 2:
                case 3:
                    clazz = Float.class;
            }
            return clazz;
        }

        @Override
        public String getColumnName(int column) {
            String name = "??";
            switch (column) {
                case 0:
                    name = "Breed";
                    break;
                case 1:
                    name = "Category";
                    break;
                case 2:
                    name = "Buy Price";
                    break;
                case 3:
                    name = "Sell Price";
                    break;
            }
            return name;
        }

        @Override
        public int getColumnCount() {
            return 4;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Pet pet = pets.get(rowIndex);
            Object value = null;
            switch (columnIndex) {
                case 0:
                    value = pet.getBreed();
                    break;
                case 1:
                    value = pet.getCategory();
                    break;
                case 2:
                    value = pet.getBuyPrice();
                    break;
                case 3:
                    value = pet.getSellPrice();
                    break;
            }
            return value;
        }

        public void add(Pet pet) {
            pets.add(pet);
            fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
        }

    }

    public class Pet {

        private String breed;
        private String category; 
        private float buyPrice; 
        private float sellPrice; 

        public Pet(String breed, String category, float buyPrice, float sellPrice) {
            this.breed = breed;
            this.category = category;
            this.buyPrice = buyPrice;
            this.sellPrice = sellPrice;
        }

        public String getBreed() {
            return breed;
        }

        public float getBuyPrice() {
            return buyPrice;
        }

        public String getCategory() {
            return category;
        }

        public float getSellPrice() {
            return sellPrice;
        }

    }

}

1
疯狂的是,这是由@camickr创建的代码,其中包含TableColumnModelListener - mKorbel
@MadProgrammer:非常感谢你的建议,我会重写invalidate()或doLayout()方法。 - adrien.pain
1
当你运行这段代码时,invalidate()方法会被调用13次。当我试图将框架的大小调整1像素时,它又被调用了9次。如果这段代码确实是必需的(请参见我的另一个答案中的发现),那么最好添加一个ComponentListener到表格中,并监听componentResized()事件。或者也许doLayout()没有被频繁调用。 - camickr
@camickr 是的,我考虑过使用ComponentListener,通常情况下使用doLayout可能更好。 - MadProgrammer

1
据我所知,在Windows 7上使用JDK7时,JTable的默认行为是在表格大小改变时等分每列的大小。只要您没有在表格列上设置首选宽度,这似乎可以正常工作。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

public class TestJTable {

    public static void main(String[] args) {
        new TestJTable();
    }

    public TestJTable() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }


                String[] columnNames = {"Column1", "a", "a long long column name", "column4"};
                DefaultTableModel model = new DefaultTableModel(columnNames, 5);
                final JTable table = new JTable(model);

                JButton button = new JButton("Display Column Widths");
                button.addActionListener( new ActionListener()
                {
                    public void actionPerformed(ActionEvent e)
                    {
                        int columnCount = table.getColumnCount();

                        for (int index = 0; index < columnCount; index++)
                        {
                            TableColumn column = table.getColumnModel().getColumn(index);
                            System.out.print(column.getWidth() + ", ");
                        }

                        System.out.println();
                    }
                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.add(button, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

0

我在上一个项目中使用了这个!

addComponentListener(new ComponentListener() {
    @Override public void componentShown(ComponentEvent e) {}
    @Override public void componentMoved(ComponentEvent e) {}           
    @Override public void componentHidden(ComponentEvent e) {}

    @Override
    public void componentResized(ComponentEvent e) {
        int w = table.getWidth() / table.getColumnCount();
        for(int i = 0; i < table.getColumnCount(); i++) {
            table.getColumn(i).setPreferredWidth(w);
        }
    }
});

0

调整 JTable 大小后,您可以获取新的宽度(JTable.getWidth()),除以 10(进行必要的调整以获得整数结果),然后手动设置每列的宽度(请参见 this 是否有帮助)。


是的,可以那样做,但我会选择MadProgrammer给我的解决方案,我更喜欢直接覆盖JTable的方法,以保持代码在一个地方 :) - adrien.pain

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接