Wednesday, July 27, 2016

Solved - JTable: wrap text in a cell, column, or row

To wrap text in a JTable, you need to create a TableCellRenderer and TableCellEditor that use the JTextArea as the component. Then, use them for the cell, column, row, or the whole table where you want to wrap text. Following is a sample code.


public class TableWrapText extends JFrame {
    private DefaultTableModel tableModel = null;
 
    public TableWrapText() {
        String[][] data = new String[][]{
            new String[]{"Travel", "Go to somewhere else from where you live to enjoy spending money and getting lost in the esoterics."},
            new String[]{"Help", "Do something to make the person get what he or she wants for pay or for free, it is upto you."},
            new String[]{"Sleep", "A state of temperary unconscious due to the nerves system is busy with something else."}
        };
        tableModel = new DefaultTableModel(data, new String[]{"Name", "Content"});

        JTable table = new JTable(tableModel){
            public TableCellRenderer getCellRenderer(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyRenderer();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyRenderer();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyRenderer();
//              }    
                } else {
                    return new DefaultTableCellRenderer();
                }
            }

            public TableCellEditor getCellEditor(int row, int col){
                //Wrap text in a specific cell
                if (row == 0 && col == 1){
                    return new MyEditor();
                //Wrap text in a particular column  
          //    if (col == 1) {
//                    return new MyEditor();
//              }
                //Wrap text in a row
//              if (row == 0) {
//                    return new MyEditor();
//              }    
                } else {
                    return new DefaultCellEditor(new JTextField());
                }
            }
        };
        table.setRowHeight(0, table.getFont().getSize()*3);
//        table.setRowHeight(table.getFont().getSize()*3);
        JScrollPane sp = new JScrollPane(table);
        getContentPane().add(sp);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(600, 400);
        this.setVisible(true);
    }
 
    private class MyRenderer extends JTextArea implements TableCellRenderer{
        public MyRenderer() {
            setOpaque(true);
            setLineWrap(true);
            setWrapStyleWord(true);
        }
     
        public Component getTableCellRendererComponent(JTable table,Object value,
            boolean isSelected, boolean hasFocus, int row,int column) {
         
            this.setText(value == null ? "" : value.toString());
            return this;
        }

    }

    private class MyEditor extends AbstractCellEditor implements TableCellEditor {
        JTextArea comp = new JTextArea();
        JTable table;
        int row;
     
        public MyEditor() {
            comp.setLineWrap(true);
            comp.setWrapStyleWord(true);
            comp.addComponentListener(new ComponentAdapter() {
                public void componentResized(ComponentEvent e) {
                    super.componentResized(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
            comp.addKeyListener(new KeyAdapter() {
                public void keyTyped(KeyEvent e) {
                    super.keyTyped(e);
                    table.setRowHeight(row, (int) (comp.getPreferredSize().getHeight()));
                }
            });
        }

        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected, int row, int column) {
            this.table = table;
            this.row = row;

            comp.setText((String) value);
            comp.setFont(table.getFont());

            return comp;
        }

        public Object getCellEditorValue() {
            return comp.getText();
        }
    }
 
    public static void main(String[] args){
        new TableWrapText();
    }
}

-----------------------------------------------------------------------------------------------------------------
Watch the blessing and loving online channel: SupremeMasterTV live




If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book for free here.

1 comment:

  1. why does it only wrap a single cell ... the rest of the table is still the same.
    also I have to click the cell to make it completely wrapped.

    ReplyDelete