Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Small problem, trying to return a function?!?

Status
Not open for further replies.

SuperSharp

Programmer
Mar 14, 2005
9
GB
Hi there, I am having a few difficulties trying to return a function, located in a class to another class. I have just started java so I am not that great at it, but I am learning (slowly).

Basically, I am creating a GUI quiz. We have been given the classes for a command line version and we just have to implement it into GUI. The part I am having trouble with is the highscore table. I am trying to call the fuction from the highscore class and display the table in a lable within another class.

I have tried everything I can think of but I keep getting different errors. I think the 'display()' function within the highscore class, needs a return to display it, but I don't know how to return it all, only like a string or number etc.

Code:
    public void display()
    {
        System.out.println("\n\n\tHighest Scores");
        System.out.println("\t==============");
        for (int i=0; i<table.length; i++)
        {
            System.out.print("\t" + (i+1) + "\t");
            if (table[i] != null)
            {
                table[i].displayHighScore();
            }
            else
            {
                System.out.println("---");
            }
        }
    }
This is how I am calling it:
Code:
		HighScoreTable HS = new HighScoreTable();

		hS = new JLabel(HS.display());
		labelArea.add(hS);
Its not liking thay because of the '.display()' because you cannot display a void. This has really been bugging me and I am really quite confused. If anyone can help at all that would be brilliant!
Thanks very much for taking the time to read this,

Edd Marshall
 
A JLabel takes as a constructor a String or an Icon object - I am confused as to why you are using a function to do some seemingly unrelated actions to the construction of a JLabel ...

--------------------------------------------------
Free Database Connection Pooling Software
 
...and void is used to indicate that there is no return value.
 
Hi sorry I am quite confused with this myself.
Currently on the command line version of this, the 'display(); fuction displays the highscore table. I have to make a GUI version of the command line version and i am trying to get the highscore to display inside a label. Am I trying to do the impossible?
Do you have an idea of how would I get the highscore table to be displayed in the GUI version?
 
No, you're not trying to do the impossible, far from it. It does, however, sound like you're doing coursework. apologies if I'm wrong, but it's not considered ethical to use the forums in this way.

Basically, you need to understand how to use the AWT or Swing frameworks to do what you are attempting. For swing (cos you mentioned JLabel in your post) you'd need JFrame and JPanel for a stand-alone GUI application. Have a read of the Swing tutorial on the Sun site.
...and keep on in there. It's not as hard as it first appears.

Tim
 
I would try something like....
Code:
public String display()
    {
        String highscore;
        System.out.println("\n\n\tHighest Scores");
        System.out.println("\t==============");
        for (int i=0; i<table.length; i++)
        {
            System.out.print("\t" + (i+1) + "\t");
            if (table[i] != null)
            {
                table[i].displayHighScore();
         */if your highsocore is the last thing in your
          array then, something like:
          highscore = table[i].getValue();
          providing there is a method getValue that returns
          the value.  Also if it is an int array you have to convert that to a String/*
            }
            else
            {
                System.out.println("---");
            }
        }
        return highscore;
    }
and set highsocre = to whatever element it is in your array
 
Hi there,
Thanks for the link to the sun website and your replies. I have read through the swing tutorial and bits and pieces, which has helped immensely. The work I am doing is coursework but I have just got a bit stuck. I don't believe that it was unethical to ask for some help in a forum, but if you believe that I have misused the forum then I apologise. I do seem to be making some progress but it seems to be more trial and error at this point, than anything else, but I think thats probably the best way to learn :)
I am still having problems showing the highscore table, but now my GUI is fully built. Gosh, I never thought it would be quite this confusing, but i'm sure I will get there in the end ;) Thanks again for your time.

Edd Marshall
 
Hey Edd. Glad the tutorial was helpful.

If we can help you learn, guide you through your problems, and you go away knowing more than when you came, then this forum has succeeded. We just try to discourage those who merely use the site as a way to get us to do their work for them. You do not seem to fall into that category, so no need to apologise.

Trial-and-error is indeed a good way to learn, IMHO. You can only learn from mistakes if you make them first :)

Building GUIs in Java is confusing at first. I remember my initiation... but that's another story.

Keep at it, Edd .. and stay 'SuperSharp' :)

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top