Results 1 to 5 of 5
Thread: SwingWorker Explination
- 03-22-2011, 03:36 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 22
- Rep Power
- 0
SwingWorker Explination
I have a question reguarding the SwingWorker object. From what i understand any action listener i have in my GUI which takes any fair amount of time (more than a second or 2) to complete, you should have a SwingWorker manage it for you so the GUI doesnt just freeze
during the work.
So this is the outline for a normal Swing application i would create, containing 1 textfield and 1 button to make that text field say "You pushed the button!"
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class myswinggui { private static void createAndShowGUI() { JFrame newFrame = new JFrame("Swing example"); Container contentpane = newFrame.getContentPane(); newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int windowWidth = 300; int windowHeight = 250; JTextField displayField = new JTextField("",10); JButton pushme = new JButton("Push Me!"); Point centP = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); JPanel topPanel = new JPanel(); JPanel bottomPanel = new JPanel(); topPanel.add(displayField); bottomPanel.add(pushme); contentpane.add(topPanel, BorderLayout.PAGE_START); contentpane.add(bottomPanel, BorderLayout.PAGE_END); pushme.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { displayField.setText("You have pushed the button!"); } }); newFrame.setBounds(centP.x - windowWidth / 2, centP.y - windowHeight / 2, windowWidth, windowHeight) newFrame.setVisible(true); } public static void main(String[] argv) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }
I Understand now that im calling my createAndShowGUI() method to construct the interface from my event dispatching thread. What if i had different button which did something else like open an SSH connection and throw a command, than waits for output. In order to do this i know you have to use a SwingWorker, but how you impliment the SwingWorker is what im confused about. Do i create a new SwingWorker Object like this
Java Code:SwingWorker myworker = new SwingWorker(); public void doInBackground() { //My Code for SSH Task here } public void done() { } }
And than put it in my EDT like this:
public void run() {
createAndShowGUI();
myworker.execute();
}
??
- 03-22-2011, 04:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
SwingWorker tutorial.
Essentially you would create and execute() your worker in the actionPerformed method.
- 03-22-2011, 04:31 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 22
- Rep Power
- 0
OHHH so since my action listeners are created in my createAndShowGUI() method, i would simply create the swing worker as i said, create a new action listener for the button doing the long work, and call my swingworker FROM that action listener .
for example if there was a second button
JButton sshbutton = new JButton("Click to SSH")
contentpane.add(JButton.BorderLayout.CENTER);
sshbutton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myworker.execute();
}
});
than when my EDT executes createAndShowGUI() that should cover everything.
Thank you so much it makes sense now and i will check out the guide you posted!
- 03-22-2011, 04:42 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
That's pretty much it, though I'm not sure what would happen if you executed the same worker object twice. That is execute it again when it hasn't finished from the previous run, which could happen in your example.
- 03-22-2011, 05:34 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
SwingWorker
By 3.14.TR in forum Threads and SynchronizationReplies: 3Last Post: 03-14-2011, 04:53 PM -
Using SwingWorker
By viking90 in forum New To JavaReplies: 1Last Post: 04-24-2010, 09:17 AM -
SwingWorker question
By cotarelo in forum Threads and SynchronizationReplies: 16Last Post: 03-23-2010, 11:29 AM -
SwingWorker Opinions
By frejon26 in forum AWT / SwingReplies: 3Last Post: 04-13-2009, 08:41 PM -
swingworker
By musiigedeo in forum AWT / SwingReplies: 1Last Post: 07-26-2007, 12:59 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks