Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JCheckBox in JTableHeader hides standard sort up/down arrow icon  RSS feed

 
Alain Oten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a classic Jtable (from java 1.6) with several columns and when you click on one column header, it sorts it in ascending or descending order according to standard up / down arrow icon.

I wish also to use checkbox in jtable header to control rowfilter.setSortable. applied on column.

I use javaranch CheckBoxHeader Class from http://www.coderanch.com/t/343795/Swing-AWT-SWT-JF...e/java/Check-Box-JTable-header

It works fine and column still sorts when clicked but standard arrow icon up / down is no longer visible.

Please, is it possible to have checkbox and sort arrow icon both in JTableHeader ?

Thank you for your help.

Best regards.

Alain
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Alain OTN", please check your private messages for an important administrative matter.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code sample in the thread you referred to does not set a TableRowSorter for the table, thus there is no sorting by clicking on the header. Add following lines in the example to enable this functionality:


If you are talking about the up/down arrow in the header of the column that has the check box, it will require much more effort. The up/down icon functionality is provided by DefaultTableCellHeaderRenderer Class AFAIK and since you are sub-classing JCheckBox not DefaultTableCellHeaderRenderer to create the renderer you don’t get the icon in the column that has the checkboxes. This should not be a limitation anyway since clicking on that particular column actually selects/deselects all instead of sorting.
 
Alain Oten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Moojid,

You are right.

CheckBoxHeader Class is just a sample that I tried to use.

I do have a TableRowSorter and what I'm trying to do is to subclass DefaultTableCellHeaderRenderer Class AFAIK to keep up/down icon functionality and add a chekbox into it.

I am pretty new to Java.

Maybe, without giving me entire solution, you can show me what should be the proper structure to create that subclass and add a checkbox in.

Thank you for your help.

Regards.
Alain.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you would probably need to

1) dig into the source code for RowSorter and in particular the part where it draws the up/down arrow.
2) extend JCheckBox to include the arrow in paintComponent(), in one of 3 states asc/desc/not showing (other column sorted)
3) set the 'extended JCheckBox' as the rendered component

no idea if the above would work, but if it does, post the solution

 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:
1) dig into the source code for RowSorter and in particular the part where it draws the up/down arrow.


up/down icon is drawn by TableCellRenderer not by RowSorter.


Alain Oten wrote:I do have a TableRowSorter and what I'm trying to do is to subclass DefaultTableCellHeaderRenderer Class AFAIK to keep up/down icon functionality and add a chekbox into it.


To be absolutely sure that we are on the same page I will restate what I said in the previous post: Unless you need to sort the rows by state of the checkboxes, the code in the other thread is all you need. It shows the up down arrow in the headers of the columns (except for the column with check boxes).
Now if you really need the checkbox and up/down arrow in the same column, I would suggest not to subclass DefaultTableCellHeaderRenderer for two reasons. 1) this class is not part of public API and can be changes/removed in future versions and your code will fail on newer versions if this happens (and this has happened before). 2) more importantly if you look at the DefaultTableCellHeaderRenderer Javadoc you can see that it extends DefaultTableCellRenderer. Read the class Javadoc for DefaultTableCellRenderer and you will see that this class uses single JLabel in very novel way to paint the headers for all the columns. Sub-classing this class is very likely to introduce bugs.

IMHO the best approach is to change the example in the other thread to extend JPanel instead of extending JCheckBox and add a JLabel and JCheckBox to that JPanel. Have a look at the code for DefaultTableCellHeaderRenderer.getTableCellRendererComponent() for hints on what you need to do.
 
Alain Oten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Michael and Moojid,

Thank you both for your advice.

I confirm that I need the checkbox and up/down arrow in the same column header because I want to filter and to sort several columns.
The up/down arrow is provided by DefaultTableCellHeaderRenderer class for sort feature as I have a TableRowSorter.
The extra checkbox I need in header is to control whether rowfilter.setSortable applies or not on column for filter feature.

Without your advice, I would have had choice between :
1- subclass DefaultTableCellHeaderRenderer and add checkbox in
2 - use CheckBoxHeader and add up/down arrow in
I think that I would have tried 1 but I was not aware that DefaultTableCellHeaderRenderer is not a public class (Thank you Moojid).

So I take 2 as a starting point and change it "to extend JPanel instead of extending JCheckBox and add a JLabel and JCheckBox to that JPanel".

Thank you again for guiding me.
Best regards.
Alain
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading your last post I had a better idea, and I have not tried it out myself so I am not sure if it would work. I suggested before to use a JCheckBox and JLabal inside of JPanel. If you look at the getTableCellRendererComponent method signature it gets a reference to JTable as an argument. You can call




and use that component with JCheckBox in your JPanel instead of using a JLabel, if it works it would save you quite a bit of work.
 
Alain Oten
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Moojid,

It sounds a very good idea.

I am looking forward trying it.
Unfortunately, it is getting pretty late for me.

I tell you more about my try asap.

Thank again.
Regards.
Alain
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!