Results 1 to 6 of 6
  1. #1
    rselvan is offline Member
    Join Date
    Dec 2012
    Location
    chennai
    Posts
    3
    Rep Power
    0

    Default NetBeans JFrame Form with Loop

    Hi,
    I am trying to create an application in java with jframe form and i am also running certain code in for loop. Here is the trick. I want to stop the operation in the middle of for loop by click a button and start it again by click another button if we want.

    My problem i can't do anything while the for loop is running.
    I got stucked here.
    Can sombody helps me?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: NetBeans JFrame Form with Loop

    Is the for loop executing on the JVM's EDT? If so, the GUI will be unresponsive until the loop exits. If the loop can be moved to a background thread that would free up the GUI so it can respond to button clicks.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: NetBeans JFrame Form with Loop

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    rselvan is offline Member
    Join Date
    Dec 2012
    Location
    chennai
    Posts
    3
    Rep Power
    0

    Default Re: NetBeans JFrame Form with Loop

    Thanks for the reply

    Its working fine but now i got a small issue please find out that my label box value is updated but not showing


    package selvan;

    import java.io.IOException;
    import javax.swing.SwingWorker;

    public class Home extends javax.swing.JFrame {
    private static FlipTask flipTask;
    public Home() {
    initComponents();
    }

    private static class FlipPair {
    private final long heads, total;
    FlipPair(long heads, long total) {
    this.heads = heads;
    this.total = total;
    }
    }

    private class FlipTask extends SwingWorker<Void, FlipPair> {

    @Override
    protected Void doInBackground() throws IOException {
    lbl_status.setText("hdhdfhdfhf");
    System.out.println(lbl_status.getText());
    int total = 500;
    for(int i=0;i<=total;i++){
    myprocess(i,total);
    }
    return null;
    }
    }

    public void myprocess(long head,long total){
    if((head) == total){
    lbl_status.setText("completed");
    }
    else{
    lbl_status.setText(head + "Out of " + total);
    }
    }

    private void startActionPerformed(java.awt.event.ActionEvent evt) {
    (flipTask = new Home().new FlipTask()).execute();
    }

    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new Home().setVisible(true);
    }
    });

    }

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: NetBeans JFrame Form with Loop

    Did you go through the tutorial linked by gimbal2? Swing methods shouldn't be invoked on a background thread ... and doInBackground does what its name suggests.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: NetBeans JFrame Form with Loop

    In case the tutorial is unclear (which I would understand), don't fret to Google for more.

    Example of a simple "java swingworker example" google:

    animation - How do I use SwingWorker in Java? - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Replies: 3
    Last Post: 10-17-2012, 09:32 AM
  2. Netbeans jpanel form add jframe form
    By modo91 in forum NetBeans
    Replies: 6
    Last Post: 09-10-2012, 07:08 AM
  3. Get more components from JFrame Form.
    By ZeCute in forum AWT / Swing
    Replies: 0
    Last Post: 05-09-2011, 10:46 AM
  4. opening a Jframe form in a top of a new form
    By Nubkadiya in forum New To Java
    Replies: 7
    Last Post: 06-01-2010, 08:21 PM
  5. setting background color of JFrame form with NetBeans 6.1
    By onefootswill in forum New To Java
    Replies: 4
    Last Post: 08-12-2008, 07:02 AM

Tags for this Thread

Posting Permissions

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