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 7624827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:04:25+00:00 2026-05-31T05:04:25+00:00

Hi everyone I am stack so if anyone can assist in any way it

  • 0

Hi everyone I am stack so if anyone can assist in any way it would be great. I am using eclipse and the program is compiling and running. I have 3 classes and they are in the same package. So I want to pass the value of i in the class ThreadQuizCountdown to other class PanelQuizCountdown int the JTextField with name timeField currently i is displayed in the console I have been trying to do it but I couldn’t so if anyone can give a hand. Here is the code

/**The driver class of the program. Here is the JFrame 
 * class name RunQuizCountdown.java
 * @author Kiril Anastasov
 * @date 09/03/2012
 */

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

public class RunQuizCountdown 
{
    public static void main(String[] args) 
    {

        JFrame application = new JFrame();
        PanelQuizCountdown panel = new PanelQuizCountdown();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(200,300);
        application.setLocationByPlatform(true);
        application.setVisible(true);
    }

}



/** Here is the GUI of the program
 * class name PanelQuizCountdown.java
 * @author Kiril Anastasov
 * @date 09/03/2012
 */

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

public class PanelQuizCountdown extends JPanel implements ActionListener
{
    JTextField timeField, answerField;
    JLabel messageLabel, correctLabel, totalLabel;
    int x, y;
    int correct;
    int total;

    ThreadQuizCountdown myQuiz;

    PanelQuizCountdown()
    {
        timeField = new JTextField(5);
        myQuiz = new ThreadQuizCountdown(timeField);
        this.add(timeField);
        myQuiz.begin();


        messageLabel = new JLabel("What is the result of " + x + " * " + y);
        this.add(messageLabel);

        answerField = new JTextField(5);
        this.add(answerField);

        correctLabel = new JLabel("You gave : " + correct +  " correct answers");
        this.add(correctLabel);

        totalLabel = new JLabel("You answered: " + total + " questions");
        this.add(totalLabel);





    }


    public void actionPerformed(ActionEvent ae)
    {

    }
}

/** Here is the thread of the program
 * class name ThreadQuizCountdown.java
 * @author Kiril Anastasov
 * @date 09/03/2012
 */

import javax.swing.JTextField;

public class ThreadQuizCountdown implements Runnable
{
    JTextField  timeField;
    Thread myThread = new Thread(this);

    int i = 30;
    boolean go = true;

    ThreadQuizCountdown(JTextField theTimeField)
    {
        timeField = theTimeField;
    }

    public void run()
    {


        while(go)
        {           
            System.out.println(i);      

            try 
            { 
                myThread.sleep(1000);          
            } 
            catch (InterruptedException ie) 
            {
                 System.out.println("thread exception");
            }

            timeField = new JTextField(26);

            if(i == 0 )
            {
                go = false;
            }
            i--;
        }

    }

    public void begin()
    {
        myThread.start();
    }

    public void finish()
    {
        myThread.stop();
    }
}
  • 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-05-31T05:04:27+00:00Added an answer on May 31, 2026 at 5:04 am

    Use delegation, add to the begin() method parameters for your delegate class that conforms interface, like

    interface DelegationInterface {
       void countdownTick(int i);
    }
    

    in ThreadQuizCountdown:
    add private field and modify begin method:

    private DelegationInterface delegate;
    
    public void begin(DelegationInterface delegate) {
       this.delegate = delegate;
       myThread.start();
    }
    

    next, modify run(): (notice that we call countdown in critical section, in this case it doesn’t matter, but if you will have many timers, it will help to avoid problems)

    public void run() {
    ....
      myThread.sleep(1000); 
      if (delegate != null) {
          synchronized(delegate) {
              delegate.countdownTick(i);
          }
      }
    ....
    }
    

    And finally, add implementation of interface to panel:

    public class PanelQuizCountdown extends JPanel implements ActionListener, DelegationInterface {
        ....
        public void countdownTick(int i) {
            // place i to to timeField
        }
        ....
    }
    

    That’s it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi everyone I am stack so if anyone can give me a help it
How can i realize an UiTableWiev like this? https://i.stack.imgur.com/8QVb4.jpg I appreciate everyone who gives
everyone. This is a piece of code from using sqlite in android tutorial: return
everyone! I have sprite moving by action, what have health bar (progress bar). When
everyone seems interested in building IPhone apps today. Do you have to have an
Greetings everyone: I would love to get some information from a huge collection of
Hello everyone this is my first post on stack overflow.com I am trying to
I've seen this all over the place on stack overflow, but everyone else's solution
I have looked through many tutorials, as well as other question here on stack
Good day everyone, I have an HTACCESS file that contains the following: Options +FollowSymlinks

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.