使特定列中的 JTable 单元格可滚动

Make JTable cells in specific column scrollable

我正在使用 swing 在 java 中创建 GUI。我使用的 JTable 看起来像这样。

我需要使最后一列(“Popis”)的单元格可滚动,因为会有描述,因此包含此描述的字符串每行的长度可变。 这就是我创建 JTable 的方式。

this.tableModel = new DefaultTableModel(
                new Object[][] { },
                new String[] {"ID", "Nazov", "Naplnena kapacita / kapacita", "Popis"}
        );
this.table1.setModel(this.tableModel);

我使用 this.tablemodel.addRow(); 将数据添加到 table 有没有办法让我的 table 最后一列的单元格可滚动?

在 JTable 中嵌入滚动组件既困难又笨拙。显示长单元格值的常用方法是使单元格的工具提示(悬停文本)显示完整值:

table1.setModel(tableModel);

table1.getColumnModel().getColumn(3).setCellRenderer(
    new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table,
                                                       Object value,
                                                       boolean selected,
                                                       boolean focused,
                                                       int row,
                                                       int column) {

            Component component = super.getTableCellRendererComponent(
                table, value, selected, focused, row, column);

            if (component instanceof JComponent) {
                ((JComponent) component).setToolTipText(
                    Objects.toString(value, null));
            }

            return component;
        }
    });

(Objects 是 java.util.Objects class。)

可以通过创建扩展 AbstractCellEditor 并实现 TableCellEditor 的私有 class 来解决。然后创建和自定义滚动条。这是例子。

public class MainForm {
    private JTable table1;
    private JPanel panel1;
    private JTextArea text = new JTextArea();

    public MainForm() {
        text.setEditable(false);
        DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{}, new String[]{"Meno", "Priezvisko", "Datum", "Popis"});
        table1.setModel(tableModel);
        table1.setRowHeight(40);
        tableModel.addRow(new Object[]{"Jaimi", "Parker", "2022", "fdjfadufaouifasoifjasifhasiofa \nasdasdasdasdasdasdas \nasdasdasdasdasdasd\nasdasdasdasd "});
        tableModel.addRow(new Object[]{"William", "Mcdonald", "2021", "fdjfadufaouasfadfdsfafifasoifjasifhasiofa"});
        tableModel.addRow(new Object[]{"Matt", "Ashley", "2020", "asfasfafdsfgdafgdfgasgsdg"});
        tableModel.addRow(new Object[]{"Ariana", "Burns", "2019", "fdjfadufaouifasfdsgdgagsgsdgsdgsdagsdgsdgsdgsagsdgoifjasifhasiofa"});

        TableColumn column = table1.getColumn("Popis");
        column.setCellEditor(new HScrollableTCE());
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("MainForm");
        frame.setContentPane(new MainForm().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    private class HScrollableTCE extends AbstractCellEditor implements TableCellEditor
    {
        JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex)
        {
            if (value != null) {
                scroll.setHorizontalScrollBar(scroll.createHorizontalScrollBar());
                scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 5));

                scroll.setBorder(new EmptyBorder(0,0,0,0));
                scroll.setToolTipText(value.toString());
                text.setText(value.toString());
                return scroll;
            }
            return null;
        }
        public Object getCellEditorValue()
        {
            return text.getText();
        }
    }

}