I want to store data (name, number), after the user clicks on a JButton "set". The premise of the program is that there are 6 circles drawn in a frame. Selecting a circle on mouse click, entering data and clicking the set button will store that information for that circle. Then when the circle is clicked again, the information will be displayed in the same JTextFields. And clicking a circle and clicking the "reset" button would clear that data. I'm not sure if I should use arrays, write a class that extends the one that creates the circles....I'm kind of lost on where I should start.
The buttons "Set" and "Reset" -- are they on the circles or somewhere else on the panel?
Either way, you could define the JTextFields to be class members and do a field.setText() to set the value and field.getText() to display the value. Resetting can be done by setting the value to be an empty string.
They are somewhere else on the panel. Well I have to be able to store the information up to 6 times for each player. Then recal the stored information when that player is clicked. Then clear the entires made if reset is clicked.
easier to post an example (rough), than try to explain it.
this uses a JList instead of your 6 circles, but the principle is the same click on an item to display the stored data, 'set' to store the data from the textfields, 'reset' to clear the data
That was a very informative example, although i'm not 100% sure on how to incorporate it into my program. Here's what I have. Not sure whether the class should extend my Player class (which extends the Shape abstract class).
sorry I should have been more clear, thats what I was curious about, how I identify the current selection. upon mouse event s = the current selection, whether it's p1, p2, etc.. It's already a shape object, so using that code from your example I will have a type shape which is incompatible with type Player. When i wasn't sure about how to incorporate it, thats because my Shape and Player superclass have alot of variables. I wasn't sure how to extend the Player class with a playerInfo class (in your example was called Player). I would have to make a super call and have many extra parameters. Then if I make it an independent class, the circle objects created are of type Shape, created using the Shape superclass and the Player class which extends it (in my program, Player class is diffrent).
here's the earlier code modified for JLabels - click a label to get/set/reset the data. There is now a field 'currentPlayer' which is set when a label is clicked.
you should be able to adapt this (the labels) to your shapes.
note: in paintComponent(), it is generally not a good idea to do this p1 = new Player(size, colour1, filled, MidX, 75); as paintComponent() is called numerous times
This is how I would have to code it. Since I saved this until the last step I have to make it fit within the program i've already created. I've tried alot of diffrent things, and attempting to assimilate your examples just gives me errors. Mostly cannot find symbol because i'm not sure how to code the PlayerInfo class (information of the player clicked) which should extend the Player class (player objects created), which extends the Shape class (which is used for circles or rectangles at the moment). So I have to make a super call inside PlayerInfo to Player (which has all the variables for creating a circle). Inside my mouse event, i set whichever player is clicked to a Shape variable called player. Then no matter what I try in set or reset, I can't make it work. Mostly because i'm having difficulty coding my PlayerInfo class.
The demo doesn't even work, my program was to big, when I tried chopping it up there were way to many errors to debug. I can send you the full program if you want via email. The demo can at least give you an idea of what i'm trying to do. Instead I now create an array containting player objects. I made it so I have a PlayerInfo class that extends player and makes a super call to all the variables. In the setOrReset listener the main problem is I cannot reference nameData, because i'm looking inside Shape.
Alright, i'm not even going to bother anymore. I took the easy way out and just created an array inside my main program called names. This array stores all the names when "set" is clicked, and then when you click on a player the index value adjusts to that player. Using the index value if playerClicked != null i use setText(names[index]). I'm going to finsh up the program then try and re-write code to make it look more presentable. Thanks for all your help.
Destroy anything that stands in your way. Except this tiny ad: