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

  • Home
  • SEARCH
  • 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 3996902
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:20:08+00:00 2026-05-20T07:20:08+00:00

I’m new in java, so i guess it’s a simple problem. In my event

  • 0

I’m new in java, so i guess it’s a simple problem. In my event listener i have a method which declares variables and i’m trying to use those variables in thread which i use in same event. Although it does not work, here is the code:

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

public class GUI extends Frame implements WindowListener,ActionListener {
cpWindow window1 = new cpWindow();
cmWindow window2 = new cmWindow();
SolarSystem system = new SolarSystem(300,300);
Planet Planetarium;
/**
* Frame for general window.
*/
public void createFrame0(){
JFrame f0 = new JFrame("Choose what you want to do");
f0.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f0.addWindowListener(this);
f0.setLayout(new GridLayout(3,1));
JButton cP = new JButton("Create a planet");
JButton cM = new JButton("Create a moon");
JButton Delete = new JButton("Delete a planet or moon");
f0.add(cP);
f0.add(cM);
f0.add(Delete);
cP.addActionListener(this);
cM.addActionListener(this);
Delete.addActionListener(this);
cP.setActionCommand("1");
cM.setActionCommand("2");
Delete.setActionCommand("3");
f0.pack();
f0.setVisible(true);
}
 /**
  * Frame for planet adding window.
  */
class cpWindow implements ActionListener,WindowListener{
 JLabel name1  = new JLabel("Name");
 JLabel color1  = new JLabel("Color");
 JLabel diam1  = new JLabel("Diameter");
 JLabel dist1  = new JLabel("Distance");
 JLabel speed1  = new JLabel("Speed");
 JTextField name2 = new JTextField();
 JTextField color2  = new JTextField();
 JTextField diam2  = new JTextField();
 JTextField dist2  = new JTextField();
 JTextField speed2  = new JTextField();
double distance;
int Speed;
double diameter;

public void createFrame1() {
    JFrame f1 = new JFrame("Add planet");
    f1.addWindowListener(this);
    f1.setLayout(new GridLayout(6,2,5,5));
    JButton mygt = new JButton("Create planet");
    mygt.addActionListener(this);
      name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText("60");spe ed2.setText("2");
    f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1);
    f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2);
    f1.add(mygt);   
    f1.pack();
    f1.setVisible(true);
} 
public void createVariables(){
    try {
          distance = Double.parseDouble(dist2.getText());
          Speed = Integer.parseInt(speed2.getText());
          diameter = Double.parseDouble(diam2.getText());
        }
        catch(NumberFormatException i) {
        }
        Main.diametras = diameter;
        Main.distancija = distance;
        Main.greitis = Speed;
        Main.vardas = name2.getText();
        Main.spalva = color2.getText();
        Planetarium = new             Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis);
    }

public void actionPerformed(ActionEvent e) {
    createVariables();
    new NewThread().run();
}


public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
/**
 * Frame for moon adding window
 */
class cmWindow implements ActionListener,WindowListener{
 JLabel name1  = new JLabel("Name");
 JLabel color1  = new JLabel("Color");
 JLabel diam1  = new JLabel("Diameter");
 JLabel dist1  = new JLabel("Distance");
 JLabel speed1  = new JLabel("Speed");
 JLabel corDist1  = new JLabel("Distance from centre of rotation");
 JLabel corSpeed1  = new JLabel("Speed which moon centres");
 JTextField name2 = new JTextField();
 JTextField color2  = new JTextField();
 JTextField diam2  = new JTextField();
 JTextField dist2  = new JTextField();
 JTextField speed2  = new JTextField();
 JTextField corDist2  = new JTextField();
 JTextField corSpeed2  = new JTextField();

double distance;
int Speed;
double diameter;
int cordistance;
int corspeed;

public void createFrame1() {
    JFrame f1 = new JFrame("Add moon");
    f1.addWindowListener(this);
    f1.setLayout(new GridLayout(8,2,5,5));
    JButton mygt = new JButton("Create moon");
    mygt.addActionListener(this);
        name2.setText("Belekoks");color2.setText("BLUE");diam2.setText("15");dist2.setText("60");sp  eed2.setText("2");corDist2.setText("15");corSpeed2.setText("3");
    f1.add(name1);f1.add(name2);f1.add(color1);f1.add(color2);f1.add(diam1);
    f1.add(diam2);f1.add(dist1);f1.add(dist2);f1.add(speed1);f1.add(speed2);
    f1.add(corDist1);f1.add(corDist2);f1.add(corSpeed1);f1.add(corSpeed2);
    f1.add(mygt);   
    f1.pack();
    f1.setVisible(true);
}
public void createVariables(){
    try {
          distance = Double.parseDouble(dist2.getText());
          Speed = Integer.parseInt(speed2.getText());
          diameter = Double.parseDouble(diam2.getText());
          cordistance = Integer.parseInt(corDist2.getText());
          corspeed = Integer.parseInt(corSpeed2.getText());
        }
        catch(NumberFormatException i) {}
    /*  Main.diametras = diameter;
        Main.distancija = distance;
        Main.greitis = Speed;
        Main.vardas = name2.getText();
        Main.spalva = color2.getText();
*/      Main.centGrt = corspeed;
        Main.centAts = cordistance;
}
public void actionPerformed(ActionEvent e) {
    createVariables();
}   
public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
/**
 * Deleting window
 */
class DelWindow implements ActionListener,WindowListener{



public void actionPerformed(ActionEvent e) {

}   
public void windowClosing(WindowEvent e) {
    dispose();
    System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}
}
////////////////////////////////////////////////////////
public GUI() {
createFrame0();
}


public void actionPerformed(ActionEvent e) {
if ("1".equals(e.getActionCommand())) {this.window1.createFrame1();}
else if ("2".equals(e.getActionCommand())) {this.window2.createFrame1();} 
//  else if ("3".equals(e.getActionCommand())) {this.window2.createFrame1();}
}

public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowClosed(WindowEvent e) {}
public void windowActivated(WindowEvent arg0) {}
public void windowDeactivated(WindowEvent arg0) {}
public void windowDeiconified(WindowEvent arg0) {}
public void windowIconified(WindowEvent arg0) {}
public void windowOpened(WindowEvent arg0) {}

//Create a new thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
  // Create a new, second thread
  t = new Thread(this);
  t.start(); // Start the thread
}
// This is the entry point for the second thread.
public void run() {
    Planet planet = new    Planet(Main.vardas,Main.distancija,Main.diametras,Main.spalva,Main.greitis);
    try {
        for(int i = 0; i >= 0; i++) {
            planet.move();
            planet.drawOn(system);
            system.finishedDrawing();
        }
    } catch (Exception e) {}
} 
}

}

any ideas?

  • 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-20T07:20:09+00:00Added an answer on May 20, 2026 at 7:20 am

    One needs to actually start the Thread, just instantiating a Thread object is not enough.

    public void actionPerformed(ActionEvent e) {
        createVariables();
        new NewThread().start();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.