Home arrow static arrow Java Programming [Archive] - What is the best way to get another object's method to update my GUI?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - What is the best way to get another object's method to update my GUI?
This topic has 6 replies on 1 page.

Posts:346
Registered: 1/15/04
What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 9:22 AM



 
[code]
package stuff;

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Test extends JFrame{

private static JButton ProcessButton = new JButton();
private static JLabel jLabel2 = new JLabel();

public static void main( String args []){
Test f = new Test();
f.setSize(500,500);
Container c = f.getContentPane();
ProcessButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e) {
jLabel2.setText("Connecting to DB");
//Connection connection = Tools.setUpConnectionToDB(url,userName,pwd);
}
});
c.add(ProcessButton, BorderLayout.NORTH);
jLabel2.setText("My Label");
c.add(jLabel2, BorderLayout.SOUTH);
f.setVisible(true);
}
}

{\code]

The method setUpConnectionToDB can take 1 - 10 mins to complete. By this time a user will prob have quit my app thinking
it's crashed because it doesn't update the GUI with a progress status. :(
What is the best way to get this method to update the GUI of the app which calls it?
Something like Connection connection = Tools.setUpConnectionToDB(url,userName,pwd, this);
ie this being a reference to the GUI's JFrame is what I'm trying to use?
 

Posts:24,036
Registered: 2/3/03
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 9:29 AM (reply 1 of 6)



 
[url=http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html]How to Use Progress Bars[/url]
 

Posts:346
Registered: 1/15/04
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 9:44 AM (reply 2 of 6)



 
A handy class to know about but not really what I'm after.
I need the method call
Tools.setUpConnectionToDB(url,userName,pwd);
to be able to update a component ( The JLabel ) on the GUI

[code]
Connection connection = Tools.setUpConnectionToDB(url,userName,pwd, this);
[\code]

method defn:
[code]
public static Connection setUpConnectionToDB( String url, String user, String pwd, JFrame f ){
//Why doesn't this code below modify the GUI on the calling App?
f.jLabel2.setText("Setting UP DB Connection");
f.repaint();

Connection c = null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection(url,user,pwd);
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null , "Error loading DB driver");
System.exit(0);
}
catch(Exception e){
JOptionPane.showMessageDialog(null , "Error connecting to DB, check config file");
System.exit(0);
}
return c;
}[\code]
 

Posts:13,769
Registered: 00-11-29
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 9:57 AM (reply 3 of 6)



 
A handy class to know about but not really what I'm
after.
I need the method call
Tools.setUpConnectionToDB(url,userName,pwd);
to be able to update a component ( The JLabel ) on the
GUI

That's really a poor design. If it's compiling and you are sure you are calling the right methods on the right objects, the problem is most likely that this process is running blocking the Swing threads from updating the GUI. if you move the window or partially cover it, does it go blank?
 

Posts:346
Registered: 1/15/04
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 10:06 AM (reply 4 of 6)



 
yep, until that method call is finished !
 

Posts:13,769
Registered: 00-11-29
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 10:11 AM (reply 5 of 6)



 
There's something I hear about a lot called SwingWorker. You should do a search on that. The quick fix is to spawn a new thread and call this method in that thread. I take back what I said before, it's not such a bad design.
 

Posts:13,769
Registered: 00-11-29
Re: What is the best way to get another object's method to update my GUI?  
Jun 16, 2004 10:15 AM (reply 6 of 6)



 
new Thread(new Runnable(){    public void run() {        Connection setUpConnectionToDB( blah, blah, blah, frame);    }}).start();
 
This topic has 6 replies on 1 page.