[java] My Trinity of Problems

Started by
5 comments, last by Kevinator 18 years, 6 months ago
Hello, 1) I am having a hard time understanding why the following function I have created does not seem to return what I expect:

public static String GenerateList(int Mode)
{
	String sData = "";
		
	for(int loop = 0; loop < MAX_TEAMS; loop++)
	{
		for(int cycle = 0; cycle < 5; cycle++)
		{
			//String sTemp = (tcTeams[loop].GetPlayerFirst(cycle));
			//aData.concat(sTemp);
			sData.concat("Blah");
		}
	}
	System.out.println("Testing: " + sData);
	return sData;
}

The MAX_TEAMS constant is declared as being 3 and the method does get called as it prints out "Testing: " (without the quotes). However, there is no indication of "Blah" anywhere after the string "Testing: ". It is as if the concat method did not do anything at all to the string. It must be something simple but I am failing to see what I did wrong. 2) My program's GUI involves having a set of button components, utilizing swing and awt, that is replaced by another set of buttons called from a different method that is superimposed onto the current set of buttons via BorderLayout. My question is should I explicitly call a method such as "this.removeAll();" to wipe out the components? Or will Java handle the trash cleanup for me and leave me better off than Win32 API? 3) After I have called another button routine, to replace the current set of buttons, sometimes the buttons do not redraw after something such as resizing or a tooltip appearing and fading. There is just the background of the frame left over. Is there something I should do to rectify this? Thank you for reading this.
Advertisement
1) Java strings never actually change after they are created. When you do something like concat(), what it actually does is makes a new string for you, it doesn't change the string you called it on.

So if you change this:

sData.concat("Blah");

to this:

sData = sData.concat("Blah");

then it should do what you want

2) Yes, I would remove the old components. Java won't really figure out that you are covering up the old buttons with new buttons (all that a layout does is set the x/y coords and the sizes). So the old buttons will still be there under the new buttons, which will probably cause problems.

3)
I'm not sure, are these custom buttons? Maybe it has something to do with how you are doing drawing (maybe you are overriding update() instead of paint() )?
Once again I should be hit with the "dumb stick" (strings are immutable).

Thank you so much.

I have the following in the callback to kill the old buttons and place new ones
case 0: //List Teams Button{//Remove Current Buttons and Output Area	jpButtonTarget.removeAll();				//Institute New Buttons and Info Output	TeamOrganizer.DataDisplay(jfHomeWindow, TeamOrganizer.GenerateList(0));	Modes.ListTeamsButtonMenu(jfHomeWindow);	jfHomeWindow.validate();}break;


jpButtonTarget.removeAll() kills the original buttons (I think)
DataDisplay does something unrelated to the area of the buttons
ListTeamsButtonMenu will draw the new buttons

Both the original method and the ListTeamsButtonMenu method for drawing buttons are almost exactly the same with respect to the button labels are different. The original buttons do not exhibit this problem, but once the originals are destroyed and the new ones take their place, the problem starts.
What kind of problems?

Maybe you are referencing the removed buttons
The only part of the program that could actually have anything to do with the buttons, that may be removed, is the following:
	public void actionPerformed(ActionEvent e)	{		final int iBUTTONCOUNT = 8;		final String sBUTTONNAMES[] = {"List Players", "List Teams", "List Games", "Exit", "By First Name", "By Last Name", "By Scores", "Back to Main Menu"};		int iButtonID = 0;			//Change String Names into Integer Representations		for(int loop = 0; loop < iBUTTONCOUNT; loop++)		{			if(e.getActionCommand().compareTo(sBUTTONNAMES[loop]) == 0)			{				iButtonID = loop;				break;			}		}...


I may have to just add a frame listener class that will repaint each time the coordinates change.
Re: Concatenating strings

You should look into using the java.lang.StringBuilder class. IME, it is many, many times faster than doing the obvious string = string + suffix business, and will produce less garbage too.
Huh. Useful tip. I never really gave it much thought, but you are right. [grin]

This topic is closed to new replies.

Advertisement