Patrick (patrickwonders) wrote,

  • Mood:

Non Obvious -- Tunnelling through Java

I have spent way too much time over the last several days trying to beat JTree into submission. The Java tutorials and various other online sources are rife with examples if you want to make a custom-displayed JTree or a custom-edited JTree. Have fun if you want to do both at the same time.

I had a nice, custom-displayed JTree all working. But, I couldn't click on the checkboxen that were part of my TreeCellRenderer-returned Component.

I tried, first, to just add a MouseListener. But, I ended up with all kinds of wackiness because the selection mechanism was getting the same events. So, I was able to click the checkbox, but I couldn't unclick it unless I first clicked somewhere off of the current node (so it was no longer selected) and then came back.

I played around for a good long time before I tried to follow along with one of the many JTreeTable implementations on the web. So, I added:


All that to try to get one column of checkboxes and one column of labels with icons.

After about five hours of futzing, I had it actually displaying the right labels on things. And, I could click the checkboxes. But, I wasn't getting the custom icons that I had before in my "simple" JTree. Ah well, forge ahead for the moment.

Well, not really because my TreeNode instances extended javax.swing.JLabel and implemented javax.swing.tree.TreeNode.

I hear you saying, "So?" But, here's the deal.... JLabel is a subclass of java.awt.Component which defines a method getParent() which returns a java.awt.Container. However, javax.swing.tree.TreeNode demands a method getParent() which returns a javax.swing.tree.TreeNode (which is not a java.awt.Container).


Now, I've ripped it all back down to just a JTree with my own TreeCellRenderer and my own TreeCellEditor. And, with just a few overrides of the DefaultTreeCellEditor, I am able to let single clicks toggle my checkbox while double-clicks expand and collapse the tree node. Friggin' yay.

All of that makes me sure that I'm going to dream about JTree stuff again tonight. A day or two ago, I dreamt...

I dreamt that I was back in my old office with my (then) officemate jerronimo. He was playing some U2 on his laptop. Nothing unusual about that.

What was unusual though was that all of this was in a big JTree with a custom javax.swing.tree.TreeModel and specially tuned javax.swing.tree.TreeNode's for each level of the hierarchy. I was viewing an augmented-reality-ish overlay of the TreeNode data structures (somewhere between UML and gdb) atop the whole office scene.

The tree, from leaves toward the root went like this:

  • Songs
  • Albums
  • Bands
  • Genres
  • Transport Media (file-sharing, file-streaming, FM radio, etc.)
  • Receiver Devices (laptops, workstations, radios, etc.)
  • Receiver Operators (jerronimo's arms and hands, period)
  • Listeners (jerronimo, myself, and a reference to boba, period)
  • Listening Spaces (the office, the hall outside the office, period)
  • The Campus
  • The Airspace above Henrietta [though it wasn't really axis-aligned with campus (which is basically oriented north-south, east-west, up-down)]
  • ... not sure where from there...

I think I'm gonna play some Star Wars: Battlefront before sleep. Hopefully knocking over a few AT-ATs will flush some code out of my gourd.

  • Post a new comment


    default userpic

    Your reply will be screened

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.