Не. Нужно что-то типа такого:
Код | package com.agilemind.commons.localization.views;
import javax.swing.AbstractCellEditor; import javax.swing.JCheckBox; import javax.swing.JComponent; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentListener; import javax.swing.event.TableModelListener; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.tree.TreeCellEditor; import java.awt.Component; import java.awt.Dimension; import java.awt.FontMetrics; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.io.Serializable; import java.util.EventObject;
public class Test extends JFrame { JTable table = new JTable(10, 5);
public Test() { for (int i = 0; i < table.getColumnCount(); i++) { TableColumn tableColumn = table.getColumnModel().getColumn(i); tableColumn.setCellEditor(new MyCellEditor(new JEditorPane(), table, tableColumn)); } add(new JScrollPane(table)); }
public static void main(String[] args) { JFrame f = new Test(); f.setBounds(100, 100, 400, 300); f.setDefaultCloseOperation(3); f.setVisible(true); }
static class MyCellEditor extends AbstractCellEditor implements TableCellEditor, TreeCellEditor {
// // Instance Variables //
/** * The Swing component being edited. */ protected JComponent editorComponent; /** * The delegate class which handles all methods sent from the * <code>CellEditor</code>. */ protected EditorDelegate delegate; /** * An integer specifying the number of clicks needed to start editing. * Even if <code>clickCountToStart</code> is defined as zero, it * will not initiate until a click occurs. */ protected int clickCountToStart = 1;
// // Constructors //
/** * Constructs a <code>DefaultCellEditor</code> that uses a text field. * * @param editorPane a <code>JTextField</code> object */ public MyCellEditor(final JEditorPane editorPane, final JTable table, final TableColumn tableColumn/*, final Object columnIdentifier*/) { editorComponent = editorPane; this.clickCountToStart = 2; delegate = new EditorDelegate() { public void setValue(Object value) { editorPane.setText((value != null) ? value.toString() : ""); }
public Object getCellEditorValue() { return editorPane.getText(); } }; editorPane.addKeyListener(delegate);
//TableColumn tableColumn = table.getColumn(columnIdentifier); Dimension dimension = new Dimension(tableColumn.getPreferredWidth(), 500); editorPane.setPreferredSize(dimension); editorPane.setSize(dimension);
editorPane.getDocument().addDocumentListener(new DocumentListener() { public void insertUpdate(DocumentEvent e) { updateTableRowHeight(); }
public void removeUpdate(DocumentEvent e) { updateTableRowHeight(); }
public void changedUpdate(DocumentEvent e) { updateTableRowHeight(); }
private void updateTableRowHeight() {
FontMetrics fontMetrics = editorPane.getFontMetrics(editorPane.getFont()); double textWidth = fontMetrics.stringWidth(editorPane.getText()) + fontMetrics.stringWidth("w"); double textHeight = fontMetrics.getHeight(); //double componentWidth = table.getColumn(columnIdentifier).getWidth(); double componentWidth = tableColumn.getWidth();
double componentHeight = (textWidth * textHeight) / componentWidth; int n = (int) (componentHeight / textHeight) + 1;
if (componentHeight != 0) { table.setRowHeight(table.getSelectedRow(), (int) (n * textHeight)); } else { //todo... } } }); }
/** * Returns a reference to the editor component. * * @return the editor <code>Component</code> */ public Component getComponent() { return editorComponent; }
// // Modifying //
/** * Specifies the number of clicks needed to start editing. * * @param count an int specifying the number of clicks needed to start editing * @see #getClickCountToStart */ public void setClickCountToStart(int count) { clickCountToStart = count; }
/** * Returns the number of clicks needed to start editing. * * @return the number of clicks needed to start editing */ public int getClickCountToStart() { return clickCountToStart; }
// // Override the implementations of the superclass, forwarding all methods // from the CellEditor interface to our delegate. //
/** * Forwards the message from the <code>CellEditor</code> to * the <code>delegate</code>. * * @see EditorDelegate#getCellEditorValue */ public Object getCellEditorValue() { return delegate.getCellEditorValue(); }
/** * Forwards the message from the <code>CellEditor</code> to * the <code>delegate</code>. * * @see EditorDelegate#isCellEditable(java.util.EventObject) */ public boolean isCellEditable(EventObject anEvent) { return delegate.isCellEditable(anEvent); }
/** * Forwards the message from the <code>CellEditor</code> to * the <code>delegate</code>. * * @see EditorDelegate#shouldSelectCell(EventObject) */ public boolean shouldSelectCell(EventObject anEvent) { return delegate.shouldSelectCell(anEvent); }
/** * Forwards the message from the <code>CellEditor</code> to * the <code>delegate</code>. * * @see EditorDelegate#stopCellEditing */ public boolean stopCellEditing() { return delegate.stopCellEditing(); }
/** * Forwards the message from the <code>CellEditor</code> to * the <code>delegate</code>. * * @see EditorDelegate#cancelCellEditing */ public void cancelCellEditing() { delegate.cancelCellEditing(); }
// // Implementing the TreeCellEditor Interface //
/** * Implements the <code>TreeCellEditor</code> interface. */ public Component getTreeCellEditorComponent(JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row) { String stringValue = tree.convertValueToText(value, isSelected, expanded, leaf, row, false);
delegate.setValue(stringValue); return editorComponent; }
//
// Implementing the CellEditor Interface
//
/** * Implements the <code>TableCellEditor</code> interface. */ public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { delegate.setValue(value); if (editorComponent instanceof JCheckBox) { //in order to avoid a "flashing" effect when clicking a checkbox //in a table, it is important for the editor to have as a border //the same border that the renderer has, and have as the background //the same color as the renderer has. This is primarily only //needed for JCheckBox since this editor doesn't fill all the //visual space of the table cell, unlike a text field. TableCellRenderer renderer = table.getCellRenderer(row, column); Component c = renderer.getTableCellRendererComponent(table, value, isSelected, true, row, column); if (c != null) { editorComponent.setOpaque(true); editorComponent.setBackground(c.getBackground()); if (c instanceof JComponent) { editorComponent.setBorder(((JComponent) c).getBorder()); } } else { editorComponent.setOpaque(false); } } return editorComponent; }
// // Protected EditorDelegate class //
/** * The protected <code>EditorDelegate</code> class. */ protected class EditorDelegate extends KeyAdapter implements Serializable {
/** * The value of this cell. */ protected Object value;
/** * Returns the value of this cell. * * @return the value of this cell */ public Object getCellEditorValue() { return value; }
/** * Sets the value of this cell. * * @param value the new value of this cell */ public void setValue(Object value) { this.value = value; }
/** * Returns true if <code>anEvent</code> is <b>not</b> a * <code>MouseEvent</code>. Otherwise, it returns true * if the necessary number of clicks have occurred, and * returns false otherwise. * * @param anEvent the event * @return true if cell is ready for editing, false otherwise * @see #shouldSelectCell */ public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart; } return true; }
/** * Returns true to indicate that the editing cell may * be selected. * * @param anEvent the event * @return true * @see #isCellEditable */ public boolean shouldSelectCell(EventObject anEvent) { return true; }
/** * Returns true to indicate that editing has begun. * * @param anEvent the event */ public boolean startCellEditing(EventObject anEvent) { return true; }
/** * Stops editing and * returns true to indicate that editing has stopped. * This method calls <code>fireEditingStopped</code>. * * @return true */ public boolean stopCellEditing() { fireEditingStopped(); return true; }
/** * Cancels editing. This method calls <code>fireEditingCanceled</code>. */ public void cancelCellEditing() { fireEditingCanceled(); }
@Override public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { MyCellEditor.this.stopCellEditing(); } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { cancelCellEditing(); } } }
} // End of class JCellEditor }
|
Просто такая реализация не катит, то что она глючит когда ввести много строк, надо как-то по правильному рассчитать размеры текста, а я блин не математик :( . Сори за непонятки |