Skip to main content

Shrink JTable row's height

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
3 replies [Last post]
marwroc
Offline
Joined: 2013-04-14

0 down vote favorite

I have a problem with my JTable object. I've put JTextArea objects inside JTable columns, to be able to wrap text. Then I've put my table inside JScrollPane. When I have long string inside a table cell and I shrink main window, the text will be automatically wraped and row will reduce it's width and increase it's heigh. The problem is that when I expand my window again row doesn't change it's height and there is lot of empty space. I paste my MainFrame code and TableCellLongTextRenderer (to put JTextArea inside JTable - author Paul Zepernick). Please help.

MainFrame

import java.awt.*;
import javax.swing.*;

public class MainFrame extends JFrame {

private String[] columnsNames = {"Message type", "Recipient", "Message"};
private JTable historyTable = null;
private JScrollPane historyTableScrollPane = null;
private final int WIDTH = 800;
private final int HEIGHT = 300;
private final int STRUT = 20;
private String[][] data = {
    {"MAIL", "Alice",
        "Snowboarding"},
    {"SMS", "John",
        "Soccer"},
    {"SMS", "Klaus",
        "study"},
    {"MAIL", "Susan",
        "buy milk"},
    {"MAIL", "David",
        "Gym"}
};

public MainFrame() {
    super("Messages sender");

    historyTable = new JTable(data, columnsNames);
    historyTable.setEnabled(true);

    historyTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellLongTextRenderer());
    historyTable.getColumnModel().getColumn(1).setCellRenderer(new TableCellLongTextRenderer());
    historyTable.getColumnModel().getColumn(2).setCellRenderer(new TableCellLongTextRenderer());

    historyTable.getColumnModel().getColumn(0).setMinWidth(100);
    historyTable.getColumnModel().getColumn(0).setMaxWidth(100);
    historyTable.getColumnModel().getColumn(1).setMinWidth(100);
    historyTable.getColumnModel().getColumn(1).setMaxWidth(100);

    historyTableScrollPane = new JScrollPane(historyTable);
    historyTableScrollPane.setPreferredSize(new Dimension(WIDTH, HEIGHT * 10));
    historyTableScrollPane.setBorder(BorderFactory.createEmptyBorder());

    setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));

    add(Box.createHorizontalStrut(STRUT));
    add(Box.createHorizontalGlue());
    add(getRightComponent());
    add(Box.createHorizontalGlue());
    add(Box.createHorizontalStrut(STRUT));

    setSize(new Dimension(800, 600));
    setLocationRelativeTo(null);
    setVisible(true);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private Component getRightComponent() {
    Box box = Box.createVerticalBox();
    box.add(Box.createVerticalStrut(STRUT));
    box.add(historyTableScrollPane);
    box.add(Box.createVerticalGlue());
    box.add(Box.createVerticalStrut(STRUT));

    return box;
}}

TableCellLongTextRenderer
import java.awt.Color;
import java.awt.Component; 
import javax.swing.JTable; 
import javax.swing.JTextArea; 
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer; 

public class TableCellLongTextRenderer extends JTextArea implements TableCellRenderer{ 

@Override 
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) { 

    this.setText((String)value); 
    this.setWrapStyleWord(true);                     
    this.setLineWrap(true);     

    //set the JTextArea to the width of the table column 
    setSize(table.getColumnModel().getColumn(column).getWidth(),getPreferredSize().height); 

    if (table.getRowHeight(row) < getPreferredSize().height) { 
        //set the height of the table row to the calculated height of the JTextArea 
        table.setRowHeight(row, getPreferredSize().height); 
    } 

    return this; 
}}

IMAGE
http://postimg.org/image/wz33lmaxh/

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
marwroc
Offline
Joined: 2013-04-14

I've clicked wrong "replay" button - sorry

zeeshanaayan07
Offline
Joined: 2013-08-11

Thanks for sharing a great coding...

marwroc
Offline
Joined: 2013-04-14

are you sarcastic?