I am new to java and the book I am using doesn't explain how to do this.
How do I change the JLabel to Undergraduate students after it reads all the Graduate students? I have been banging my head against the wall for weeks trying to figure this out. Any help is appreciated.
Code:import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.JFrame;
public class StudentRead extends JFrame implements ActionListener
{
private JLabel gradStudentList = new JLabel("GRADUATE Student List",JLabel.CENTER);
private JLabel undergradStudentList = new JLabel("UNDERGRADUATE Student List",JLabel.CENTER);
private Font bigFont = new Font("Arial", Font.ITALIC, 24);
private JLabel prompt = new JLabel("View the students",JLabel.CENTER);
private JTextField idNumText = new JTextField(4);
private JTextField lastNameText = new JTextField(6);
private JTextField firstNameText = new JTextField(6);
private JLabel idNumberLabel = new JLabel("ID Number",JLabel.CENTER);
private JLabel lastNameLabel = new JLabel("Last name",JLabel.RIGHT);
private JLabel firstNameLabel = new JLabel("First name",JLabel.RIGHT);
private JButton viewStudentButton = new JButton("View Student");
private Container con = getContentPane();
DataInputStream gradStudentInStream;
DataInputStream undergradStudentInStream;
public StudentRead()
{
super("Read Student Records");
try
{
gradStudentInStream = new DataInputStream(new FileInputStream("GradStudentsFile"));
undergradStudentInStream = new DataInputStream(new FileInputStream("UndergradStudentsFile"));
}
catch(IOException e)
{
System.err.println("File not opened");
System.exit(1);
}
setSize(300,175);
con.setLayout(new FlowLayout());
gradStudentList.setFont(bigFont);
con.add(gradStudentList);
con.add(prompt);
con.add(idNumberLabel);
con.add(idNumText);
con.add(lastNameLabel);
con.add(lastNameText);
con.add(firstNameLabel);
con.add(firstNameText);
con.add(viewStudentButton);
viewStudentButton.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String lastName, firstName;
int IdNum;
try
{
IdNum = gradStudentInStream.readInt();
lastName = gradStudentInStream.readUTF();
firstName = gradStudentInStream.readUTF();
idNumText.setText(String.valueOf(IdNum));
lastNameText.setText(lastName);
firstNameText.setText(firstName);
}
catch(EOFException e2)
{
closeFile();
System.exit(0);
}
catch(IOException e3)
{
System.err.println("Error reading file");
System.out.println("out");
System.exit(1);
}
}
public void closeFile()
{
try
{
gradStudentInStream.close();
System.exit(0);
}
catch(IOException e)
{
System.err.println("Error closing file");
System.exit(1);
}
}
public static void main(String[] args)
{
StudentRead rsr = new StudentRead();
}
}
