jtree and jtable syn

Posted on 2006-04-21
Last Modified: 2008-03-17
hello there,

i have a Jtree and a Jtable which gets populated from how should i implement a feature,so that when i click on a Jtree node,it should highlight the record in the Jtable.

Question by:zolf
    LVL 14

    Expert Comment

    do you mean somthing like this ?

    the tree:
    + row1
       - col1
       - col2

    the table:
    ____    col1    col2
    row1   val11   val12
    row2   val21   val22

    and when you click on col1 on tree then the corresponding cell in table should be highlighted
    confirm if im on the right track

    Author Comment


    LVL 14

    Expert Comment

    for JTree register a TreeSelectionListener through this method addTreeSelectionListener(TreeSelectionListener tsl)
    have the nodes of JTree to have row-col indices of themselves in JTable
    every time a selection occurs select the proper row-col in the JTable by calling table.changeSelection(row, col, false, false);
    LVL 14

    Expert Comment

    did you get My idea ?
    each JTree node must be aware of its corresponding row and column indexes in JTable
    so whenever it gets selected, you know which cell in JTable should be selected correspondingly

    Author Comment


    can you provide me a small demo with code.if you dont mind.
    LVL 14

    Accepted Solution

    just as a path on how to do it

    // this is used as JTree node
    Class Node {
        int row, column; // these two represent the cell that is correspondent to this node
        ... // other values

    // register a selection listener
    addTreeSelectionListener( new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent e)  {
            find the selected tree node
            get the values of row and column from that
            select the corresponding cell from JTable at position [row, col]
    LVL 14

    Expert Comment

    if you have trouble making JTree nodes point to JTable cells you have still another choice: every time a node in JTree gets selected, search the entire JTable to find an equivalent cell in JTable and highlight it.
    my first suggestion needs some preindexing, at the time you populate JTree and JTable, you need to make the interrelations (row,col) and it saves you a lot, by eliminating the search cost in JTable.
    you may try either one of these two choice

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 55
    IT Company 5 55
    stringclean challenge 26 43
    mapBully challenge 6 53
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now