Results 1 to 5 of 5
  1. #1
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default 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();
    }
    ??

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,808
    Rep Power
    19

    Default

    SwingWorker tutorial.

    Essentially you would create and execute() your worker in the actionPerformed method.

  3. #3
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default

    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!

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,808
    Rep Power
    19

    Default

    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.

  5. #5
    aadem is offline Member
    Join Date
    Mar 2011
    Posts
    22
    Rep Power
    0

    Default

    Tolls i was having a heck of a time getting the concept of SwingWorker to click so i very much so appreaciate your help! I Owe ya one
    And ill heed your advice and make sure executing the worker thread before it stops cant happen



    TY again!

Similar Threads

  1. SwingWorker
    By 3.14.TR in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-14-2011, 04:53 PM
  2. Using SwingWorker
    By viking90 in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 09:17 AM
  3. SwingWorker question
    By cotarelo in forum Threads and Synchronization
    Replies: 16
    Last Post: 03-23-2010, 11:29 AM
  4. SwingWorker Opinions
    By frejon26 in forum AWT / Swing
    Replies: 3
    Last Post: 04-13-2009, 08:41 PM
  5. swingworker
    By musiigedeo in forum AWT / Swing
    Replies: 1
    Last Post: 07-26-2007, 12:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •