How to word wrap inside a JTable row

北慕城南 提交于 2019-12-31 00:30:10

问题


I have a simple JTable that shows the details (in column format) of a row from another JTable. This works nicely. However, sometimes the text in a row is very long so the user ends up having to scroll across which isnt neat.

How can I wrap the text in a row and allow the row height to change to show all the text in it.

Here is the code:

 table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int selectedRow = table.getSelectedRow();
                    DefaultTableModel newModel = new DefaultTableModel();
                    String rowName = "Row: " + selectedRow;
                    newModel.setColumnIdentifiers(new Object[]{rowName});
                    for (int i = 0; i < table.getModel().getColumnCount(); i++) {
                        newModel.addRow(new Object[]{table.getModel().getValueAt(selectedRow, i)});
                    }
                    JTable newTable = new JTable(newModel) {
                        /**
                         * 
                         */
                        private static final long serialVersionUID = 1L;

                        @Override
                        public Dimension getPreferredScrollableViewportSize() {
                            return new Dimension(140, 240);
                        }
                    };
                    newTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    newTable.setRowHeight(14, 30);
                    TableColumnAdjuster tcanewTable = new TableColumnAdjuster(newTable);        
                    tcanewTable.setColumnHeaderIncluded(true);
                    tcanewTable.setColumnDataIncluded(true);
                    tcanewTable.setOnlyAdjustLarger( true );
                    tcanewTable.setDynamicAdjustment( true );
                    tcanewTable.adjustColumns();

                    // Apply any custom renderers and editors
                    JOptionPane.showMessageDialog(frame, new JScrollPane(newTable),
                        rowName, JOptionPane.PLAIN_MESSAGE);
                }
            }
        });

回答1:


You can accomplish this by using a JTextArea as a TableCellRenderer for that column in your table. For example:

static class WordWrapCellRenderer extends JTextArea implements TableCellRenderer {
    WordWrapCellRenderer() {
        setLineWrap(true);
        setWrapStyleWord(true);
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setText(value.toString());
        setSize(table.getColumnModel().getColumn(column).getWidth(), getPreferredSize().height);
        if (table.getRowHeight(row) != getPreferredSize().height) {
            table.setRowHeight(row, getPreferredSize().height);
        }
        return this;
    }
}

To use WordWrapCellRenderer in your table:

table.getColumnModel().getColumn(columnIndex).setCellRenderer(new WordWrapCellRenderer());


来源:https://stackoverflow.com/questions/37768335/how-to-word-wrap-inside-a-jtable-row

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