How to wrap words inside a JTable string

I have a simple JTable that shows the details (in column format) of a row from another JTable. It works well. However, sometimes the text in the line is very long, so the user has to scroll through everything that is not neat.

How can I wrap the text in a line and let me change the height of the line 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);
                }
            }
        });
+4
source share
1 answer

You can accomplish this by using JTextAreaboth TableCellRendererfor this column in your table. For instance:

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 WordWrapCellRendererin your table:

table.getColumnModel().getColumn(columnIndex).setCellRenderer(new WordWrapCellRenderer());
+6
source

All Articles