Need help with cell editor
Hello !
I want to write a cell editor with the following pretty simple requirements:
1) The valid values are only integers in some range [A,B].
2) Each time before I start edit the cell (either after a double click on the cell or just start typing), all the content in the cell selected, so the "old" data is overwritten (not appended) with the new one.
I have seen many examples of code that does something like this, but not exactly what I want. I want to write the most simple editor I can. Can some one please guide me how to perform this task ? Should I use JFormattedTextField or JTextField is good enough ? Should I use NumberFormatter ? Which of the DefaultCellEditor's methods should I overwrite ?
I'm really confused... PLEASE HELP !
Thanks in advance !
[808 byte] By [
moroshkoa] at [2007-11-26 19:35:15]

# 3
> Hello !
>
> I want to write a cell editor with the following
> pretty simple requirements:
> 1) The valid values are only integers in some range
> [A,B].
Sounds like a JSpinner. Not sure how a table made out of Spinners would look but it might be what you want and should be the most intuitive to the user as far as acceptable values.
> 2) Each time before I start edit the cell (either
> after a double click on the cell or just start
> typing), all the content in the cell selected, so the
> "old" data is overwritten (not appended) with the new
> one.
This might not be an issue if you a JSpinner, not sure though, you might be able to add a listener and then select the contents? Try it out.
# 9
Here's a simple SSCCE example:
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
public class DocumentDigitFilterTest {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextField field = new JTextField();
((AbstractDocument) field.getDocument()).setDocumentFilter(new DocumentDigitFilter());
field.setPreferredSize(new Dimension(100, 20));
frame.getContentPane().add(field);
frame.pack();
frame.setVisible(true);
} catch (Exception e) {}
}
private static class DocumentDigitFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {
try {
Integer.parseInt(str);
super.insertString(fb, offs, str, a);
} catch (NumberFormatException e) {
Toolkit.getDefaultToolkit().beep();
}
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {
try {
Integer.parseInt(str);
super.replace(fb, offs, length, str, a);
} catch (NumberFormatException e) {
Toolkit.getDefaultToolkit().beep();
}
}
}
}