Learning Outcomes
Task 1 – Import a GUI Project
Task 2 Read a Text File
1
AST111103 Problem Solving with Programming Skills
Lab 9: Reading and Writing Text File
Understand the mechanism of file input/output
Read data from a text file and display in various GUI components
Write String value to a text file
We are writing a file read/write application this week.
Download Lab9_FileIOStart.zip from the Canvas and import it to your NetBeans IDE (If you have
forgotten how to import, please check the steps in previous labs).
You shall see a partially completed GUI application. We will start building our lab with this application.
Run the project and click the “Read” button. You will see the output as follows:
Open your project folder and you will find the input file under “
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Display the text content in Text Area
In the lab, we focus on modifying the code to make our GUI application works. The mechanism of FileReader and BufferReader will be discussed in lecture.
In the existing code, the output is printed by System.out.println(line);, if we want to change its printing target to the TextArea using .append() method, we can rewrite this code as follows:
Run the program again to see the changes, you will see:
Oh, the output is not line by line, how can we solve this problem?
Add “\n” (new line character) to code as follows:
2
AST111103 Problem Solving with Programming Skills
Modify the content of this txt file as you wish and run the program again, you will find that our starting application will read and display the file content to the output window.
Double click on the “Read” button to view the code and you will see the code for reading a file “src/input.txt” and read its content line by line using the readLine() method and display it using the System.out.println() method.
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
This is how we can read a file and display its content line by line in a Text Area.
Task 3 Putting File Content in Combo Box and List
3
AST111103 Problem Solving with Programming Skills
Caution! To continue with the following tasks, add the following two lines at the beginning of jButton1ActionPerformed method:
Can you guess what have the two statements above done?
Also change the content of our input file “
After you have performed the two changes above, run the program again and test it.
Currently, the combo box (drop down menu) and list are displaying “Item1, Item2, Item3 ,Item 4”, we
will use the line in the input file (i.e. Kenny, Timothy, Andrew and Simon) as their content.
Add the following 3 statements to your jButton1ActionPerformed method:
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
4
AST111103 Problem Solving with Programming Skills
Don’t worry for the error of the first statement, this is because we have used a new class DefaultBomboCoxModel that require us to import a Java library (just like calling external helper)
Click the blub with the red symbol and select Add import for javax.swing.DefaultComboxModel, this is a suggested solution by NetBeans IDE to help us solving problem by adding an import statement at the top of the programme.
Run the application and see what these three statements have done.
Remember that making a GUI application is like front-stage and back-stage in a drama,
1. We prepare a new model at the backstage (DefaultComboBoxModel model = new DefaultComboBoxModel();)
2. Read the file and add the item to this model line by line (while(…) { …model.addElement(line);… }
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Putting file content to the List
We have out the file content in the combo box, it is time to change the list as well
Add the statements to the jButton1ActionPerformed method as follows:
The mechanism is similar to the combo box, remember to add the library (click the blub) for DefaultListModel.
Run your program and test it.
Task 4 Write Output to a File
5
AST111103 Problem Solving with Programming Skills
3. Make substitution with the new model to our Combo Box (jComboBox1.setModel(model);)
Recall that .setXXX() methods allow the program to set new value from code (back stage) to properties of a component, for example, jTextFielf1.setText(“abc”);
and .getXXX() methods make the program to get data from the GUI (front stage), for example, String name = jTextField1.getText();
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Timothy Andrew
Kenny Simon
6
AST111103 Problem Solving with Programming Skills
Can you guess what are the functions of the following two methods? jComboBox1.getSelectedItem()
jList1.getSelectedValue())
Double click on the “Write” button and look at its code:
The code looks like the “Read” button, but it is in opposite direction, pay attention on the output file name (“
Try to modify the statement in .write() or even duplicate it several times.
Run the program and click the write button, check the content of the output file (“
You will find that the .write() method is similar to System.out.print() method, but it prints
output to a file.
Now, combine jComboBox1.getSelectedItem() and jList1.getSelectedValue())
with the .write() to produce output in file like:
You have chosen: ComboBox – , List – . You have chosen: ComboBox – , List – .
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved
Task 5 Exercise – Study the program
7
AST111103 Problem Solving with Programming Skills
This will record the choice of users in our GUI application in a file.
Study the program and make revision on how to read and write files, it would be very useful in your group project. Moreover, take a look at the code for the following methods: jList1ValueChanged() and jComboBox1ActionPerformed(), make sure you understand the code.
This week we do not have programming exercise, but there is a short quiz online.
The access code of the exercise is 34429252.
You can try the exercise unlimited of time, complete the exercise before next lab session and ask the instructor if you have any uncertainty.
© Copyright 2018, Prepared by Dr. Lau Ho Lam. All Rights Reserved