Solved

refreshing display from a tree control

Posted on 2004-09-02
34
224 Views
Last Modified: 2010-03-31
Hi,

Im having a tree control with three nodes, lets say A, B, C., and Im also having around 10 textfields on which, data is displayed as a particular node is selected. On selection of A, I am displaying some data on 5 textfields, and on selection of B, I am displaying other 5.
What I want to get here is, when i go back to A node, the data that is being displayed by B node to disappear, something like refresh,etc.

For eg:-

      A --------------------------> data is displayed on 5 textfields
      |
       --B------------------------> data is displayed on other 5 textfields
            |
             ----
                  C

so when I choose A node again, this data from B node should dissappear.

All contributions will be greatly appreciatted.

Thanks.
0
Comment
Question by:vihar123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 8
  • 6
  • +1
34 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11961262
As well as displaying fields when a node is selected, you could also clear all other fields as required (or just clear all fields before setting any).

Another option would be to have a seperate form for each node, and swicth the form displayed using a CardLayout.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961272
Try to set the text on the textfields that are filled up from B node by using

setText("");
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:vihar123
ID: 11961317
jTree1.addTreeSelectionListener(new TreeSelectionListener() {
              
                public void valueChanged(TreeSelectionEvent evt) {
                                                                                
               TreePath tp = evt.getNewLeadSelectionPath();
                             Connection conn2;
                               Statement stmt2;
                               ResultSet rset2 = null;
                               DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) tp.getLastPathComponent();                               
                               String empNo = node1.toString();
             
                                                              
                               try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn2 = DriverManager.getConnection ("jdbc:oracle:thin:@fe64700:1521:tanska","anska_test", "praktikant2004");
           
                              stmt2 = conn2.createStatement ();
                                  rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");
                                  
                               
                               while (rset2.next())
                           {
                            jTextField1.setText(node1.toString());
                            jTextField2.setText(rset2.getString("ang_nr1"));
                            jTextField3.setText(rset2.getString("prod_bez1"));
                            jTextField4.setText(rset2.getString("kunde_nr1"));
                            jTextField5.setText(rset2.getString("ref_nr1"));
                            jTextField6.setText(rset2.getString("proj_nr1"));
                            jTextField7.setText(rset2.getString("basisprojekt1"));
                            /*jTextField8.setText(rset2.getString("ver_nr1"));
                                    jTextField9.setText(rset2.getString("gesc_jahr1"));
                                    jTextField10.setText(rset2.getString("ver_besh1"));
                                    jTextField11.setText(rset2.getString("curr1"));
                                    jTextField12.setText(rset2.getString("ges_kalk1"));
                                    jTextField13.setText(rset2.getString("kalk_ele1"));
                                    jTextField14.setText(rset2.getString("kalk_mek1"));*/
                           }
                              
                                          conn2.close();
                                          stmt2.close();
                                          rset2.close();
                                          
                                     
                                  }
                 
                 
                 
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                                  
                           
                             Connection conn3;
                               Statement stmt3;
                               ResultSet rset3 = null;                                                 
                               String verNo = node1.toString();
                               
                               
                   try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn3 = DriverManager.getConnection ("jdbc:oracle:thin:@fe64700:1521:tanska","anska_test", "praktikant2004");
           
                              stmt3 = conn3.createStatement ();
                                  rset3 = stmt3.executeQuery("select * from regdet WHERE ver_nr1 = '" + verNo + "'");
                                              
                               while (rset3.next())
                           {
                            jTextField8.setText(rset3.getString("ver_nr1"));
                                    jTextField9.setText(rset3.getString("gesc_jahr1"));
                                    jTextField10.setText(rset3.getString("ver_besh1"));
                                    jTextField11.setText(rset3.getString("curr1"));
                                    jTextField12.setText(rset3.getString("ges_kalk1"));
                                    jTextField13.setText(rset3.getString("kalk_ele1"));
                                    jTextField14.setText(rset3.getString("kalk_mek1"));
                               }
                              
                                          conn3.close();
                                          stmt3.close();
                                          rset3.close();                                           
                                    
                                  }
                 
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                      
                                                                                                 
                                                                                        
                                                                                        }
               
                                                                                                    }
       
                                                          );      
0
 

Author Comment

by:vihar123
ID: 11961326
oops i pressed enter before writing, well to add to the above code, i did use setText for the B node, but I dont know how to clear these values when again A node is selected.
0
 
LVL 92

Expert Comment

by:objects
ID: 11961327
you really shouldn't be making db calls from the gui thread, that'll result in porr gui responsiveness.
0
 
LVL 92

Expert Comment

by:objects
ID: 11961331
Clear them with setText("");
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961334
This is what I do in such cases.
Let's call the panel where the text fields are displayed "rightPanel".
On every selection change in the tree the first thing I do is:

   rightPanel.removeAll();  // Remove all the previous stuff
   try {

          // Some logic to decide which JPanel to show according to the selected node
          if ( ) {
             PropertiesPanel1 propsPnl = new PropertiesPanel1(); // showing the information for your node. Probably pass the selected node as parameter
             rightPanel.add(propsPnl);
          } else if ( ) {
             PropertiesPanel2 propsPnl = new PropertiesPanel2(); // showing the information for your node. Probably pass the selected node as parameter
             rightPanel.add(propsPnl);
          }
          // and so on if needed

   } finally {
      rightPanel.revalidate();
      rightPanel.repaint();
   }

So in fact the panel within rightPanel is totally recreated for every new selection
0
 
LVL 92

Expert Comment

by:objects
ID: 11961337
easiest would be to add a method that cleared everything, and call that first when a node is selected.
0
 
LVL 92

Expert Comment

by:objects
ID: 11961342
No need to be creating, adding and removing panels, that just creates extra unnecessary work for the application.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961354
So in your case PropertiesPanel1 contains the text fields 1 to 7 while PropertiesPanel2 contains the text fields 8 to 14.
Switching between tree nodes = removing the previously created PropertiesPanel1 and creating a new PropertiesPanel2 or vice versa.
No clearing problems.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11961365
public void valueChanged(TreeSelectionEvent evt) {
   clearForm();
   // now set the required fields
   ....
}

private void clearForm()
{
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
...
}

                                                                       
0
 
LVL 92

Expert Comment

by:objects
ID: 11961372
> No clearing problems.

Just a proliferation of unnecessary objects for the gc to clean up :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961376
>> No need to be creating, adding and removing panels, that just creates extra unnecessary work for the application.

vihar123,

   That's just his opinion. I use it and I'm quite comfortable with it.
   Moreover, why showing 7 text fields when they don't have to display anything useful for the selected tree node?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961380
>  but I dont know how to clear these values when again A node is selected.

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21116390.html#11961272
0
 
LVL 92

Expert Comment

by:objects
ID: 11961386
> Moreover, why showing 7 text fields when they don't have to display anything useful for the selected tree node?


Then you could also use seperate forms as I mentioned earlier.
0
 

Author Comment

by:vihar123
ID: 11961392
>>>>Try to set the text on the textfields that are filled up from B node by using

setText("");


where exactly i shld write this, if i write this inside try block, itll give me exceptions, and outside i cant use these variables.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961399
> where exactly i shld write this, if i write this inside try block, itll give me exceptions, and outside i cant use these variables.

In the block of code that it is called when the node A is selected. Then the setText(""); should be called on the text fields of the node B.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961409
If this is node A, then do:

while (rset2.next())
                         {
                          jTextField1.setText(node1.toString());
                          jTextField2.setText(rset2.getString("ang_nr1"));
                          jTextField3.setText(rset2.getString("prod_bez1"));
                          jTextField4.setText(rset2.getString("kunde_nr1"));
                          jTextField5.setText(rset2.getString("ref_nr1"));
                          jTextField6.setText(rset2.getString("proj_nr1"));
                          jTextField7.setText(rset2.getString("basisprojekt1"));

                          // Node B text fields.
                          jTextField8.setText(rset2.getString(""));
                              jTextField9.setText(rset2.getString(""));
                              jTextField10.setText(rset2.getString(""));
                              jTextField11.setText(rset2.getString(""));
                              jTextField12.setText(rset2.getString(""));
                              jTextField13.setText(rset2.getString(""));
                              jTextField14.setText(rset2.getString(""));
                         }
0
 
LVL 92

Expert Comment

by:objects
ID: 11961411
> where exactly i shld write this,

I'd put it in a seperate method that cleared all the fields as I outline above.
You can then call it whenever you need to clear the form.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961414
Sorry, it should be:

 // Node B text fields.
                          jTextField8.setText("");
                              jTextField9.setText("");
                              jTextField10.setText("");
                              jTextField11.setText("");
                              jTextField12.setText("");
                              jTextField13.setText("");
                              jTextField14.setText("");
0
 
LVL 92

Expert Comment

by:objects
ID: 11961415
public void valueChanged(TreeSelectionEvent evt) {
   clearForm();
   // now set the required fields as you already do
   ....
}

private void clearForm()
{
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
...
}
0
 

Author Comment

by:vihar123
ID: 11961448
Hey,

thanks objects, its working

and ofcourse thanks to all for ur informative contributions.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961454
>> and ofcourse thanks to all for ur informative contributions.
You're welcome
0
 
LVL 92

Expert Comment

by:objects
ID: 11961466
> thanks objects, its working

good stuff, let us know if you need further assistance :)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961479
> and ofcourse thanks to all for ur informative contributions.

np :)
0
 

Author Comment

by:vihar123
ID: 11961529
>> np :)

sorry again, i get excited when its working and usually forget the option of split points, well i think its a big process again to put it into community and reallocate points.
so this time please excuse me and i promise ill not repeat this again.

thanks for your understanding.  :-))))
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961564
> i think its a big process again to put it into community and reallocate points.
Not at all.

Just FYI:
1) You simply post a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body:
           Please reopen this question:
           http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21116390.html

2) A moderator reopens it (within hours or at most two days) and you get a mail
3) You simply re-accept with points split

>> so this time please excuse me and i promise ill not repeat this again.
That's OK ;°)
0
 
LVL 35

Expert Comment

by:girionis
ID: 11961575
>so this time please excuse me and i promise ill not repeat this again.
>
>thanks for your understanding.  :-))))

Don't worry, as long as you are happy with the solution :)
0
 

Author Comment

by:vihar123
ID: 11961628
>>>Don't worry, as long as you are happy with the solution :)

thanks a lot.
------
a small question again, im getting sql exception (SQLException: ORA-01722: invalid number) when im clicking the C node.
what does it mean?


0
 
LVL 35

Expert Comment

by:girionis
ID: 11961636
ORA-01722 invalid number

Cause: The attempted conversion of a character string to a number failed because the character string was not a valid numeric literal. Only numeric fields or character fields containing numeric data may be used in arithmetic functions or expressions. Only numeric fields may be added to or subtracted from dates.

Action: Check the character strings in the function or expression. Check that they contain only numbers, a sign, a decimal point, and the character "E" or "e" and retry the operation.

0
 
LVL 37

Expert Comment

by:zzynx
ID: 11961639
0
 
LVL 92

Expert Comment

by:objects
ID: 11961642
0
 
LVL 92

Expert Comment

by:objects
ID: 11961645
snap
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

689 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