• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

JTextFields & storing data

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> i'm not 100% sure on how to incorporate it into my program

you have a panel with circles
I have a Jlist with items

click a circle selects the circle
click an item selects the item

click 'set' or 'reset' and the action works with the current selection.

the difference is how you identify the current selection, and which
listener best suits.
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

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

 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you post a working demo program
just 2 circles, 1 textField, set and reset buttons.

then we can see (running) exactly what you have.
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
email me the files (address in my profile)

include 'java' in the subject, so I can ID it - address gets a lot of spam
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic