This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm starting with Swing programming, so I hope that I am not wasting bandwidth on something trivial. However, I'm really stuck.
I'm creating a Swing application involving students. I've created a JFrame and populated it with JComboBoxes, JCheckBoxes, JRadioButtons and the appropriate event handlers. I read a list of students from a file and populate a JComboBox. When the application is run, the results are written to a file. This all works fine. The "Submit" button causes the data to be written to the file.
I have two questions. First, the handler for the "Submit" button merely writes the data to the file. However, it does not change the values of the various Swing components back to their default values. I'd like to uncheck the JCheckBoxes, etc. Can anyone tell me how to do so?
The second question is related. I'm going to want a second version of this program for editing student information. In other words, I'd like to select the student from a JComboBox and read the information from a file. Then, I'd like to use a from with JCheckBoxes, JComboBoxes, etc to edit that student's record and write it to a file.
Many thanks for any assistance you can render. I'm trying to do this quickly and am floundering.
> However, it does not change the values of the various Swing components back > to their default values. I'd like to uncheck the JCheckBoxes, etc.
create a reset() method where you set all your initial values, then call reset() at the end of the submit button's actionPerformed()
> I'd like to select the student from a JComboBox and read the information from a file.
create a Student class and read in all of the data from the file as Student objects. Store the students in an ArrayList, or Hashmap if you have id's etc
Include in the Student class a toString() which returns the student's name, or whatever it is you want displayed in the comboBox, then add the student objects to the comboBox.
Add a listener to the comboBox, so that when a student is selected, the various textfields/checkboxes/whatever are filled with that student's data.
The edit button removes the currently selected student from the collection, calls add() (method for adding a new student), then iterates the student collection saving the student information to a file (overwriting existing file - or to a temp file, then delete student file, rename temp etc)