• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

updating a textfield?

 
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have a text field for a user to change their major. They can type anything in the textfield, and then hit the button change major which should update the major on the same screen. But I am stuck....I can create new methods if that helps

my window which allows the major change


And my other classes with anything to do with major
Major:


Catalog:


Student
 
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am stuck


Please explain what you are stuck on and what problem you are having.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stuck on what to do with the value I get from them entering a new major. There’s a get major method but it doesn’t take any parameters, and the major method that does take a parameter is just used for finding majors in a list.
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stuck on what to do with the value I get


I assume you mean that there is a String in a textfield named ???1 that you want to copy to another text field named ???2.
Is that right?  What are the names of the variables where I have entered ???1 and ???2?
If the Strings are in text fields, there are methods to get the String from one textfield and set the contents of the other textfield to that String.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the first textField called majorName has the value of the setMajor method which is "undeclared". The textField in which the user enters a new major is changetheMajor. When they enter a major in changetheMajor, and hit the button to change the major I want majorName to show what the value was in changetheMajor
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hit the button to change the major I want majorName to show what the value was in changetheMajor


The code in the changeMajor listener needs references to the classes where changetheMajor and majorName are defined so it can access the get method of one and the set method of the other.

What class is  changetheMajor in?
What class is  majorName in?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are both in my student Info class. majorName get the get major. and getName methods from the major class. and setMajor is in my Student class
They are in the same window because I want the major change to happen in the same window.


Student Class with setMajor
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

They are both in my student Info class


Ok, if both fields are in the same class  have you tried using the get method to retrieve the user's input from the input text field
and the set method to put that String into the field where you want it displayed?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So something like changetheMajor = getmajor().getname();

SetMajor(changetheMajor)
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the API doc for the Text class to see what methods are available for the two Text fields.
There needs to be a method to get a String from: changetheMajor
There needs to be a method to set the contents of: majorName

Then add code to get the text from the one and set the text of the other.

I'm assuming that you want to copy the text from one Text to the other Text.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh you mean get and set text
So strong change = changetheMajor.getText();
Major name.setText(change)
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what I am suggesting?
Did you try it?
What happened?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I tried, and nothing happened when I hit the button
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was the handle method called?  Add a print statement that prints a message so you can see.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? Like inside the if statement have something like system.out.println(change)
To see if change is holding somehing
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One just inside in the handle method to see if it was called.

Also the one you suggested.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So nothing was being called so I decided to switch the code to this



It does what I want, but now I want it to stick with the student. Like if I closed the student selection after I changed the major, I want to be able to open it back up and have the major I entered be there when it gets opened
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Decided to add a major to the student list so now it looks like
Jeff,Beck,Computer Programming

Now when I run it though I get an arrayindexOutOfBounds: 1 on my college.loadStudents method which looks like:


And My Student class again


Error is coming from line


I dont see why
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to explain why I added the majors. Since set major makes all majors undeclared, and I did not originally make students with majors...even if I change majors it’s automaticall going right back to undeclared.

Give the student a major so I don’t have to set a major
 
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cody Biggs wrote:


I dont see why


Well,

look at what is in the Student file. Did you store the new Major in it? Add after the line 'String[] stu = line.split(",");' a check if 'stu' has length 3; if not, print out line.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks like its calling load students twice? Here is what I did


And here is the output, so it goes from 3 to 1
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

so it goes from 3 to 1


Also print the value of line so you can see what the computer saw when that code was executed.
Another useful method for debugging arrays is the Arrays class's toString method for printing the contents of an array:
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using that code provided it gave me
 
Piet Souris
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print out 'line' after that one, since that is causing the error.
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is 1 the value of?  Where is it printed?   Same for the 3?
Add an unique "id String" like the name of the variable being printed to the print statement so you can verify where it is printed.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im getting
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So theres something wrong with my list. I added a new list that looks like
Cody, Biggs, Programming
Blake, Bortles, twizzlers
strickland, black, kid

Ran the program, and it works fine now. Of course those list values arent gonna be what im using I just thought of words
 
Piet Souris
Saloon Keeper
Posts: 3304
146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was a line in your Student class that had no comma's. Therefore the split gave an array of length 1 causing the IndexOutOfBound exception.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I could use y'alls continued help. So I get the majors loaded in from the list, and the user can change the majors, but I still need to save that change even if I close the student window I selected from the list. So when I select him from the list again the new major will be there.

This is what I have now



 
Sheriff
Posts: 6039
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the list even when you exit the application?  Use a simple database or serialization.

Save the list when you close a window but the app is still running?  Put the list in a class that persists after you close the window.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes save the major after I close that student. When the app first runs there is a ListView where you select a student, and once selected a new window opens that shows the student info (first and last name plus major) in that same window they can change their major. I’m trying to get it once that student window is closed if I select them again from the list the new major shows
 
Knute Snortum
Sheriff
Posts: 6039
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming the ListView in the main window is backed by an ObservableList?  If so, make that list accessible to the Student class.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is I have. How do I make a lit accessible?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still need help with this one
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make an up-to-date explanation of your problem?
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have two text fields one thats shows the students major, and one for them to enter a new major. When the button is hit I want the new major to replace the original major. Which we already did, but when I close the student window the major goes back to normal. I want to save that major change, so even if I close the window the new major will be there when I open the student again.

 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I want to save that major


If you want to save it between program executions, it will need to be written to an external location like a file, database or the cloud.

If the Student's info is read from a file, then that file would need to be changed with the new major or the code would need to know that the data from the file is not up-to-date and the new data would need to be retrieved from where ever it was saved.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The application is always running. I’m not ending the program when I close the window. Each student is selected from a list, and then opens a new window to show the student. So when I change the major the original major textField should be updated. When that window is closed and selected again from the same running application it shows the new major.

Knute said something about making my list views accessible?
 
Norm Radder
Rancher
Posts: 3391
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I’m not ending the program

Ok, so the value is held in a variable somewhere in the program.
If the variable holding the old value of the major is changed and is replaced by the new major, how can the code get the old value again?  If its gone from the variable, the program can not get it again.
 
Cody Biggs
Ranch Hand
Posts: 335
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it does. The code gets the major name which is set to undeclared. Change the major textfield gets the new major, and sets it to majorName. but when that window closes it goes back to undeclared
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!