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

The Archive Base Latest Questions

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

I have those to classes: the main and GUI. In GUI there is actionListener

  • 0

I have those to classes: the main and GUI. In GUI there is actionListener which gathers information from user interface panel. How can send these variables to driver class, to execute everything there? I need to add them to infinite loop to draw a moving object, drawing methods are in other class. GUI has extended frame

here are the classes:

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

public class GUI extends Frame implements WindowListener,ActionListener {
 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();

public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
double distance;
int Speed;
double diameter;

public void actionPerformed(ActionEvent e) {
createVariables();
}
public void createVariables(){

try {
      distance = Double.parseDouble(dist2.getText());
      Speed = Integer.parseInt(speed2.getText());
      diameter = Double.parseDouble(diam2.getText());
    }
    catch(NumberFormatException i) {
    }   
    Planet cP = new        Planet(name2.getText(),distance,diameter,color2.getText(),Speed  );
    Main plnt = new Main(cP);
}

public void createFrame1(){
addWindowListener(this);
setLayout(new GridLayout(6,6,5,5));
JButton mygt = new JButton("Add planet");
mygt.addActionListener(this);
name2.setText("belekoks");color2.setText("RED");diam2.setText("30");dist2.setText(" 60");speed2.setText("2");
add(name1);add(name2);add(color1);add(color2);add(diam1);
add(diam2);add(dist1);add(dist2);add(speed1);add(speed2);
add(mygt);

}
public GUI(String title){
super(title);
createFrame1();
}
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) {}
}

and Main:

import java.awt.event.ActionEvent;
import java.util.Random;
public class Main{
/**
 * @param args
 */

public Main(Planet name){
    super(name);
}

public static void main(String[] args) {
SolarSystem system = new SolarSystem(300, 300);
GUI p = new GUI("New Planet");
p.pack();
p.setVisible(true);
Sun sun = new Sun("Sun", 0, 90, "YELLOW", 0);
Planet venus = new Planet("Venus",70,15,"ORANGE",2);
Planet earth = new Planet("Earth",100,20,"BLUE",1);
Moon moon = new Moon("Moon",earth.dist,5,"GRAY", 1, 30, 3);
Moon[] moons = new Moon[100];
moons[0] = moon;
Planet[] planets = new Planet[100];
planets[0] = earth;
planets[1] = venus;
Random rG = new Random();
Asteroid[] asteroids = new Asteroid[100];
for(int i=0; i<100;i++){
    Asteroid Asteroids = new Asteroid("Asteroid",     rG.nextDouble()+rG.nextInt(10)+45,rG.nextDouble()+rG.nextInt(10),"DARK_GRAY",rG.nextInt(360        ), 1);
    asteroids[i]=Asteroids;
}   

/*LinkedList list = new LinkedList();
for (int i=0;i<1;i++){
    list.add(moons[i]);
}
for (int i=0;i<2;i++){
    list.add(planets[i]);
}
for (int i=0;i<100;i++){
    list.add(asteroids[i]);
}
for (int i=0;i<100;i++){
    list.add(sun);
}

for(int y=0;y<2;y++){
    planets[2].move();
    planets[2].drawOn(system);
    system.finishedDrawing();
}

    for (int i=0; i>=0; i++){
    sun.drawOn(system);

    for(int y=0;y<3;y++){
        planets[y].move();
        planets[y].drawOn(system);
    }

    for (int y=0; y<100; y++){
        asteroids[y].move();
        asteroids[y].drawOn(system);
    }
    for (int y=0; y<1; y++){
        moons[y].move();
        moons[y].drawOn(system);
    }

    system.finishedDrawing();
    }
*/
}
}

i don’t expect you to look through whole code, but the main thing is that i get variables in actionListener in GUI and i want to pass them to main in order to create an object which I could draw later.

planet class creates an object:

public class Planet extends CosmicEntity {
public Planet(String name, double distance, double diameter, String col, int speed)    {
    super(name,distance,diameter,col,speed, 0);
}
}

and CosmicEntity class which holds all the variables and methods to draw a planet from passed variables (super(name,distance,diameter,col,speed, 0)).

  • 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-20T05:08:54+00:00Added an answer on May 20, 2026 at 5:08 am

    It’s hard to know exactly what you need based on the information provided, but I think that you don’t want to use an infinite loop, not for your GUI. Most simple animations can be easily driven by a Swing Timer.

    As for your other problem, a the general rule is that objects can call methods of other objects if they have a valid reference to the other object. How to do this for your project will depend on your code. Often we pass references of one object to another though as a method or constructor parameter. For instance,

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class FooMain {
       public static void main(String[] args) {
          FooNonGui nonGuiReference = new FooNonGui();
          FooGui fooGui = new FooGui(nonGuiReference);
    
          fooGui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          fooGui.pack();
          fooGui.setLocationRelativeTo(null);
          fooGui.setVisible(true);
    
       }
    }
    
    class FooGui extends JFrame implements ActionListener {
       private FooNonGui nonGuiVariable;
       private int counter = 0;
    
       public FooGui(FooNonGui nonGuiParameter) {
          super("GUI");
          this.nonGuiVariable = nonGuiParameter;
          JButton button = new JButton("Button");
          button.addActionListener(this); // I hate doing this, but for brevity's sake...
          add(button);
       }
    
       public void actionPerformed(ActionEvent e) {
          nonGuiVariable.nonGuiMethod(counter);
          counter++;
       }
    }
    
    class FooNonGui {
       public void nonGuiMethod(int counter) {
          System.out.print("In non-GUI method.  ");
          System.out.println("counter is " + counter);
       }
    }
    

    I suggest that you give us more information so that we can be more knowledgeable about your problem and give you better help. This link may help: Smart Questions

    Edit A: What are you adding your planets to? The SolarSystem object? Whichever class it is, likely will have or should have a public addPLanet(Planet planet) method. If SolarSystem, then you’ll want to pass a reference to this object into your GUI class similar to what I do above.

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

Sidebar

Related Questions

I come from classes object orientation languages and recently I have been learning those
I have a few classes, such as those that outline database table structure or
I have a set of classes which I didn't write, and they're read only.
If I have 2 header files, Test1.h and Test2.h, in which I define classes
So I'm trying to figure out how can I have 3 classes call one
I have a program complete with a number of classes which create complex objects,
i have a GUI class and it uses lots of other classes by creating
My Goal I would like to have a main processing thread (non GUI), and
I have 2 main div's. Group1 and group2. Those group divs contain other 2
I have those two classes ///////////BASE CLASS class Base{ public: Base(int = 0); ~Base();

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.