This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How do I uncheck a JCheckBox that I am using as one of my header columns?  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My JCheckBox "check all" header column works in that if I check the box, all checkboxes in this column become checked. I want to be able to uncheck the header checkbox if i uncheck one of the checkboxes in the column. For example, I set the checkbox in the header to be checked so now all my checkboxes underneath the header checkbox all become set or checked. If I uncheck a checkbox in the column say in the 5th row, I now want the checkbox in the header to be unchecked because not all boxes are currently checked in the column. I have messed around with the code for awhile with no luck. How do I complete what I am trying to do?


 
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crossposted: https://stackoverflow.com/questions/52845609/how-do-i-uncheck-a-jcheckbox-that-i-am-using-as-one-of-my-header-columns/52856949#comment92645883_52856949

You were asked to post a SSCCE that demonstrates the problem (and you were given suggestion on how to do this)

We can't compile or test your code.

All you did was completely ignore the suggestions from you other questions and dump the code here.

 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:Crossposted: https://stackoverflow.com/questions/52845609/how-do-i-uncheck-a-jcheckbox-that-i-am-using-as-one-of-my-header-columns/52856949#comment92645883_52856949

You were asked to post a SSCCE that demonstrates the problem (and you were given suggestion on how to do this)

We can't compile or test your code.

All you did was completely ignore the suggestions from you other questions and dump the code here.



How or what exactly do you want me to do. Would you also like me to post the code for the JFrame as well so that you guys can compile and run the code. Post screens? The code I have posted above shows my working table that has the checkbox in the header working properly as in when you check it, all things get checked and when you uncheck it, all things get unchecked. But my problem is I have no clear idea how to program the checkbox to uncheck and check itself without a mouseclick occuring on it although I have come close. I have tried all types of ways messing with the code. I have been trying to solve this problem for awhile since there isn't or hasn't been a clear solution to this problem that I have searched all over on the web. Would you like me to post all the different types of ways I have tried to solve this. I have somewhat found a close solution in terms of getting the checkbox to self check uncheck itself but there is a problem in a delay where it didn't happen right away on the screen and so it was unacceptable. Should I post this as well?
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How or what exactly do you want me to do.



In this question I provided a link for information on providing a "SSCCE".

In your other question you were given the link for information on providing an "MCVE".

They are both the same concept.

We want simple code that demonstrates the problem, not just a dump of your application.

Would you also like me to post the code for the JFrame as well so that you guys can compile and run the code.



Yes, We need to be able to see exactly what you are doing and we need to be able to use our own problem solving skills

But we do NOT want a dump of your existing application.

The SSCCE should contain standard JDK classes, except for your custom code.

So we don't want to see your custom TableModel. As I told you in your other posting you can just use the DefaultTableModel to display the data. You just need to override the getColumnClass() method for the column to contain the Boolean values.

The point is we want to eliminate the possibility of errors when you use non JDK classes.

So the only custom class you need is the header renderer.

A few tips:

1. you don't need the ItemListener class. When you invoke the doClick() method, you just update the data in all the rows at the same time
2. you don't need a checkAll() and uncheckAll() method. You just use a variable to indicate the new state.

Something like:


So get your custom check box renderer code restructured and simplified.

Once that is working then you can work on your problem, which is unchecking the checkbox in the header.

As I stated in your other question you can use a TableModelListener and I gave you a link to a working example which contains a SSCCE/MCVE for you to play with so understand the basic concept of using this class.

So know you can modify your custom check box renderer class to also implement TableModelListener. Then when an event is generated you simply get the value of the Boolean variable. If it is false, you change the state of the check box and then repaint the header.

Again if you have problems you have a much simpler SSCCE/MCVE to post.
 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I will attempt this again.

First I will post another version of the previous code. It just deals with a table but this time I’m just using one column instead of 5 to keep it simple and contains the functionality of what I have so far without the implementation of the solutions I have attempted so far . I have also changed and used a DefaultTableModel instead of the one I had as suggested although I didn’t see much difference with the Table model I was using and the AbstractTableModel but I am still quite new to JForms in general  so I have made the adjustments. I also removed my ItemListener and did the update of all values in the columns underneath where doClick() occurs - line 153 - JPanel Code. I also added a TableListener as well.  I know I'm dumping more code but I want to explain myself clearly and I think you would have better understanding if you have all the code I'm working with.

I have also pasted a link to download the code so if you would like to run it and see what it does below. It basically contains the code before any of the solutions I have tried to come up with were implemented. There is also a button that just prints out the actual values of the array that is used by the table just so I can make sure the checkmarks match the Boolean values of the array.

Here is the code which is what is found in the download file. I am using Netbeans btw as my ide.
https://drive.google.com/drive/folders/15bmmzKSygAqaaFPpT1FtgMGjrLLJW_dW?usp=sharing

The JFrame

 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The JPanel

 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So far what I have is this




When I click the checkbox header I get all values to be checked as expected.



Now after I uncheck one of the values (red arrow), I then want the checkbox header to uncheck itself(blue arrow). Right now the only way I can get the actual header to check/uncheck itself is by clicking directly on it but I want it to uncheck itself automatically after unchecking one of the values in the table.



I will post some of the solutions I have tried to attempt but it's getting late so I will do that when I get the chance.

Your suggestion is to use a table model listener which I implemented here in the following code



At the moment, when I click on a value in the column, all it does is state which row is clicked for the listener as the above code shows. You said "Then when an event is generated you simply get the value of the Boolean variable. If it is false, you change the state of the check box and then repaint the header".
I have a good idea of keeping track of the state of the checkbox but the repainting part is where I am stuck at. So lets say all values in my column are all checked. Now I uncheck a value in the column. So now my TableListener is called. I change the state of the header checkbox but now how do I repaint the checkboxheader itselft to display this state of change. I know after messing with the code that this following line found in my renderer - line 138 is what does the painting part of the header in my mouseclick methods


So one of my solutions was to create a global variable of type Source and assign it to the above code so now I can use this variable and call it in another function outside of a mouseclick event with no luck as well as creating a global JTableHeader and assigning the above to it as well.
How do I get this line of code to work in my table listener?
 
Rob Camick
Rancher
Posts: 2974
20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm not a fan of code generated by an IDE.

1. you get all the extra layout code which hide the code directly related to the problem
2. you can't structure you code to create a reusable class, which in this case would be the check box renderer

Also, a TableModel is for storing data. The Boolean values should be stored in the model, not in some external storage variable with a hard coded size restriction. You also overly complicated the model because of the external storage. There is no need for custom getValueAt() or setValueAt() methods.

So here is my approach.

All you are trying to do is create a reusable class that can be added to any JTable where you want to use a checkbox in the header and automatically selected/deselect the values in the same column.

So the class is  called CheckBoxHeaderRenderer, which contains all the relevant logic.

The rest of the code in the main() method is just to build a simple GUI containing a JTable to test the above class.

It shows how to add the CheckBoxHeaderRenderer to the table.

 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. I now understand how to repaint the header using the tablemodel listener.

Now I have ran across another problem.

I have used this class and tried to implement it to two columns in the same table and I am running into some problems.

I first called the following code below.



First, one thing I noticed is that in my tablemodel listener, whenever it fires off, the value that I get from tme.getColumn() from the tableChanged()... method that always returns a 1 instead of the actual column I clicked on. As you can see in my code that I am using coluimns 4 and 5 so if I click on 4 I should be getting a 4 and 5 if I click on 5 correct?

Another thing is whenever I click on a cell in column 4 to check or uncheck the box, I saw that the tablelistener gets called twice, first for column5, and then for column 4. I placed some system.out.printlns in the tableChanged method and wanted to see which column it was using so basically whenever I updated a cell in column 4, it would fire off the tableChanged method first using column 5 as the column and proceeded to execute the functions inside it which I do not want and then a second round of the tableChanged() method but this time using column 4 and then proceeded to execute the same functions in the method. It happens if I update a cell in column 5 as well with the tablelistener executing twice in the same order first using 5 as the column and then 4. If I could get tme.getColumn() to actually return the right column I clicked on then this would be a easy problem but at the moment am stuck trying to figure this out.

This would explain why in another table, I had, that the header in column 5 was being repainted even though I was updating a cell in column 4.

Here is the my CheckBoxRenderer code which I modified



I modified the tableChanged(....) method to take into account if the user manually checks every cell in the table to true and the header to repaint itself to true if this occurs.

How do I prevent my table listener from firing twice or get the actual column from tme.getColumn() to return the right value as it did in your class?

I was trying to do something where I created another model as you can see in the outcommented TableModel model2 and create another listener thinking that creating another listener would work but it still kept firing off twice.
 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a sample output when I clicked on a cell in column4 when running my program

stateOfHeader = false
bool = false
Column = 5
tme.getColumn = 1
tme.getFirstRow = 1
tme.getLastRow = 1
tme.getType = 0
tme.getSource = com....(something something)
tme.getClass = class javax.swing.event.TableModelEvent
stateOfHeader = false
bool = true
Column = 4
tme.getColumn = 1
tme.getFirstRow = 1
tme.getLastRow = 1
tme.getType = 0
tme.getSource = com....(something somethign)
tme.getClass = class javax.swing.event.TableModelEvent
 
Rob Camick
Rancher
Posts: 2974
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks for your reply.



Yes, well you should be responding when the suggestion is given so everybody in the forum knows the status of the question, not 4 days later when you want follow-up help.

I saw that the tablelistener gets called twice,



Yes, you have two listeners listening for changes to the table model.

whenever it fires off, the value that I get from tme.getColumn() from the tableChanged()... method that always returns a 1 instead of the actual column I clicked on



I don't believe that. The event will contain the model row/column that was changed.

Again, whenever you have a problem post a proper SSCCE that demonstrates the problem. I gave you simple code that included the renderer and the code to create the table and display it on a frame all you had to do was copy/paste/compile/test.

If you have a problem, then prove it by posting simple code that I can copy/paste/compile/test, otherwise all I can say is that the problem is with the code you didn't post.

if I click on 4 I should be getting a 4 and 5 if I click on 5 correct?



Correct, and that is why my code checks the column from the event to make sure we are processing only changes for the specified column.

The TableModel generates a TableModelEvent for changes in any row/column. It does not know what row or column you might be interested in.

Why did you change the logic I gave you if you don't know why the logic is there? If you have a question about the logic, then ask what it does, don't just ignore it.


Once again the above logic is different from the logic that I provided for you. One of your statements is correct the other is not.

In many cases the model and view are the same, but not always, so you need to understand the difference between the "model" and the "view".

The model contains all the data available for display in the table.

The view is for how the data is displayed.

For example the user could reorder the columns of the table. They could move the last column to be the first column. The data in the model doesn't change, only the view of the table is changed.

Or, you could have 100 rows in the model, but then sort or filter the data in the table. Again the data in the model doesn't change, only the view is changed to paint the data in a different order.

The method table.getValueAt(...) and table.getModel().getValueAt(...) can return different results if the view has been modified so you need to know when to use each method.

Note how I used model.getValueAt(...). You changed your code to use table.getValueAt(...) which is (potentially) wrong if the user ever reorders the columns.

However, you usage of table.getRowCount() is correct, since you only care about the rows being displayed in the table.

The change I made to my code to have the header automatically reset itself is as follows:




 
Adam Villahermosa
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Camick wrote:

Thanks for your reply.



Yes, well you should be responding when the suggestion is given so everybody in the forum knows the status of the question, not 4 days later when you want follow-up help.



If it means anything, I did go back to the original Stack Overflow question and linked it back to this page while mentioning that the solution was posted here several days ago. I just forgot to add a response to update here as well. If you don't believe me just look at the Stack Overflow page.

Rob Camick wrote:

Why did you change the logic I gave you if you don't know why the logic is there? If you have a question about the logic, then ask what it does, don't just ignore it.




I'm not understanding this part. I didn't change much except for the tablechanged listener part where I added code for adjusting the table when you select all cells manually and then the header paints itself to true. Are you referring to moving this code to the handleclickevent() and not in the mouseclicked event? If this is the case, I actually did not differentiate the two at first meaning I didn't notice that your code was in the mouseclicked method and my original code was in a handleclick method. So at first, I remember when implementing the code you gave me, I wasn't getting all my cells to update with the single click of the header and then I noticed that your code was all in the mouseclicked method and I forgot what I remember doing at this point but basically just did it in the handleclick event since I had it working at the time. It still works but I will probably modify it and put it back in the mouseclicked method since it means less code.


Rob Camick wrote:
I don't believe that. The event will contain the model row/column that was changed.

Again, whenever you have a problem post a proper SSCCE that demonstrates the problem. I gave you simple code that included the renderer and the code to create the table and display it on a frame all you had to do was copy/paste/compile/test.

If you have a problem, then prove it by posting simple code that I can copy/paste/compile/test, otherwise all I can say is that the problem is with the code you didn't post.



The code you provided me worked as well in my own table that I created as in I do get the right column from tme.getColumn() . The thing is I am trying to implement this to an already existing project. This is why I had an AbstractTable at first as my table model in the very first code I posted because this is what the project is using. You then told me to use a Default-table model which I did. At first before posting this thread, I basically tried to create my own table in my own Netbeans new project mimicking the structure of the table from this project which is why I was using an abstract table model because it was using an abstract table model. The code you gave me for the Checkbox header renderer I then implemented into my own table as well because yours created the GUI programmatically and I modified it to use a table already created from design and I did get the right column in my project but when I tried to move everything I learned to this larger existing project, I cannot figure out why I am receiving a 1 every time.

I did however find another solution to my previous problem which is creating a global variable that is set to the actual column that is clicked and use this to prevent the other column I don't want from executing any code so instead of using tme.getColumn() to get the column that was clicked, I just used a global variable for now until I can figure out why so at the moment, the listener still fires off twice for both column 4 and 5 but I use the global variable to use so the listener knows which actual column to execute so my checkbox renderer is working correctly as of now.
 
Rob Camick
Rancher
Posts: 2974
20
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I did go back to the original Stack Overflow question and linked it back to this page


How does that help the people of this forum? They don't know that you are satisfied with the suggested solution.

That is the problem with cross posting in multiple forums. You always forget to update both forums when a solution is found and a reason why I won't be answer future questions if they are cross posted.

I'm not understanding this part...



You misread my comment. The code you included above does not belong to the comment. My comment was addressing your issue of getting the wrong column in the TableModelListener, which I still don't believe because you haven't posted a SSCCE demonstrating the problem.

My original code was:


You change it to:

That is why you have all the issues with multiple events being generated and so on.

In my latest version I changed the code to:

To make it more obvious that the two conditions must be checked together.

I did however find another solution to my previous problem which is creating a global variable...


That is a terrible solution. I just gave you a working solution with the code from above. Again, I ask why are you changing my basic code and then complaining it doesn't work? Understand the reason for the code and it you don't understand it, then ask a follow-up question. Don't just ignore it.

The thing is I am trying to implement this to an already existing project


It doesn't matter. The CheckBoxHeaderRender class I gave is reusable for any JTable. That is the whole point of creating a self contained class.

You should be able to add it to your existing project with a single line of code:

Your columns will obviously be different.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!