aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Event handling and local variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event handling and local variables" Watch "Event handling and local variables" New topic
Author

Event handling and local variables

aal fak
Greenhorn

Joined: Apr 25, 2013
Posts: 3
have to create buttons, first one will create an object (theTeam) when it's clicked for the first time and the labels are updated with the team’s name, count, and maximum size. Clicked again, message will appear that team has been already created and nothing is instantiated.

When the other two are clicked, it should check team instance has already been instantiated. If it has not, then a message asking the user to create a team first is displayed. If a team instance has been created, then the appropriate method of the team instance should be invoked.

So, when “Add player” button is chosen, the method addPlayer()should be invoked. When the button “Add remaining players” is clicked, the method addPlayers()is invoked. Either way,program should check first whether the team is full or not. If the team is full, an error message is displayed.

I have a problem with the object instantiation, clicking the first button should create the instance of my class thus invoking the constructor. The subsequent buttons rely on this event (instantiation) so, if the first button has not been clicked, the user is required to click it. The problem is when I use the instance in the other methods' bodies, it says it has not been initialized but I cannot instantiate it outside of the first button body so what should I do? I have attempted the flag method but it is treated as false whether the first button is clicked or not.

I do not expect someone to solve this instead of me, I would however, appreciate if you can pinpoint my error.

Thanks in advance

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Welcome to the Ranch

I tried adding code tags to your code, which makes it look better (), but your indentation is inconsistent and there were excess blank lines, which I deleted. Consistent indentation is a good way to see errors before they cause problems.
I suspect there is a design problem; if I see source == or anything I suspect you have been using addActionListener(this) whereas you should create a Listener for each button. Three buttons, three Listeners.
You should also not try to count clicks inside your method. You do not double‑click a button, so you don’t usually count clicks. And how is the click count ever going to be < 1?
I can see some problems about new Team(). You then calling its fields and setting the text from those fields. Those fields may be null, so surely you should be setting the fields from the text?
Never use == true or == false. I have explained why not here.
aal fak
Greenhorn

Joined: Apr 25, 2013
Posts: 3
Campbell Ritchie wrote:Welcome to the Ranch

I tried adding code tags to your code, which makes it look better (), but your indentation is inconsistent and there were excess blank lines, which I deleted. Consistent indentation is a good way to see errors before they cause problems.
I suspect there is a design problem; if I see source == or anything I suspect you have been using addActionListener(this) whereas you should create a Listener for each button. Three buttons, three Listeners.
You should also not try to count clicks inside your method. You do not double‑click a button, so you don’t usually count clicks. And how is the click count ever going to be < 1?
I can see some problems about new Team(). You then calling its fields and setting the text from those fields. Those fields may be null, so surely you should be setting the fields from the text?
Never use == true or == false. I have explained why not here.



Thanks, so am I to create different ActionListener classes for each button?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
I would recommend separate Listeners, yes.
I would also suggest you don’t count clicks.
And I would suggest you take the text from the text components and pass it to the Team constructor. You may need some validation of the text, however. That can be a very advanced topic.
aal fak
Greenhorn

Joined: Apr 25, 2013
Posts: 3
Campbell Ritchie wrote:I would recommend separate Listeners, yes.
I would also suggest you don’t count clicks.
And I would suggest you take the text from the text components and pass it to the Team constructor. You may need some validation of the text, however. That can be a very advanced topic.


So, by this the local variable (theTeam) will be initialized outside the first listener (button)?

My problem is that I need to get the size and other methods of the Team class when clicking the other two buttons. I'm required to initiate the object of the team class in the first button's body, so it keeps informing me that the variable might not have been initialized and when I created two listeners, it's not even identified as the declaration in the first listener body
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
aal fak wrote: . . . My problem is that I need to get the size and other methods of the Team class when clicking the other two buttons.
I don’t understand how you need three buttons to get size, etc. You can use one button and one actionPerformed method to call several methods which find the size, etc. Presumably you have the size in a text component somewhere already?
I'm required to initiate the object of the team class in the first button's body, . . .
Yes, somewhere in the Listener which you add to the create button, it will say team = new Team(...);
The addPlayer method does something different; it adds a player to an existing team. You must have a Team object already in existence for that to work. The thing about “not initialised” suggests you are declaring the Team object twice, as a local variable. Do you want it to be a local variable? Maybe team should be a field of some class or other.

At this stage, it looks as though you have design problems. A very effective way to get bad design is to try to design your application to match your GUI. You need to design your GUI to match your application. You should be able to display all the details of the teams without using a GUI at all. Like this:-Only when you can get that sort of command‑line program to work should you even think of putting a GUI on top of it.

Why have you got a createPlayer method in the Team class? Teams do not make players, so that method should not be there. It sounds like something which should be in the Player class, as a possible factory method.
Keith Spriggs
Greenhorn

Joined: Jul 19, 2012
Posts: 18
I presume a couple of things here.
1st is that theFlag is already set to false


2nd why don't you set the button to setenabled = false for the create team
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Event handling and local variables