0

I need a method that allows only certain characters are entered in a jTable.

My jframe is below:

enter image description here

In the columnd "Precedentes" i need that the column accepts only numbers and ",".

A input example that i need:

enter image description here

My validation method of this column is below:

private void jTable1KeyPressed(java.awt.event.KeyEvent evt) {                
            i = 0; 
            String carac="0987654321,";
            if(!carac.contains(tableModel.getValueAt(i, 3)+"")){
            tableModel.consume();// TODO add your handling code here:
            }
}  

i => It's my line number (it's used in a loop to verify each line). So I go line line by line getting what the user typed in the column "precedentes" and validate with that method.

When i type something that is not a number or "," the columns "Precedente" should not accept.

Error message when i execute my method:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I think what i need is a mask that accepts only number and ",", but i don't know how to do this to a column of a jTable, i know how to to this with a text field, but the method that i use to validate that in a text field doesn't work in a jtable.

Mutante
  • 278
  • 5
  • 18

1 Answers1

1

As shown in How to Use Tables: Using an Editor to Validate User-Entered Text, you can override the DefaultCellEditor method stopCellEditing() to check whether the text is valid. The CellEditor seen here is an example for model values of type Integer. For values of type Date, you can use JFormattedTextField in your CellEditor, as shown here, and you can validate dates using SimpleDateFormat, as shown here. To validate individual characters, construct your JFormattedTextField with a MaskFormatter, as shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I want that when the user type something that not be number or "," in a columng of the jtable, the programa doesn't accept (don't allow to write in the table of jframe) , I can do that integrating this Two methods above ? – Mutante Jun 26 '16 at 04:51
  • You might look at [`MaskFormatter`](https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#maskformatter), a `DocumentListener` to examine individual keystrokes or a `DocumentFilter` to enforce numeric entry, cited [here](http://stackoverflow.com/a/37878422/230513). – trashgod Jun 26 '16 at 04:57
  • I've updated the original post with what I'm trying to do. – Mutante Jun 26 '16 at 05:01
  • 1
    I'd start with a `MaskFormatter` for your `JFormattedTextField` in your `CellEditor`; please edit your question to include a [mcve] that shows your chosen approach. – trashgod Jun 26 '16 at 05:12
  • I believe now it'a easier to understand my doubt. I've updated to the MCV. – Mutante Jun 26 '16 at 18:44
  • What do you need more? I believe with that you can understood my problem, like i said before, i need that the column "precedentes" accept only Numbers and ",", i am using the method above to validate that, but when i type something like 20,30,a,50 in the column "precedentes" i need that my method of validation do not accept nothinng more than Numbers and ",", In the example above the letter "a" should not appear in the column when i type it. Sorry about my doubt, i started programming java 2 weeks ago. – Mutante Jun 26 '16 at 20:09
  • Without your [mcve], I don't know where you're stuck. If you're in a hurry, try [tag:jcalendar], seen [here](http://stackoverflow.com/a/14880675/230513). – trashgod Jun 27 '16 at 02:41
  • I added a photo and improved my explanation of my problem. Now I believe that you will understand my question and understand my validation method that is not working . – Mutante Jun 27 '16 at 13:46
  • 1
    I see the kind of problem you're trying to solve, but I don't see where your `MaskFormatter` fails. – trashgod Jun 27 '16 at 14:06
  • I edited the post again, i put my entire method of validation, and the error that appears when i execute. In addition to the error happens the method does not work because when I press a button other than number or "," the column still accepts, the correct thing is when I press a button that is not number or ", " the column doesn't show . – Mutante Jun 27 '16 at 14:15