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]
# 1
Can somebody help please ?
moroshkoa at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 2
1) Use JFormattedTextField2) When returning the cell editor component, attach a FocusListener which selects all the text when the focus is gained
itchyscratchya at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 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.

zadoka at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
Not sure how a table made out of Spinners would lookYou'd only need to use a spinner for the editor - you could use a text field for the renderer; in fact you could probably just grab the editor component from the spinner.
itchyscratchya at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 5

> Not sure how a table made out of Spinners would

> look

>

> You'd only need to use a spinner for the editor - you

> could use a text field for the renderer; in fact you

> could probably just grab the editor component from

> the spinner.

I didn't think about that. Good idea.

zadoka at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks for a pesponse.I want text field, not JSpinner.1) Why I actually need JFormattedTextField ? Why JTextField isn't good enough ? 2) Where should I check for the validity of the entered value ?
moroshkoa at 2007-7-9 22:09:47 > top of Java-index,Desktop,Core GUI APIs...
# 7
You can use a document filter to allow only input of digits: http://java.sun.com/docs/books/tutorial/uiswing/components/generaltext.html#filter
Rodney_McKaya at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...
# 8
I read this page, but don't really understand how to write a program that uses this. Can you give a code example, or tell me where I can find one.Thanks !!
moroshkoa at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...
# 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();

}

}

}

}

Rodney_McKaya at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...
# 10

> Can you give a code example, or tell me where I can find one.

Thanks !!

The tutorial has a working example!!!

Just like I already gave you a working example the last time you posted this question.

If examples from the tutorial don't work and example posted in the forum don't work I'm not really sure how you expect us to help you.

camickra at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...
# 11
Rodney_McKay, thanks a lot !!!One thing I didn't understand: why did you overridden insertString ? I tried to comment it out and it seems to work also without it.
moroshkoa at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...
# 12
Overriding insertSting is necessary to cover all cases.This is how it should be done according to the tutorial.
Rodney_McKaya at 2007-7-9 22:09:48 > top of Java-index,Desktop,Core GUI APIs...