0

I am new to java, how does returning value work? I need to return the width of an GLabel, to use it in another method

private double createLabel(String text, int locX, int locY){
    double widthOfLabel = labelText.getWidth();
    return widthOfLabel;
}

(I've cut out the un=important parts) but this should return the width of the label.

how do i use it within another method?

private void  getWidthofLabel(){
    double name = ?????????
}

I tried some different stuff, but they all seem to just return null. One more thing when I get the return value of the createLabel method, do I have to set it´s parameters (string,int,int)? I don´t want that, but the debugger seems to force me to set them.

4 Answers 4

1

If you want to get the result of createLabel you will need something like:

double name = createLabel(text, locX, locY);

You will have to provide the parameters, otherwise you cannot compile the code. If you don't need them you can create an overload with no parameters:

private double createLabel(){
      double widthOfLabel = labelText.getWidth();
      return widthOfLabel;
}

but at this point I'm not sure what's going on in your code. :D

Sign up to request clarification or add additional context in comments.

Comments

1

The return call will be done like:

double name =  createLabel(text, locX, locY);

and if you define method like following:

private double createLabel(String text, int locX, int locY){
}

You would need to pass a String and two int. In case you want a variant where you need not to pass parameters; overload the method like:

private double createLabel(){
}

which you can call like:

double name =  createLabel();

Comments

0

You need to call the function:

double name = createLabel(text,locX,lockY);

where text is a string, locX and lockY is ints.
it is impossible to call a function without its parameters, unless there is another function with the same name (that's called overload method).

1 Comment

A function with the same name but different parameters is called an overload, not override.
0

you can call like this

private void  getWidthofLabel(){
   double name = createLabel("label", 3, 5);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.