• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 502
  • Last Modified:

jtree and jtable syn

hello there,

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

  • 5
  • 2
1 Solution
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
zolfAuthor Commented:

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);
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
zolfAuthor Commented:

can you provide me a small demo with code.if you dont mind.
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]
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now