• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event handling and local variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic