Results 1 to 2 of 2
  1. #1
    marwroc is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Question Shrink JTable row's height

    Hi, 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

    Java Code:
    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

    Java Code:
    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
    Shrink JTable row's height-table.jpg

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

Similar Threads

  1. should I consult a shrink
    By willemjav in forum New To Java
    Replies: 5
    Last Post: 02-11-2013, 01:22 PM
  2. Expand & Shrink Buttons
    By umaza in forum AWT / Swing
    Replies: 1
    Last Post: 10-31-2012, 05:22 AM
  3. Replies: 4
    Last Post: 07-03-2012, 03:40 PM
  4. Replies: 9
    Last Post: 09-11-2011, 06:27 PM
  5. shrink and expand nemu when user click
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-06-2009, 08:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •