1

There's a custom class extending JPanel, named CustomJPanel, that has a JTable in it. I've added an instance of CustomJPanel to the JDialog derived class, which has an OK and cancel buttons.

Once the user presses OK, I'd like to access the value at specific cell from my custom JDialog class. This is because the user should know if there's a cell with an incorrect value set by the user, and if there is, it should know which one exactly. The problem is the JDialog class has the reference to CustomJPanel, not the JTable. I have two options:

  1. add a method to CustomJPanel that returns the JTable (it breaks encapsulation)

  2. add a method to CustomJPanel that returns the value of a specific cell from JTable

Which one is better? These are the two I was able to come up with, maybe you could suggest something better.

user5539357
  • 1,024
  • 1
  • 9
  • 19
  • I think you've answers your own question, CustomJPanel should be able to validate the values in the table and tell any interested party if it's not valid and possible which cells and/or, when requested, highlight the invalid cells – MadProgrammer May 21 '16 at 09:54
  • Also consider validating in place, for [example](http://stackoverflow.com/a/7539298/230513). – trashgod May 21 '16 at 10:36
  • 1
    Simply make a method `boolean isValid()` in your `CustomPanel`which checks whether the values in table are correct. And call this method when user presses OK in your dialog. – Sergiy Medvynskyy May 21 '16 at 12:06

0 Answers0