7

I have class Probability. I want use custom renderer for it (already done) and double like editor. But I can't even find double editor (only Number), so I really have no idea how I should implement it. The question is: how I should implement it?

*difference from double editor: it should permit only numbers in range 0..100

Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132

2 Answers2

5

..numbers in range 0..100

Use a JSpinner as the TableCellEditor.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks. But unfortunately I don't think it will be useful in my case. It's not integers and users will input values manually by typing numbers. – Stan Kurilin Jun 12 '11 at 12:16
  • I'm really confused, where is your fromBlueToPurple eye, +1 for good advice without sideEffects – mKorbel Jun 12 '11 at 12:44
  • @mKorbel: "where is your fromBlueToPurple eye" I was getting sick of that image. After playing with [text clipping](http://stackoverflow.com/questions/6295084/cut-out-image-in-shape-of-text/6296381#6296381) the other day, I thought I'd try and combine some letters into a single `Shape`. A couple of `Area`s (& minutes) later, & I had the new image. (tilts head) Not sure if I like it, yet. ;) – Andrew Thompson Jun 12 '11 at 17:52
5

What about a JFormattedTextField with an AbstractFormatter doing the conversion, and a DocumentFilter to reject anything which is not a valid percentage value?

Here is an example DocumentFilter (not tested, from reading the documentation):

class PercentageFilter extends DocumentFilter {
    insertString(FilterBypass bp, int offset, String adding, AttributeSet attrs) {
        Document doc = bp.getDocument();
        String text = doc.getText(0, offset) + adding + doc.getText(offset, doc.getLength()-offset);
        try {
            double d = Double.parseDouble(text);
            if(d < 0 || 100 < d) {
                // to big or too small number
                return;
            }
        }
        catch(NumberFormatException ex) {
            // invalid number, do nothing.
            return;
        }
        // if we come to this point, the entered number
        // is a valid value => really insert it into the document.
        bp.insertString(offset, adding, attrs);
    }
}

You would want to override remove() and replace similarly.

(I suppose there could be a more efficient implementation, but this will be fast enough for most user's typing speed, I suppose.)

This filter would be returned from your AbstractFormatter implementation's getDocumentFilter method.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • +1 [`DecEditor`](http://stackoverflow.com/questions/2511270/advice-welcomed-on-creating-my-own-swing-component/2511415#2511415) is an example. – trashgod Jun 12 '11 at 13:49
  • Thanks a lot. It works exactly as I wish for editing valid values. But I didn't get idea with `DocumentFilter`. How could I permit entering big numbers in this way? – Stan Kurilin Jun 12 '11 at 14:47
  • @Stas: I added an example to the answer. – Paŭlo Ebermann Jun 12 '11 at 15:02