Java - AWT/Swing

TableCellEditor mit JTextArea

javax.swing.table.TableCellEditor
javax.swing.JTextArea

Folgendes Beispiel zeigt, wie eine TextArea als CellEditor verwendet werden kann:
TextArea als TableCellEditor


import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import java.awt.Component;
import java.util.EventObject;
 
public class TextAreaRendererDemo extends JFrame {
 
   public TextAreaRendererDemo() {
      JTable table = new JTable(new Object[][] {
            {"Zeile 1 von 1", "Zeile 1 von 10\nZeile 2 von 10\nZeile 3 von 10\nZeile 4 von 10\nZeile 5 von 10\nZeile 6 von 10\nZeile 7 von 10\nZeile 8 von 10\nZeile 9 von 10\nZeile 10 von 10", "Zeile 1 von 1"},
            {"Zeile 1 von 2\nZeile 2 von 2", "Zeile 1 von 3\nZeile 2 von 3\nZeile 3 von 3", "Zeile 1 von 1"},
            {"Zeile 1 von 1", "Zeile 1 von 10\nZeile 2 von 10\nZeile 3 von 10\nZeile 4 von 10\nZeile 5 von 10\nZeile 6 von 10\nZeile 7 von 10\nZeile 8 von 10\nZeile 9 von 10\nZeile 10 von 10", "Zeile 1 von 1"},
            {"Zeile 1 von 2\nZeile 2 von 2", "Zeile 1 von 2\nZeile 2 von 2", "Zeile 1 von 1"}
           },
           new String[] {"Spalte 1", "Spalte 2", "Spalte 3"});
      getContentPane().add(new JScrollPane(table));
 
      table.setDefaultRenderer(Object.class, new TextAreaCellRenderer());
      table.setDefaultEditor(Object.class, new TextAreaCellEditor());
      for (int r=0; r<table.getRowCount(); r++) {
         TableCellRenderer renderer = table.getCellRenderer(r, 1);
         Component comp = table.prepareRenderer(renderer, r, 1);
         int height = comp.getPreferredSize().height + 2;
         table.setRowHeight(r, height < 60 ? height : 60);
      }
   }
 
   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            final JFrame frame = new TextAreaRendererDemo();
            frame.setBounds(0, 0, 400, 300);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
         }
      });
   }
}
 
class TextAreaCellRenderer extends DefaultTableCellRenderer {
   private JScrollPane scrollPane;
   private JTextArea textArea;
 
   public TextAreaCellRenderer() {
      textArea = new JTextArea();
      textArea.setLineWrap(true);
      textArea.setWrapStyleWord(true);
      scrollPane = new JScrollPane(textArea);
      scrollPane.setBorder(BorderFactory.createEmptyBorder());
   }
 
   public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
      textArea.setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
      textArea.setText(value.toString());
      textArea.setForeground(c.getForeground());
      textArea.setBackground(c.getBackground());
      return scrollPane;
   }
}
 
 
class TextAreaCellEditor implements TableCellEditor {
    protected EventListenerList listenerList = new EventListenerList();
    private final JScrollPane scrollPane;
    private JTextArea textArea = new JTextArea();
 
    public TextAreaCellEditor() {
        scrollPane = new JScrollPane(textArea);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        textArea.setLineWrap(true);
        textArea.setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
    }
 
    @Override
    public Object getCellEditorValue() {
        return textArea.getText();
    }
 
    @Override
    public Component getTableCellEditorComponent(
        JTable table, Object value, boolean isSelected, int row, int column) {
        textArea.setFont(table.getFont());
        textArea.setText((value!=null)?value.toString():"");
        return scrollPane;
    }
 
    @Override
    public boolean isCellEditable(final EventObject e) {
        return true;
    }
 
    @Override
    public boolean shouldSelectCell(EventObject e) {
        return true;
    }
 
    @Override
    public boolean stopCellEditing() {
        fireEditingStopped();
        return true;
    }
 
    @Override
    public void cancelCellEditing() {
        fireEditingCanceled();
    }
 
    @Override
    public void addCellEditorListener(CellEditorListener l) {
        listenerList.add(CellEditorListener.class, l);
    }
 
    @Override
    public void removeCellEditorListener(CellEditorListener l) {
        listenerList.remove(CellEditorListener.class, l);
    }
 
    protected void fireEditingStopped() {
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0 ; i < listeners.length; i++) {
            if (listeners[i] instanceof CellEditorListener) {
                ((CellEditorListener)listeners[i]).editingStopped(new ChangeEvent(this));
            }
        }
    }
 
    protected void fireEditingCanceled() {
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0 ; i < listeners.length; i++) {
            if (listeners[i] instanceof CellEditorListener) {
                ((CellEditorListener)listeners[i]).editingCanceled(new ChangeEvent(this));
            }
        }
    }
}


Stichworte:

CellEditor, JTextArea, MulitLineCellEditor, JTable

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007