Ultra-powerful tables with SwingX and FilterHeader in just 20 minutes

Remember, you can find all of the articles code and tools, as well as latest additions and changes at project code site, http://code.google.com/p/swingtools/.

 

Previously we’ve created a really powerful addition to the tables – the model which automatically figures out which columns are available from the given data set, their types which will determine the rendering and editing data. The performance of that model is extremely high as well especially on very large data sets comparing to more heavyweight beans binding solutions which is always a point of interest when working with tabular data that tend to grow huge.

 

Let’s continue on user experience side. Standard JTable does not provide almost anything to make user’s life easier, comparing to its quite famous colleague, pivot table from Microsoft Excel, which allows you to do various filtering and even calculate trends and functions based on data in the table. We’ll leave functions and calculations aside, it’s too complicated and will take a lot of time, and rarely needed, most of such functions are done by the application business layer instead. If you still need something like that where logic is interleaved with presentation, you can either use JIDE’s grids implementation (though they are commercial, behave differently and cost quite a bit) or attach scripting with JDK’s built-in Javascript and use it as a macro language.

 

We’ll concentrate on simpler usability and appearance things which are still missed from standard JTable, but frequently required by users. First of all, sorting is available now in the JTable, but disabled by default. But more advanced sister of JTable, JXTable from open-source SwingX set, does enable sorting by default, so it will be a good idea to always start with JXTable. The other powerful thing from JXTable is that it allows you to hide columns and restore them back, and more than that, provides a menu to do that with a custom button located in upper right corner of the scroll pane where a JXTable is put. One of the most powerful features of SwingX table is that it allows you to do highlighting over the rows based on some criteria, including simple striping, i.e. interleaving colors between even and odd rows of the table which makes them easier to read. Let’s start:

public class BaseTable extends JXTable {

    public BaseTable() {
        // always enable column control and striping
        setColumnControlVisible(true);
        setHighlighters(HighlighterFactory.createSimpleStriping());
    }

Moving on, there is filtering built in into the standard JTable, but it’s headless (has no UI) and requires some effort to enable and write filters programmatically. Meanwhile, there are no default UI components to do the simplest filtering on the table’s data, neither in core JTable nor in JXTable. Fortunately, there’s a nice open-source solution available from Coderazzi and it’s available from the central Maven repository. It’s extremely easy to use – we just need to create a new instance of table filter header UI component and pass our table into it. Table filter will track model and data changes and update itself automatically. It makes sense to include the table filter header directly into our advanced table:

public class BaseTable extends JXTable {
    /** Filter header used in the table */
    private TableFilterHeader tableFilterHeader;
    /** Flag to enable or disable filter header */
    private boolean filterHeaderEnabled;

    public BaseTable() {
        // always enable column control and striping
        setColumnControlVisible(true);
        setHighlighters(HighlighterFactory.createSimpleStriping());
    }

    public boolean isFilterHeaderEnabled() {
        return filterHeaderEnabled;
    }

    /**
     * Enables or disables and hides filtering header
     * @param filterHeaderEnabled True to enable and attach filter header component
     */
    public void setFilterHeaderEnabled(boolean filterHeaderEnabled) {
        // if filter was off and enabled, create and attach it
        if (filterHeaderEnabled && !this.filterHeaderEnabled && tableFilterHeader == null) {
            // enable the filter header
            tableFilterHeader = new TableFilterHeader(this);
        }
        if (tableFilterHeader != null) {
            tableFilterHeader.setVisible(filterHeaderEnabled);
            tableFilterHeader.setEnabled(filterHeaderEnabled);
        }
        this.filterHeaderEnabled = filterHeaderEnabled;
    }

    /**
     * 
     * @return Filter header component used in the table or null if never used
     */
    public TableFilterHeader getTableFilterHeader() {
        return tableFilterHeader;
    }

As you can see from the code, we don’t attach the filter header directly and forever – you can disable or enable it back, or get it and manipulate it directly. There’s a reason behind that – although quick and simple filtering using the header is nice, it does involve some overhead, especially when new large bulk of data (rows) arrive. By default the filter header is disabled, but it’s easy to enable it only by setting the appropriate flag to true.

 

Another thing that is missing from the both JTable and JXTable is tooltips for the cells. As we all know, most of the time the cells are rendered by the JLabel, which hosts some form of text representation of the value in that cell. To enable tooltip, you need that label to have it installed with setToolTipText(). Override all renderers and call it explicitly? Luckily, no. There is a helper method in  JTable class which gets called every time before renderer got prepared to be painted in the cell. Since it’s a single point of access to all of the renderers installed on the table, we can override it and do a simple job of enabling tooltips there:

/**
     * Overriden to set the tooltip text for the label-based renderers.
     * @param renderer
     * @param rowIndex
     * @param vColIndex
     * @return 
     */
    @Override
    public Component prepareRenderer(TableCellRenderer renderer,
            int rowIndex, int vColIndex) {
        Component c = super.prepareRenderer(renderer, rowIndex, vColIndex);
        if (c instanceof JLabel) {
            JLabel label = (JLabel) c;
            label.setToolTipText(label.getText());
        }
        return c;
    }

Taking the demo from previous article, where we dynamically populated model with beans, let’s do the same, but use new BaseTable with advanced functionality and filter header enabled:

final BaseTable table = new BaseTable();
        table.setFilterHeaderEnabled(true);
        add(new JScrollPane(table));
        BeanPropertyTableModel<TableBean> model = new BeanPropertyTableModel<TableBean>(TableBean.class);
        model.setOrderedProperties(Arrays.asList("name","surname","date"));
        model.setData(TableBean.generateList(100));
        table.setModel(model);

 

We’ll have the following – notice that you can hide and show all columns or adjust them to their preferred width (“pack”):

Table Demo

 

 

Tooltip are available for all cells by default, without dealing with renderers individually. And filtering works like a charm (in this screen, first column shows everything starting with Z, seconds everything starting with P – both of them should be true so we have only one such a row):

Table Demo

 

Much better and more usable than the standard JTable, and still as fast as it is.

 

This entry was posted in Uncategorized. Bookmark the permalink.

371 Responses to Ultra-powerful tables with SwingX and FilterHeader in just 20 minutes

  1. Heli says:

    Much appreciated for the information and share!

  2. Nancy says:

    Thanks for the share!
    Nancy.R

  3. Nelle says:

    I can’t believe I’ve been going for years without knwoing that.

  4. dvfheq says:

    rfiZsj , [url=http://egtvzmexaphq.com/]egtvzmexaphq[/url], [link=http://cqaasfnbetrb.com/]cqaasfnbetrb[/link], http://cwavoxaujoox.com/

  5. 1820114 says:

    1820114 beers on the wall. sck was here

  6. Hello! I just wanted to ask if you ever have any problems with hackers? My last blog (wordpress) was hacked and I ended up losing many months of hard work due to no backup. Do you have any solutions to stop hackers? Baufinanzierungsrechner Online

  7. Woah! I’m really enjoying the template/theme of this blog. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and visual appeal. I must say you’ve done a superb job with this. Also, the blog loads very fast for me on Safari. Exceptional Blog! Krankenkasse Vergleich

  8. Aufgrund seiner Reserven hat m.E. nach aber einen wesentlich solideren Handlungsspielraum als die USA und zwar ohne QE. Krankenkasse Vergleich

  9. Hello there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the same niche. Your blog provided us useful information to work on. You have done a wonderful job!
    Kredite Sofort

  10. Great! thanks for the share!
    Arron

  11. Awesome blog! Do you have any hints for aspiring writers? I’m hoping to start my own website soon but I’m a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there that I’m totally overwhelmed .. Any ideas? Many thanks!
    Sofortkredite

  12. a1263690 says:

    I’ve said that least 1263690 times. The problem this like that is they are just too compilcated for the average bird, if you know what I mean

  13. You really make it appear so easy with your presentation but I in finding this matter to be actually one thing that I believe I might by no means understand. It seems too complex and extremely extensive for me. I am having a look forward to your next submit, I will try to get the dangle of it!

  14. That is the right weblog for anyone who wants to seek out out about this topic. You notice so much its almost arduous to argue with you (not that I actually would need…HaHa). You undoubtedly put a brand new spin on a subject thats been written about for years. Nice stuff, just nice moncler inverno 2016

  15. Your article has inspired me. It’s interesting, thoughtful, compelling and well-written. You are a very talented writer with great skills and original thoughts. Your viewpoints match mine in many ways. http://www.samsung1080phdtv.net/

  16. Good day! I could have sworn I’ve been to this website before but after checking through some of the post I realized it’s new to me. Anyhow, I’m definitely glad I found it and I’ll be bookmarking and checking back frequently! Kredite ohne Schufa

  17. The apple company is now offering Rhapsody just as one software, a fantastic start off, but it’s at present hindered with the lack of ability to maintain in the area with your ipod touch, possesses a new disappointing 64kbps touch charge. In the event the modifications, it is going to relatively negate this specific advantage to the Zune, even so the Ten songs monthly it’s still a sizable also inside Zune Pass’ favor.

  18. smftijqtpguxbsf, Capsiplex, VxmQvsL.

  19. scqcljqtpguxbsf, green hosting, NfvWwlb.

  20. Meridia says:

    vpcfnjqtpguxbsf, Reductil, OrtXmNr.

  21. Vicodin says:

    orabijqtpguxbsf, Vicodin, hQjDhGu.

  22. Cialis says:

    jizxvjqtpguxbsf, Buy Cialis, wAhPepx.

  23. lens.com says:

    snvqxjqtpguxbsf, lens.com coupons, tGiRRBx.

  24. tkcwfjqtpguxbsf, Levitra, fXHrQDZ.

  25. izrdpjqtpguxbsf, Viagra, UWVcdYQ.

  26. Buy Viagra says:

    ccqudjqtpguxbsf, Buy Viagra, OqyCyki.

  27. ftd says:

    aaupqjqtpguxbsf, ftd, zVKkRHV.

  28. Market risk says:

    lbbcejqtpguxbsf, risk, nrsflpO.

  29. Cialis cheap says:

    oigomjqtpguxbsf, Generic Cialis, NxgOCyH.

  30. eclccjqtpguxbsf, omnis coupons, aUzNuFd.

  31. Does your blog have a contact page? I’m having problems locating it but, I’d like to send you an email. I’ve got some ideas for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it expand over time. privat kreditvergleichen

  32. grcbmjqtpguxbsf, Cialis, NwmHbxX.

  33. Buy Xanax says:

    alqsxjqtpguxbsf, Xanax withdrawal, xkQgwMY.

  34. Natural hgh says:

    osjffjqtpguxbsf, Genf20 reviews, jvQVqpz.

  35. Awesome site you have here but I was curious about if you knew of any user discussion forums that cover the same topics discussed here? I’d really like to be a part of community where I can get suggestions from other experienced people that share the same interest. If you have any suggestions, please let me know. Bless you!

  36. ndhrmjqtpguxbsf, Cialis, MFnbNMt.

  37. Hgh reviews says:

    cbsbbjqtpguxbsf, Hgh product, TNgKHxZ.

  38. Ambien says:

    wqfqajqtpguxbsf, Buy Ambien, syAEJhH.

  39. eyebuydirect says:

    cldrkjqtpguxbsf, Eyebuydirect, iiQHLEs.

  40. Buy Valium says:

    bjalmjqtpguxbsf, Valium, VvvAqna.

  41. TRX says:

    wtlpujqtpguxbsf, TRX, cyBTqGi.

  42. Dogfunk says:

    zpewijqtpguxbsf, dogfunk coupon, kWnJCvP.

  43. hlazqjqtpguxbsf, skinit coupons, LygKwuY.

  44. Tadalafil says:

    ntzqfjqtpguxbsf, Tadalafil, kEXMBbE.

  45. bcneljqtpguxbsf, Levitra, MZjmneA.

  46. Cialis says:

    vzudqjqtpguxbsf, Cialis, XstwGxC.

  47. drgcajqtpguxbsf, Viagra, gMEkUuf.

  48. Valium says:

    azptajqtpguxbsf, Buy Valium, uaYVfGl.

  49. Claripro says:

    wtnhhjqtpguxbsf, Claripro, bzkSkaW.

  50. Viagra says:

    zymzbjqtpguxbsf, Viagra, PquFaDj.

  51. external hd says:

    fwjksjqtpguxbsf, external hd, dDzuwal.

  52. Cialis says:

    kgfyijqtpguxbsf, Buy Cialis, dCMKWph.

  53. Cialis says:

    jykzgjqtpguxbsf, Cialis 20mg, WOmQtES.

  54. Propecia says:

    kkegejqtpguxbsf, Propecia, XLXFCeH.

  55. Free viagra says:

    pajxrjqtpguxbsf, Generic Viagra, voIXcNQ.

  56. lmxcijqtpguxbsf, weight loss, HYKkqVp.

  57. hgjaxjqtpguxbsf, Tramadol, keanSaU.

  58. jnnozjqtpguxbsf, Levitra, nltOIeH.

  59. pzfbjjqtpguxbsf, payless shoes, yVbUaan.

  60. vqjzgjqtpguxbsf, cooking games, dhGlecf.

  61. otkkljqtpguxbsf, spiegel, VSPaIGE.

  62. Buy Cialis says:

    yutlpjqtpguxbsf, Buy Cialis, BGluglC.

  63. Tramadol says:

    srkjcjqtpguxbsf, Tramadol forum, uvYJMRq.

  64. DancingBear says:

    scewpjqtpguxbsf, DancingBear, csSaZse.

  65. Valium says:

    wtfprjqtpguxbsf, Buy Valium, xNTPQMn.

  66. zztdtjqtpguxbsf, Propecia, TiMDtSG.

  67. Codeine says:

    ncldhjqtpguxbsf, Codeine, eZdbscH.

  68. My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on numerous websites for about a year and am concerned about switching to another platform. I have heard great things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any kind of help would be really appreciated! Kredit ohne Schufa

  69. Tadalafil says:

    tnbgnjqtpguxbsf, Tadalafil, nHfTebW.

  70. Buy Cialis says:

    ipmdwjqtpguxbsf, Generic Cialis, GFNtDKH.

  71. tblihjqtpguxbsf, Levitra, GmiHSXr.

  72. Buy Cialis says:

    ichcijqtpguxbsf, Buy Cialis, EWsGQYW.

  73. Capsiplex says:

    adqqpjqtpguxbsf, Capsiplex, WQOLXfH.

  74. Buy Valium says:

    lnpgujqtpguxbsf, Buy Valium, Pwqmwdo.

  75. rqcjijqtpguxbsf, Lean Muscle X, CIdhckg.

  76. expedia says:

    lwpjgjqtpguxbsf, expedia, jRMyhmc.

  77. zmfdojqtpguxbsf, Cialis, UItewjP.

  78. koqbpjqtpguxbsf, Generic Xanax, KgwJBYu.

  79. Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can recommend? I get so much lately it’s driving me crazy so any assistance is very much appreciated. Sofortkredit Schufa

  80. Viagra says:

    gscdpjqtpguxbsf, Viagra, GYTZUtG.

  81. CamFrog says:

    claiajqtpguxbsf, Camfrog capture, dLoCxDG.

  82. Cialis says:

    xnywljqtpguxbsf, Cialis, aoSgTLy.

  83. rffgcjqtpguxbsf, Savings Rates, LAHwVrG.

  84. aovjqjqtpguxbsf, Keflex, gtBppAN.

  85. Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is totally off topic but I had to tell someone! Sofortkredit ohne Schufa

  86. Buy Levitra says:

    jlabtjqtpguxbsf, Levitra, RaWXaMD.

  87. Sweet blog! I found it while surfing around on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Thank you Sofortkredit ohne Schufa

  88. qcouljqtpguxbsf, Buy Ativan, RgPFrxu.

  89. Hey there great blog! Does running a blog such as this take a great deal of work? I have absolutely no knowledge of programming however I was hoping to start my own blog soon. Anyhow, if you have any ideas or techniques for new blog owners please share. I know this is off topic but I just needed to ask. Many thanks! Sofortkredit Schufa

  90. Viagra says:

    oepbrjqtpguxbsf, Generic Viagra, mVKOJsm.

  91. Cialis says:

    xcvetjqtpguxbsf, Forms of cialis, jvpiNst.

  92. vxzmnjqtpguxbsf, Propecia, pwnDETI.

  93. Buy Levitra says:

    eifrxjqtpguxbsf, Effet levitra, jhYYnmJ.

  94. upzuajqtpguxbsf, Keflex, waYOXXO.

  95. quwuxjqtpguxbsf, Viagra, vVHSlRC.

  96. Butalbital says:

    bhouijqtpguxbsf, Butalbital, CjeuNsd.

  97. pzwdtjqtpguxbsf, Ativan dose, wvYQHwn.

  98. Tramadol says:

    xyyajjqtpguxbsf, Tramadol, UwTfWwP.

  99. Buy Ultram says:

    pqgwnjqtpguxbsf, Ultram, OFgMnyp.

  100. Valium says:

    dsxmfjqtpguxbsf, Generic Valium, dwRpQIH.

  101. Cialis says:

    kgrhijqtpguxbsf, Cialis, iJPwJgs.

  102. Viagra says:

    kgraqjqtpguxbsf, Generic Viagra, ryjTWgD.

  103. Cam4.cam says:

    jzjtijqtpguxbsf, Cam4 rialove, pGPbYLs.

  104. Celebrex says:

    dcbvljqtpguxbsf, Celebrex, pSjGoBF.

  105. Buy Valium says:

    yiforjqtpguxbsf, Buy Valium, ZUUVXZV.

  106. Howdy I am so excited I found your website, I really found you by accident, while I was searching on Digg for something else, Anyways I am here now and would just like to say cheers for a remarkable post and a all round interesting blog (I also love the theme/design), I donit have time to read through it all at the moment but I have saved it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent work. haftpflichtversicherung vergleich

  107. Today, while I was at work, my sister stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My apple ipad is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone! haftpflichtversicherung vergleich

  108. uwdlhjqtpguxbsf, Cam4 hack, yfGQIeA.

  109. Buy Ultram says:

    smpbzjqtpguxbsf, Ultram, QgragXw.

  110. Hcg diet? says:

    cxzobjqtpguxbsf, Hcg diet pellets, nvKGMst.

  111. Tramadol says:

    cozrijqtpguxbsf, Tramadol pacing dog, bRuaEHr.

  112. yygshjqtpguxbsf, Xhamster vid, ayCdFJO.

  113. Buy Lunesta says:

    xqsqmjqtpguxbsf, Lunesta, WAXkRVr.

  114. iysarjqtpguxbsf, Adipex pill, vPSXQGn.

  115. Levitra says:

    vhkdvjqtpguxbsf, Buy Levitra, YlQdIEU.

  116. Soma says:

    huvcvjqtpguxbsf, Goddess soma, ZQmyvVr.

  117. My spouse and I stumbled over here from a different web page and thought I should check things out. I like what I see so now i am following you. Look forward to looking at your web page again. haftpflichtversicherung vergleich

  118. uanyqjqtpguxbsf, Ambien, ZdzhyLH.

  119. Buy Viagra says:

    dhpwkjqtpguxbsf, Viagra, hCAbdgB.

  120. Dapoxetine says:

    wolqkjqtpguxbsf, Buy dapoxetine, bchIBzx.

  121. hxdnxjqtpguxbsf, Soma, BLcVsrs.

  122. qyxbyjqtpguxbsf, wirefly, LzcXkbJ.

  123. Propecia says:

    iumdjjqtpguxbsf, Generic Propecia, yUiOAiD.

  124. Priligy says:

    ilbqdjqtpguxbsf, Priligy, rXSMIWB.

  125. Cialis says:

    xkmezjqtpguxbsf, Cialis, HySkHGr.

  126. Ativan says:

    irusjjqtpguxbsf, Ativan, BBJCcGD.

  127. Lasix says:

    lbujmjqtpguxbsf, Lasix buy, vPSBBCV.

  128. Nexium says:

    rjwcsjqtpguxbsf, Buy cheap nexium, bBHbicx.

  129. Ambien says:

    nzguxjqtpguxbsf, Ambien, HVOxjoq.

  130. sizegenetics says:

    iuyurjqtpguxbsf, sizegenetics, EPcRqMb.

  131. iyphmjqtpguxbsf, Ambien, zMjrwef.

  132. Viagra says:

    wnghsjqtpguxbsf, Viagra, UouQCbE.

  133. kxlmmjqtpguxbsf, beauty.com, VJXzWdb.

  134. Klonopin says:

    bpnftjqtpguxbsf, Klonopin, sNbILdW.

  135. Hentai porn says:

    vkxvijqtpguxbsf, Anime porn, CWXYRrY.

  136. Valium says:

    vbnqhjqtpguxbsf, Valium, GSjIKsa.

  137. Clomid says:

    zjtcgjqtpguxbsf, Clomid, CHMHouS.

  138. eacngjqtpguxbsf, Cialis, YioiijB.

  139. flexeril says:

    zrpljjqtpguxbsf, flexeril, QKWCtdL.

  140. Revitaslim says:

    fzzbcjqtpguxbsf, weight loss, ShDQzhc.

  141. Buy Vicodin says:

    hscpejqtpguxbsf, Buy Vicodin, bYzZbtA.

  142. Cialis says:

    lfbvijqtpguxbsf, Cialis pill, BXlSDVI.

  143. rzfbsjqtpguxbsf, Meridia, aMvLOrO.

  144. Ativan says:

    olwpijqtpguxbsf, Pictures ativan, xrogIfN.

  145. Somatropin says:

    gvrhjjqtpguxbsf, Hgh product, oAtIWNS.

  146. Buy Xanax says:

    aduqmjqtpguxbsf, Xanax, TUaRIiM.

  147. pjvdjjqtpguxbsf, Cialis, CWQgquY.

  148. ebjryjqtpguxbsf, Xanax, SmgAgCQ.

  149. hhqdljqtpguxbsf, Buy Viagra, nuhNHuk.

  150. Ambien says:

    dtukgjqtpguxbsf, Ambien lunesta, WAKvWHZ.

  151. alloy coupon says:

    mxdenjqtpguxbsf, alloy coupon, IAkPNbd.

  152. Acomplia says:

    qufvqjqtpguxbsf, Acomplia, CUKAdWb.

  153. Buy Nexium says:

    qbrswjqtpguxbsf, Nexium cost, fFMXIEQ.