Hello Good day.

Here is the problem I am trying to solve.
I have a table as seen below.

232465554203149acd8363d3a4a127ae
My GOAL:
Columns A through G are going to be filled with integers. (I have already created an integer model so that these cells only take integers). As the cells in that range are filled, the Totals column will decrease in number until it reaches zero. So far I have managed to use a TableModelListener to respond to the changing cell values.

What I wish to do now is to prevent a cell, that is being edited, from accepting an integer value if it leads to the Totals Column value being a negative number. Any help with this please?

Recommended Answers

All 14 Replies

So far I have managed to use a TableModelListener to respond to the changing cell values

  • don't to use Model Listeners to change Model,
  • this is Model_To_View notifier, carrefully with that, result can be endless loop,
  • override setValueAt for computations inside XxxTableModel

What I wish to do now is to prevent a cell, that is being edited, from accepting an integer value if it leads to the Totals Column value being a negative number.

  • no issue to write that, quite basic stuff, but this isn't way how this Forums ....
  • better (for you) to post an SSCCE, short, runnable, compilable,
  • just about JFrame, with JTable contains hardcoded value for XxxTableModel

You should be able to do that by creating your own TableCellEditor - Google for lots of examples

mKorbel, I was not asking for a logical solution, I was asking for a User interface solution.

I'm not sure why I should override setValueAt. Please help me to understand why and how?

@JamesCherill : A TableCellEditor huh? Okay I'll give it a look. By the way, do you agree with mKorbel in my not using a TableModelListener?

Can I get some help using the DefaultTableCellEditor. I looked at the examples on the oracle website. But it did not help. All it did was use a JFormattedTextField and use the isEditValid method to determine whether the input is valid; the criteria being, the number must be within a range 0 to whatever.

I got no insight into overriding the specific methods. Please help me.

In essence I want to be entering data in a particular cell A. The data I enter in it will impact another cell B. I therefore want to check what the responding value in Cell B is and then prevent the cell A from accepting the value it has.

The DefaultTableEditor only allows me to respond to the cell being edited. It gives me no control over any other cells.

You respond to one cell, yes, but in that code you can still get the values of any other cells to peorform you validation.

@CoilFyzx wrote I was not asking for a logical solution, I was asking for a User interface solution.

I gave you more info that you needed, without side effect based on mistakes or wrong decisions
______________________________________________________________________________________________________

  • is there some issue to set/allow only =>0 for JFormattedTextField as editors component with number
    formatter or better with international formatter, simple to use JSpinner with SPinnerNumberModel,
    required to add DocumentFilter for [0-9] or allowed chars for both JFormattedTextField and JSpinner

  • you can to use BoundedRangeModel for JSpinners through whole JTable

  • whats issue with override start, cancel and stopCellEditing()

______________________________________________________________________________________________________

again

  • no issue to write that, quite basic stuff, but this isn't way how this Forums ....
  • better (for you) to post an SSCCE, short, runnable, compilable,
  • just about JFrame, with JTable contains hardcoded value for XxxTableModel

Sigh.
@mMorbel: there is no issue to do that. In fact that is what I have implemented, but the negative values are still being entered into the table. I can't manually enter negative values, but during a logical computation, I use the setValueAt method; this results in the negative value being entered into the table.

@JamesCherill: How?

Guys I'm simpply asking for an example please, not a coded solution, just an example.

just use getValueAt(int row, int column) (Returns the cell value at row and column.) to get the necessary values for the other cells and compute the total(s) to see if the value being edited is acceptable

@JamesCherrill not flamewar, not clear from your last sentence, (my view)miss logics there,

  • 1st. of options how is possible that value with negative sign can be accepted by editors component, then this JComponent is wrong, used wrong methods/settings

  • next of options, setValueAt (after stopCellEditing() fired, internally (API) or by override) is responsible for distribution from editors component to the XxxTableModel, JTables View

  • last of options, to create editors component (JFormattedTextField / JSpinner) that allows input value in min and max range (restricted by formatter), where final computation always returns value greater than zero

please where (as I'm understand in option 3rd.) is your getValueAt(int row, int column)

James Cherill: How do I access those methods from inside an external class that extends DefaultCellEditor? Oy....

Okay so:
I am using a TableModelListener. Inside it, I do my computations. My computations alert me to the fact that the value has changed, and is now negative. I was fine up until that point.

I therefore realized my need to ensure that the particular cell does not accept the value that it now has. So I sought to find help on this forum.

You guys told me to use a TableCellEditor so I implemented the exact TableCellEditor from the Oracle website: Click Here

This now validates numbers in a certain range for the cell being edited. I don't understand it so I can't make it do what I want. I am stuck at this point, and my code is not making any sense. I hope I'm not stressing you guys out; but I really need an example to understand so that I can create one to suit myself.

Simply pass a reference to the JTable as a parameter to the constructor of your editor class, and keep a copy of that as an instance variable. Then you can access the methods of that JTable whenever you want.

Okay. So I have found a solution for the problem. I thank you guys for all the patience and help; this truly was a hair puller. It was actually a lot simpler than I thought. Turns out I had to implement my own hard logic.

I thought that Java had some built in functions/classes that I could override to achieve my goals.

Solution:

  1. create a new DefaultCellEditor
  2. Override the stopCellEditing method and do the validation in there. (That's what worked for me)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.