-1

This might sound weird but this is what happening. I Have a JTable which is part of JPanel When I add JPanel(pnlMain) I can get values of checked checkBoxes(Ref Line#437)

On otherhand when I add pnlMain in JFrame and click X button on top it returns Checkbox value as false(Ref Line#424-433)

I am not able to figure out why putting pnlMain in JFrame not letting fetch checkbox value.

The code that is fetching value is on Line# 439

Code in Question given here

Update: Previous Question I asked is given here:

Checkbox in JTable always return false despite of it's checked

Community
  • 1
  • 1
Volatil3
  • 14,253
  • 38
  • 134
  • 263
  • 1
    Please share only relevant and testable code. – Braj Jul 19 '14 at 23:42
  • Agrees with @Braj. Code posted in pastebin is of no use for future visitors to this site. Please post your relevant code **here**. – Hovercraft Full Of Eels Jul 19 '14 at 23:43
  • HUh? When I did this in previous question I was asked to put `runnable` code. What's wrong in that? – Volatil3 Jul 19 '14 at 23:43
  • Yes the code should able to reproduce the issue. – Braj Jul 19 '14 at 23:44
  • 2
    Yes, runnable code **here**. Questions here are not just for your benefit but also for the benefit of future users. Links go bad. If you have too much code to post here, then you have too much code to ask a volunteer to go through. For better help, consider creating and posting a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Jul 19 '14 at 23:44
  • Please read the link in my comment above. And yeah, you posted way too much code to ask a volunteer to go through, and it's not runnable by us to boot. Please try to isolate your problem as much as possible in small runnable code. – Hovercraft Full Of Eels Jul 19 '14 at 23:46
  • @HovercraftFullOfEels I did something similar http://stackoverflow.com/questions/24844884/checkbox-in-jtable-always-return-false-despite-of-its-checked/24844930 but got fruitless. Unable to isolate this particular issue as a separate program – Volatil3 Jul 19 '14 at 23:48
  • Volatil, that's likely the key for you or us to solve this, your isolating the problem. It will take work on your part, but will be well worth it. Your method that you posted is huge, which suggests that it's in a very large class, which would explain why its hard for you to isolate your problem. You would do well to try to refactor your code into manageable units with classes that obey the "single responsibility principle" of object oriented programming. This will allow you to create small testable classes. – Hovercraft Full Of Eels Jul 19 '14 at 23:51
  • For instance, your pnlFilter could be in its own class, same for pnlMain, so could the table model,... – Hovercraft Full Of Eels Jul 19 '14 at 23:56
  • @HovercraftFullOfEels Agree on your points. My only issue is, why same thing does notwork when `pnlMain` is added in `JFrame` but works when `pnlMain` is added in `JOptionPane`. What' wrong being done with `JFrame` when I cloe it by clicking X button – Volatil3 Jul 19 '14 at 23:58
  • @Volatil3: I wish I could answer this. For me to figure out why the code is not behaving as expected, I'd need runnable code that I could understand, test, and modify. Do you see the predicament that we're in? – Hovercraft Full Of Eels Jul 20 '14 at 00:01

1 Answers1

2

Here's a possible answer:

  • When you put the JPanel in a JOptionPane and display it, you are displaying it in a modal dialog box. This means that program flow stops right where you display the JOptionPane, and then does not resume until the JOptionPane has been dealt with. When that happens, when the user disposes of the JOptionPane, then the calling code resumes program flow from right below where the option pane was displayed.
  • When you display it as a JFrame, it is in a non-modal window, meaning that program flow in the calling code does not stop when the JFrame is displayed. And so if you query the state of the JPanel's components immediately after it is displayed in the JFrame, you are making this query before the user has had a chance to interact with it.

One solution: display it in a modal JDialog or in a JOptionPane, not a JFrame.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I did figure out but why it's effecting checkBox only? why not other columns which are of type `String`? – Volatil3 Jul 20 '14 at 00:06
  • @Volatil3: this is all I could come up with based on your description of the problem. For more detailed help, please try creating your [mcve](http://stackoverflow.com/help/mcve). – Hovercraft Full Of Eels Jul 20 '14 at 00:07
  • The reason I had to go for `JFrame` that I had to make it Modaless plus had to add custom buttons :( – Volatil3 Jul 20 '14 at 00:09
  • @Volatil3: custom buttons has nothing to do with JFrame vs. JDialog as both can display the same JPanel GUI's. But you seem to be expecting both modal and non-modal behavior. Otherwise you will need to query the state of your components based on some event listener. – Hovercraft Full Of Eels Jul 20 '14 at 00:13
  • So you mean: 1)I quit `DEfaultTableModel` and go for `AbstractModel` 2) Add Listener for `Jtable` and check `tableChange()` method ? – Volatil3 Jul 20 '14 at 00:18
  • @Volatil3: No, 1) you can extend DefaultTableModel if that serves your purpose, although extending from the AbstractTableModel often allows you to produce cleaner code, since you can avoid using Object type so much and having to cast your values. and 2) your listener could be a TableModelListener or a WindowListener or ActionListener, depending on which event you want to listen for. Up to you. – Hovercraft Full Of Eels Jul 20 '14 at 00:29
  • OK. Since I have a few buttons. I try to use `ActionListener` and try to iterate records and check Column Value whether it's checked or not. In this way it should solve modality issue too. Am I right? – Volatil3 Jul 20 '14 at 00:31
  • 1
    @Volatil3: Soem examples using `DefaultTableModel` are cited in response to your comment [here](http://stackoverflow.com/a/17265891/230513). – trashgod Jul 20 '14 at 01:32
  • @HovercraftFullOfEels I accepted your answer because you did figure out main issue which helped me to fix my problem by using Event Listener. – Volatil3 Jul 20 '14 at 19:16