Reputation: 1
Okay so my program appears to be very basic, I have one class that inputs the students to a text file and this class has to read them, display them in a GUI so the user can pic which student the want and then a remove method to delete the students from the text file. The problem is that this is way more complex than the stuff that I have ever studied, and I am really in need of help, here is my code for the removing students class:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class StudentsRemove extends Frame implements ActionListener
{
String messsage="";
Button buttonView, buttonClose, buttonRemove; // Implements all the buttons
Label labelAnswer; // Implements all the different text boxes
TextField textAnswer;
StudentsRemove(String name)
{
super(name);
setLayout(new GridLayout(7,7));
labelAnswer = new Label("Remove Student: ");
textAnswer = new TextField("");
buttonView = new Button("VIEW STUDENTS");
buttonRemove = new Button("REMOVE");
buttonClose = new Button("CLOSE");
add(labelAnswer);
add(textAnswer);
add(buttonView);
add(buttonRemove);
add(buttonClose);
setVisible(true);
buttonView.addActionListener(this);
buttonRemove.addActionListener(this);
buttonClose.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
String s="";
String str = e.getActionCommand();
if(str.equals("VIEW STUDENTS"))
{
try
{
BufferedReader BuffRe = new BufferedReader(new FileReader("Students.txt"));
StringBuilder StrBu = new StringBuilder();
String line = BuffRe.readLine();
while (line != null)
{
StrBu.append(line);
StrBu.append(System.lineSeparator());
line = BuffRe.readLine();
//numStudents++;
}
String everything = StrBu.toString();
BuffRe.close();
}
catch(Exception z)
{
System.out.println("The Exception Is : " +z);
}
}
}
}
I need help displaying them in a GUI once I've read the entire program, and then allowing the user to select one of the students and removing them. I know it's a lot but I am completely lost and don't have an "extensive" knowledge of programming.
Thanks in advance guys.
Cyla.
Upvotes: 0
Views: 227
Reputation: 347204
Start by declaring a JList
and ListModel
instance variables...
private JList studentList;
private DefaultListModel model;
This is what will be used to display your information and allow the user to interact with.
Create some methods to update the model...
public void addStudent(String student) {
model.addElement(student);
}
public void deleteStudent(String student) {
model.removeElement(student);
}
Create methods to read and write the student information from and to disk...
public List<String> loadStudents() throws IOException {
List<String> students = new ArrayList<>(25);
try (BufferedReader br = new BufferedReader(new FileReader("Students.txt"))) {
String student = null;
while ((student = br.readLine()) != null) {
students.add(student);
}
} finally {
}
return students;
}
public void saveStudents(List<String> students) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("Students.txt"))) {
for (String student : students) {
bw.write(student);
bw.newLine();
}
bw.flush();
} finally {
}
}
Then load the student information from the file into the model...
protected void save() {
try {
List<String> students = loadStudents();
model = new DefaultListModel();
for (String student : students) {
model.addElement(student);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Could not read students.txt: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
studentList.setModel(model);
}
And write a method to save the information from the model to disk...
protected void save() {
List<String> students = new ArrayList<>(model.getSize());
for (int index = 0; index < model.getSize(); index++) {
students.add((String)model.get(index));
}
try {
saveStudents(students);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Could not write students.txt: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
Now, obviously, you need to create the JList
and add it your UI, but I'll leave that up to you.
Take a look at Creating a GUI With JFC/Swing and How to Use Lists for more details
Upvotes: 1