javaswingjtablejtextareatablecellrenderer

Select text of a JTextArea that is inside a JPanel rendered by a TableCellRenderer


I have a JTable where all row is rendered by a custom TableCellRenderer. In this last the method getTableCellRendererComponent returns a panel that has inside some things including a JTextArea. How can i add the possibility to user to select the text (with mouse, like a normal JTextArea) of the JTextArea that is inside this panel?


Solution

  • Cell renderers are not interactive (means user cannot edit or select contents). So, you need to add a cell editor in addition to the cell renderer.

    Try this example code. It will give the table shown in below screenshot. (Yellow cell is the editor. Other grey cell is the renderer.) User can select text in editor and copy it (Ctrl+C).

    import javax.swing.*;
    import javax.swing.event.CellEditorListener;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    import java.awt.*;
    import java.util.EventObject;
    
    public class TextAreaRenderer
    {
      public static void main(String[] args)
      {
        JTable table = new JTable(new String[][] {{"ABC"}, {"PQR"}}, new String[] {"Column Name"});
        table.getColumnModel().getColumn(0).setCellRenderer(new CustomRenderer());
        table.getColumnModel().getColumn(0).setCellEditor(new CustomEditor());
        table.setRowHeight(30);
    
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new JScrollPane(table));
        f.pack();
        f.setVisible(true);
      }
    }
    
    class CustomRenderer implements TableCellRenderer
    {
      private JPanel panel = new JPanel();
      private JTextArea textArea = new JTextArea();
    
      CustomRenderer()
      {
        panel.add(new JLabel("Label"));
        panel.add(textArea);
      }
    
      @Override
      public java.awt.Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                              boolean hasFocus, int row, int column)
      {
        textArea.setText((String) value);
        return panel;
      }
    }
    
    class CustomEditor implements TableCellEditor
    {
      private JPanel panel = new JPanel();
      private JTextArea textArea = new JTextArea();
    
      CustomEditor()
      {
        panel.add(new JLabel("Label"));
        panel.add(textArea);
        panel.setBackground(Color.yellow);
      }
    
      @Override
      public java.awt.Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
                                                            int row, int column)
      {
        textArea.setText((String) value);
        return panel;
      }
    
      @Override
      public Object getCellEditorValue()
      {
        return textArea.getText();
      }
    
      @Override
      public boolean isCellEditable(EventObject anEvent) { return true; }
    
      @Override
      public boolean shouldSelectCell(EventObject anEvent) { return true; }
    
      @Override
      public boolean stopCellEditing() { return true; }
    
      @Override
      public void cancelCellEditing() {}
    
      @Override
      public void addCellEditorListener(CellEditorListener l) {}
    
      @Override
      public void removeCellEditorListener(CellEditorListener l) {}
    }
    

    screenshot