सरल समाधान में से एक प्रस्तावित
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
केवल स्ट्रिंग स्तंभों के लिए अच्छा है। समस्या यह है कि, उदाहरण के लिए, कॉलम के फ़्लोट प्रकार को संपादित किया जा रहा है, संबंधित सेल में एक खाली स्ट्रिंग दर्ज करें और फिर विंडो के किसी भी अन्य नियंत्रण पर क्लिक करें - CellEditorRemover.propertyChange()
JTable.java
की विधि NullPointerException
पर फेंकता है। यह संपादन को रोकने या रद्द करने के लिए getCellEditor()
कॉल का उपयोग करता है लेकिन यह इस मामले में null
देता है। यदि दर्ज किया गया मान खाली नहीं है या यदि मैं terminateEditOnFocusLost
ध्वज हटाता हूं तो सबकुछ ठीक है। शायद, वर्णित स्थिति एक बग है।
मुझे आशा है कि मैं पिछले पोस्टों में से एक के आधार पर समाधान प्रदान कर सकता हूं। यह इतना छोटा नहीं है जैसा कि मैंने पहले माना था लेकिन मुझे लगता है कि यह काम करता है। मुझे डिफ़ॉल्ट सेल संपादक और JTextField
से अपने स्वयं के टेक्स्ट फ़ील्ड से अपना स्वयं का सेल संपादक प्राप्त करना था जिसमें FocusListener
है। जब संपादन कक्ष फोकस खो देता है, और खिड़की के दूसरे नियंत्रण से प्राप्त फोकस खो देता है तो यह फोकस श्रोता ठीक काम करता है। लेकिन सेल चयन के मामले में फोकस श्रोता "बहरा" बदलता है। यही कारण है कि अगर प्रवेश मूल्य अमान्य हो जाएगा तो इसे पुनर्स्थापित करने के लिए संपादन को संपादित करने से पहले मुझे पहले वैध मान भी याद रखना होगा।
नीचे दिए गए कोड को देखें। Double
, Float
और Integer
के साथ परीक्षण किया गया, लेकिन मुझे उम्मीद है कि यह Byte
और String
के साथ भी काम करेगा। फोकस श्रोता साथ
पाठ क्षेत्र:
public class TextFieldCell extends JTextField {
public TextFieldCell(JTable cellTable) {
super(); // calling parent constructor
final JTable table = cellTable; // this one is required to get cell editor and stop editing
this.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
}
// this function successfully provides cell editing stop
// on cell losts focus (but another cell doesn't gain focus)
public void focusLost(FocusEvent e) {
CellEditor cellEditor = table.getCellEditor();
if (cellEditor != null)
if (cellEditor.getCellEditorValue() != null)
cellEditor.stopCellEditing();
else
cellEditor.cancelCellEditing();
}
});
}
}
डिफ़ॉल्ट सेल संपादक वर्ग:
myTable.setDefaultEditor(Float.class, new TextFieldCellEditor(new TextFieldCell(myTable), Float.class));
myTable.setDefaultEditor(Double.class, new TextFieldCellEditor(new TextFieldCell(myTable), Double.class));
myTable.setDefaultEditor(Integer.class, new TextFieldCellEditor(new TextFieldCell(myTable), Integer.class));
आशा:
class TextFieldCellEditor extends DefaultCellEditor {
TextFieldCell textField; // an instance of edit field
Class<?> columnClass; // specifies cell type class
Object valueObject; // for storing correct value before editing
public TextFieldCellEditor(TextFieldCell tf, Class<?> cc) {
super(tf);
textField = tf;
columnClass = cc;
valueObject = null;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
TextFieldCell tf = (TextFieldCell)super.getTableCellEditorComponent(table, value, isSelected, row, column);
if (value != null) {
tf.setText(value.toString());
}
// we have to save current value to restore it on another cell selection
// if edited value couldn't be parsed to this cell's type
valueObject = value;
return tf;
}
@Override
public Object getCellEditorValue() {
try {
// converting edited value to specified cell's type
if (columnClass.equals(Double.class))
return Double.parseDouble(textField.getText());
else if (columnClass.equals(Float.class))
return Float.parseFloat(textField.getText());
else if (columnClass.equals(Integer.class))
return Integer.parseInt(textField.getText());
else if (columnClass.equals(Byte.class))
return Byte.parseByte(textField.getText());
else if (columnClass.equals(String.class))
return textField.getText();
}
catch (NumberFormatException ex) {
}
// this handles restoring cell's value on jumping to another cell
if (valueObject != null) {
if (valueObject instanceof Double)
return ((Double)valueObject).doubleValue();
else if (valueObject instanceof Float)
return ((Float)valueObject).floatValue();
else if (valueObject instanceof Integer)
return ((Integer)valueObject).intValue();
else if (valueObject instanceof Byte)
return ((Byte)valueObject).byteValue();
else if (valueObject instanceof String)
return (String)valueObject;
}
return null;
}
यह आप तालिका प्रारंभ की कोड निम्नलिखित जोड़ने के लिए , यह किसी ऐसे व्यक्ति की मदद करेगा जिसकी एक ही समस्या है।
दूसरा लिंक काम नहीं कर रहा है। :( – Niroshan
तात्कालिक मीडिया पर संग्रहीत सामूहिक ज्ञान के संदर्भ में मानवता के लिए सीखने के लिए एक सबक। – alphazero
द वेबैक मशीन में यह था - मैंने तदनुसार लिंक बदल दिया। –