javaswingjtable

How to word wrap inside a JTable row


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

Solution

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