Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7729001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:51:33+00:00 2026-06-01T05:51:33+00:00

I have a main class(which is basically a netbeans form;drag and drop) from where

  • 0

I have a main class(which is basically a netbeans form;drag and drop) from where my application starts and another class(call it class 2) where my functions resides.I first call a function in class2 from my main method and that method has a while loop which increments a counter.Depending on that counter i call a function of my main class and try to display the counter in a textfield and also st the progressbar in intermediate but it is not working though it shows the print statements correctly(the counter).

Some code i am adding it is giving me problem as it is neither updating the progressbar nor updating the textfield.Kindly help me why is this happening

i have edited the code but still it displays nothing 🙁

public class NewClass 
{
    public static int counter = 0;
    public  NewJFrame p = new NewJFrame();
    public void packet() 
    {
        try 
        { 
            while (true) 
            {
                //some code ,right now which i have omitted
                counter = counter + 1;
                counter2(counter);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void counter2(int counter) 
    {
        counter3();
    }

    public void counter3() 
    {

        p.progress(counter);
    }
}

Now here is the main method from where i call the functions present in other class()code of that is given above)

public class NewJFrame extends javax.swing.JFrame 
{

    /** Creates new form NewJFrame */
    public NewJFrame() 
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")   
    public void progress(int y)
    {
        jProgressBar1.setIndeterminate(true);
        jTextField1.setText(y+"packets processed");
        System.out.println(y);
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) 
    {    
        NewClass m=new NewClass();
        m.packet();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {         
        try 
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        java.awt.EventQueue.invokeLater(new Runnable() 
        {
            public void run() 
            {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:51:34+00:00Added an answer on June 1, 2026 at 5:51 am

    Here a small example with SwingWorker will help you update your JTextField on the run :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class NewJFrame extends javax.swing.JFrame 
    {
    
        /** Creates new form NewJFrame */
        public NewJFrame() 
        {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setLocationByPlatform(true);
    
            JPanel contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
    
            jTextField1 = new JTextField(10);
            contentPane.add(jTextField1, BorderLayout.PAGE_START);
    
            jProgressBar1 = new JProgressBar(0, 100);       
            contentPane.add(jProgressBar1, BorderLayout.CENTER);
    
            jButton1 = new JButton("START");
            jButton1.addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent me)
                {
                    jProgressBar1.setIndeterminate(true);
                    jButton1MouseClicked(me);
                }
            });
            contentPane.add(jButton1, BorderLayout.PAGE_END);
    
            setContentPane(contentPane);
            pack();
            setVisible(true);
        }
    
        @SuppressWarnings("unchecked")   
        public void progress(final int y)
        {
            System.out.println("progress Method is working.");
            /*
             * This thing needs to be done on Event
             * Dispatcher Thread.
             */
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    jTextField1.setText(y+"packets processed");
                    System.out.println(y);
                }           
            });         
        }
    
        private void jButton1MouseClicked(java.awt.event.MouseEvent evt) 
        {    
            NewClass m=new NewClass(this);
            m.execute();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) 
        {         
            try 
            {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
    
            java.awt.EventQueue.invokeLater(new Runnable() 
            {
                public void run() 
                {
                    new NewJFrame().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        public javax.swing.JProgressBar jProgressBar1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
    }
    
    class NewClass extends SwingWorker<Void, Void>
    {
        public static int counter = 0;
        // Added this variable to keep the instance.
        private NewJFrame p;
        private boolean flag = true;
    
        public NewClass(NewJFrame frame)
        {
            p = frame;
        }
    
        public Void doInBackground()
        {
            while(flag)
            {
                counter = counter + 1;
                counter2(counter);
            }
            return null;
        }
    
        public void done()
        {
            System.out.println("I am DONE");
        }
    
        public void counter2(int counter) 
        {
            counter3();
        }
    
        public void counter3() 
        {
            p.progress(counter);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Service class and an main Acitivity class which is supposed to
I have a main class in a program that launches another class that handles
I have a Class(call it main class) and the method in main class calls
I have a small Java test app in Netbeans where the main() class reads
I have a class called GUIMain which registers, creates, and shows a main window
I have the following Class which basically creates a list of categories (called commands,
I have got a utility class, called ErrorLog , which basically does some basic
I have a Main class and a HelloWorld class. I have created a button
I have a question about the 'Event Dispatch Thread'. I have a Main class
I have the next scenario: I define an int[][] variable in my main class.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.