Solved

refreshing display from a tree control

Posted on 2004-09-02
34
217 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
  • 14
  • 8
  • 6
  • +1
34 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
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 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 35

Expert Comment

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

setText("");
0
 

Author Comment

by:vihar123
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Clear them with setText("");
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> No clearing problems.

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

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
>  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
Comment Utility
> 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
Comment Utility
>>>>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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Expert Comment

by:girionis
Comment Utility
> 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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hey,

thanks objects, its working

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

Expert Comment

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

Expert Comment

by:objects
Comment Utility
> thanks objects, its working

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

Expert Comment

by:girionis
Comment Utility
> and ofcourse thanks to all for ur informative contributions.

np :)
0
 

Author Comment

by:vihar123
Comment Utility
>> 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
Comment Utility
> 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
Comment Utility
>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
Comment Utility
>>>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
Comment Utility
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
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
snap
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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

14 Experts available now in Live!

Get 1:1 Help Now