Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to word wrap inside a JTable row

Tags:

java

swing

jtable

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);
                }
            }
        });
like image 602
Sebastian Zeki Avatar asked Jun 11 '16 20:06

Sebastian Zeki


People also ask

How do you make a text wrap in Jtextfield?

To wrap the lines of JTextArea we need to call the setLineWrap(boolean wrap) method and pass a true boolean value as the parameter. The setWrapStyleWord(boolean word) method wrap the lines at word boundaries when we set it to true .

How do you make a JTable cell editable?

jTableAssignments = new javax. swing. JTable() { public boolean isCellEditable(int rowIndex, int colIndex) { return editable; }};

Can I add buttons in JTable?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.


1 Answers

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());
like image 195
ck1 Avatar answered Oct 16 '22 23:10

ck1