Liebe Leute,
Ich will in meinem JXTreeTable mit meinem (Oracle) IntegerEditor beim Editieren den gesamten Text editieren.
Leider hat zum Beispiel diese Lösung nicht funktioniert:
und diese auch nicht (bzw. vielleicht habe ich sie falsch verstanden)
java - How to select all text in JTable cell when editing - Stack Overflow
Hier ist "mein" DefaultCellEditor-Code:
Ich will in meinem JXTreeTable mit meinem (Oracle) IntegerEditor beim Editieren den gesamten Text editieren.
Leider hat zum Beispiel diese Lösung nicht funktioniert:
Java:
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column ) {
textField.setText(value.toString());
textField.selectAll();
return textField;
}
und diese auch nicht (bzw. vielleicht habe ich sie falsch verstanden)
java - How to select all text in JTable cell when editing - Stack Overflow
Hier ist "mein" DefaultCellEditor-Code:
Java:
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.EventObject;
import java.util.Locale;
import javax.swing.AbstractAction;
import javax.swing.DefaultCellEditor;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;
/**
* Implements a cell editor that uses a formatted text field
* to edit Integer values.
*/
public class IntegerCellEditor extends DefaultCellEditor {
/**
*
*/
private static final long serialVersionUID = 1L;
JFormattedTextField ftf;
NumberFormat integerFormat;
private int minimum, maximum;
private boolean DEBUG = false;
public IntegerCellEditor(int min, int max, int numberOfClicksToEdit) {
super(new JFormattedTextField());
ftf = (JFormattedTextField)getComponent();
minimum = min;
maximum = max;
/*
String sysroot = System.getenv("SystemRoot");
try {
Process proc = Runtime.getRuntime().exec(sysroot + "/system32/osk.exe");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
setClickCountToStart(numberOfClicksToEdit);
//Set up the editor for the integer cells.
integerFormat = NumberFormat.getIntegerInstance();
NumberFormatter integerFormatter = new NumberFormatter(integerFormat);
integerFormatter.setFormat(integerFormat);
integerFormatter.setMinimum(minimum);
integerFormatter.setMaximum(maximum);
ftf.setFormatterFactory(
new DefaultFormatterFactory(integerFormatter));
ftf.setValue(minimum);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
//React when the user presses Enter while the editor is
//active. (Tab is handled as specified by
//JFormattedTextField's focusLostBehavior property.)
ftf.getInputMap().put(KeyStroke.getKeyStroke(
KeyEvent.VK_ENTER, 0),
"check");
ftf.getActionMap().put("check", new AbstractAction() {
/**
*
*/
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
if (!ftf.isEditValid()) { //The text is invalid.
if (userSaysRevert()) { //reverted
ftf.postActionEvent(); //inform the editor
}
} else try {
ftf.selectAll();//The text is valid,
ftf.commitEdit(); //so use it.
ftf.postActionEvent(); //stop editing
} catch (java.text.ParseException exc) { }
}
});
// new VirtualNumberPad(ftf);
}
//Override to invoke setValue on the formatted text field.
public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int column) { /*
JFormattedTextField ftf =
(JFormattedTextField)super.getTableCellEditorComponent(
table, value, isSelected, row, column);
ftf.setValue(value);
ftf.selectAll();
System.out.print("lol");
return ftf;*/
super.getTableCellEditorComponent(table, value, true, row, column); // hier wird der Text gesetzt
ftf.requestFocus();
ftf.selectAll(); // selektieren
return ftf; // Textfeld zurückgeben.
}
//Override to ensure that the value remains an Integer.
public Object getCellEditorValue() {
final JFormattedTextField ftf = (JFormattedTextField)getComponent();
Object o = ftf.getValue();
if (o instanceof Integer) {
return o;
} else if (o instanceof Number) {
return new Integer(((Number)o).intValue());
} else {
if (DEBUG) {
System.out.println("getCellEditorValue: o isn't a Number");
}
try {
return integerFormat.parseObject(o.toString());
} catch (ParseException exc) {
System.err.println("getCellEditorValue: can't parse o: " + o);
return null;
}
}
}
//Override to check whether the edit is valid,
//setting the value if it is and complaining if
//it isn't. If it's OK for the editor to go
//away, we need to invoke the superclass's version
//of this method so that everything gets cleaned up.
public boolean stopCellEditing() {
JFormattedTextField ftf = (JFormattedTextField)getComponent();
if (ftf.isEditValid()) {
try {
ftf.commitEdit();
} catch (java.text.ParseException exc) { }
} else { //text is invalid
if (!userSaysRevert()) { //user wants to edit
return false; //don't let the editor go away
}
}
return super.stopCellEditing();
}
/**
* Lets the user know that the text they entered is
* bad. Returns true if the user elects to revert to
* the last good value. Otherwise, returns false,
* indicating that the user wants to continue editing.
*/
protected boolean userSaysRevert() {
Toolkit.getDefaultToolkit().beep();
ftf.selectAll();
Object[] options = {"Bearbeiten",
"Zurücksetzen"};
int answer = JOptionPane.showOptionDialog(
SwingUtilities.getWindowAncestor(ftf),
"Der Wert muss eine Zahl sein zwischen "
+ minimum + " und "
+ maximum + ".\n"
+ "Sie können den Wert entweder bearbeiten"
+ " oder zum letzten Wert zurücksetzen.",
"Invalid Text Entered",
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
options,
options[1]);
if (answer == 1) { //Revert!
ftf.setValue(ftf.getValue());
return true;
}
return false;
}
@Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
}