Vege Thokr

Greenhorn
+ Follow
since Feb 04, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Vege Thokr

Hi,
I'm doing B&S Assignment. My JTable has a column that can take only 0-9. I'm using a customized cell editor, which is basically a formatted text field (MaskFormatter).
The problem is getValue() returns null, while getText() returns the entered value. Precisely it fails during commitEdit() with ParseException.
I saw one example in Java Tutorial where for a mask formatter they were using getText() directly instead of getValue().
Is it right doing this way or could I be missing something?

Thanks
Vege Thokr
I'm not sure what's wrong with your code. But there is an easier way for a checkbox in JTable. If you have the datatype of that column as Boolean, it will be very straightforward. Java tutorial has a simple example on this, TableDemo.java. Hope this helps.
20 years ago
Hi All,
I'm trying to use Maskformatter in a JTable's column. I went thro' Swing tutorial and coded accordingly.
When I edit values in the table, the formatter does work and restrict the values.
But when the focus is lost, the entered value in the cell disappears.
When I debugged, I found it fails during commitEdit (precisely during stringToValue conversion). Here is the code.
please provide your thoughts.

public class CustomerIDEditor extends DefaultCellEditor {
JFormattedTextField ftf;

public CustomerIDEditor() {
super(new JFormattedTextField());
ftf = (JFormattedTextField)getComponent();
MaskFormatter formatter = null;

try {
formatter = new MaskFormatter("########");
} catch (java.text.ParseException e) {
System.err.println("formatter is bad: " + e.getMessage());
System.exit(-1);
}

ftf.setFormatterFactory(new DefaultFormatterFactory(formatter));
ftf.setValue(null);
ftf.setHorizontalAlignment(JTextField.TRAILING);
ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);
} //CONSTRUCTOR ENDS

public Component getTableCellEditorComponent(JTable table,
Object value, boolean isSelected,
int row, int column) {
JFormattedTextField ftf =
(JFormattedTextField)super.getTableCellEditorComponent(
table, value, isSelected, row, column);
System.out.println("getTableCellEditorComponent called: " + value);
ftf.setValue(value);
return ftf;
}

public Object getCellEditorValue() {
JFormattedTextField ftf = (JFormattedTextField)getComponent();
try { //The text is valid,
ftf.commitEdit(); //so use it.
} catch (java.text.ParseException e) {
System.out.println("Exception:" + e.getMessage() + "Text:" + ftf.getText() ); }

System.out.println("getCellEditorValue called: " + ftf.getValue());
return ftf.getValue();
}
}
20 years ago