Results 1 to 6 of 6
  1. #1
    lttlLux is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Default How to call a function from another class when I'm in te Graphical Environment

    Hello, I haven't been able to find the same problem in the forum, so I'm sorry if somebody has asked this before.
    I have the class Registro where there are the functions getTemperatura() and getDistancia(), then I have the class Cosmico:

    public class Cosmico{
    protected String nombre;
    protected Registro valores;

    public Cosmico(float temperatura, float distancia, String nombre){
    valores=new Registro(temperatura, distancia);
    this.nombre=nombre;
    }

    Cosmico is the father class of Satelite and my problem is when I'm in the Graphical Enviroment trying to call getTemperatura() from a Satelite object.

    private void BuscarSateliteActionPerformed(java.awt.event.Actio nEvent evt) {
    // TODO add your handling code here:
    String s=SateliteBuscado.getText();
    Object aux;
    Satelite auxs;
    aux=ss.getPorNombre(cuerpos, s);
    Registro rg=(Registro)aux;
    if (aux==null){
    ErrorLabelObDS.setText("Satelite no encontrado");
    }
    else if(aux instanceof Satelite){
    auxs=(Satelite)aux;

    muestraTempObDS.setText(""+auxs.getTemperatura());
    muestraDistanciaObDS.setText(""+auxs.getDistancia( ));

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: How to call a function from another class when I'm in te Graphical Environment

    Not certain what you mean by a graphics environment. But if you mean in a method like

    Java Code:
    public void paintComponent(Graphics g)
    Then you can call a static method directly or you need
    to create or have an instance of the desired class to call
    the instance method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    lttlLux is offline Member
    Join Date
    May 2016
    Posts
    2
    Rep Power
    0

    Default Re: How to call a function from another class when I'm in te Graphical Environment

    I'm talking about the GUI, the last part of code is in my GUI. Sorry english is not my mother language ^^'

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

    Default Re: How to call a function from another class when I'm in te Graphical Environment

    when I'm in the Graphical Enviroment
    Can you describe what that means? Most code that is executed is define inside of a method that is in a class.
    I don't know what you are describing when you say "in the Graphical Enviroment".
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: How to call a function from another class when I'm in te Graphical Environment

    Quote Originally Posted by lttlLux View Post
    I'm talking about the GUI, the last part of code is in my GUI. Sorry english is not my mother language ^^'
    The location of that code does not really matter. What Jim is telling you is that there is no instance of the Satelite class that can call the method inside that class. Assuming that you always want to create a new instance once the event handler is triggered, your code should look something like
    Satelite auxs = new Satelite();

  6. #6
    Join Date
    May 2016
    Location
    Brazil
    Posts
    16
    Rep Power
    0

    Default Re: How to call a function from another class when I'm in te Graphical Environment

    Looks like the line

    Java Code:
    aux=ss.getPorNombre(cuerpos, s);
    is not returning a valid Satelite object, by the way I'm not seeing ss being declared/initialized.

Similar Threads

  1. How to call a VBA function in Java
    By gorgon777 in forum New To Java
    Replies: 0
    Last Post: 01-20-2014, 01:24 PM
  2. call a function dynamically
    By bigjo in forum New To Java
    Replies: 3
    Last Post: 01-18-2013, 03:56 PM
  3. Call a function via URL
    By cali_guy_0024 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 05:04 AM
  4. function call error
    By peter_thm in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 12:57 PM
  5. help with System.exit (1) function call
    By katie in forum Advanced Java
    Replies: 2
    Last Post: 08-06-2007, 08:03 PM

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
  •