Results 1 to 14 of 14
Thread: Changing the Jframe
- 09-14-2008, 11:01 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Changing the Jframe
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.
Java 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(); } }
- 09-14-2008, 11:21 AM #2
you have the problem with adding action listener to your button,it should be :
Java Code:public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("View Student")){ 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); } } }
- 09-14-2008, 11:29 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Can you explain how that changes the Label from Graduate student to Undergraduate student. I am not trying to be smart, I just want to learn this right. I may not have explained it right either. Just in case that is the case here is a better explanation.
When reading the graduate files it should say graduate student list. When that completes then when it reads the undergraduate files the label should change to undergraduate student list and read those records.
- 09-14-2008, 03:37 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
This is what it is supposed to do. If this helps.
This file should read the records from the Graduate file first,
in sequential order as the “View Student” button is pressed.
When the program reaches the end of the Graduate Students
records, it should modify the JFrame to display
“Undergraduate Students” and then read and display the
files from the Undergraduate database
- 09-15-2008, 02:06 PM #5
Which JLabel do you want to change? Where in your code do you want to do this? I don't see any comments in your code describing what and where you want to do these things.
- 09-15-2008, 02:37 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
JFrame
After it reads the graduate files it is supposed to change modify the Jframe to read Undergraduate Student list and then bring up those files.
catch(EOFException e2)
{
closeFile(); // I think this should be removed
// code should be inserted somewhere here
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();
}
- 09-15-2008, 03:55 PM #7
That doesn't make sense to do all the processing you describe in a catch block. Exceptions are just that, exceptions. Normal processing should not be done on an exception basis.
Most methods you use to read from a file return a value to indicate EOF. You should test for that value instead of ignoring it and causing an exception by continuing to read the file.
- 09-15-2008, 05:24 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
That is why I am asking for help. I already knew that part of the code was messed up.
What I am asking is how do I get it to read the graduate files then after it finishes those it reads the undergraduate and changes the label to Undergraduate student list. If there is something wrong in the code, I would appreciate someone explaining how to fix it. I am sure those catch statements should be in there somewhere, but that part of the code is what is giving me the problem
I am not asking anyone to write the entire program for me. I have written the code for the files it needs to write(which works fine) and most of the code for this. I am not looking for someone to do it for me.
- 09-15-2008, 06:50 PM #9
How do you detect the end of file when reading from it?read the graduate files then after it finishes
What does the method you use return to tell you that?
What format is the file in that you are reading? You are using the DataInputStream class to read it, which implies that the file is not an ASCII text file.
I suggest that you try writing a very simple program that only reads and displays data from the file so that you can understand how to read a file and detect the end of it. Put the code that reads the records from the file in a loop. Read a recode, display it and read the next one, and so on. At the end of the file, write a message, close the file and exit the loop. If you can do that, then you can add to that program another loop to read, display etc another file. When you see how to do that, you will be able to continue on your above problem.
- 09-16-2008, 02:05 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Made these changes which was a suggestion from somebody.
It compiles fine but when it runs it gives me this:
Error reading file
out
Does that mean there is something wrong in the write portion?
Java Code: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) { System.err.println("Error reading file"); System.out.println("out"); System.exit(0); } catch ( java.io.IOException io ) { System.err.println("io error"); }
-
This:
doesn't give much useful information.Java Code:catch ( java.io.IOException io ) { System.err.println("io error"); }
This is better:
Java Code:catch ( java.io.IOException io ) { io.printStackTrace(); }
- 09-16-2008, 03:15 AM #12
Do you see where in your program that output comes from?when it runs it gives me this:
Error reading file
out
The error could be because the data in the file is NOT in the DataInputStream required format.What format is the file in that you are reading? You are using the DataInputStream class to read it, which implies that the file is not an ASCII text file.
As Fubarable says: io.printStackTrace(); will show us what the error is.
- 09-19-2008, 03:57 PM #13
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
When I ran the write part of this for the first time it automatically added the txt file. I have even deleted it and tried to save it as a UTF file and it does the same thing.
- 09-19-2008, 03:58 PM #14
Member
- Join Date
- Sep 2008
- Posts
- 13
- Rep Power
- 0
Similar Threads
-
Changing Active Window
By jmHoekst in forum New To JavaReplies: 1Last Post: 06-14-2008, 12:21 AM -
Dynamically changing the display
By abhiN in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-22-2008, 11:19 PM -
Changing icon of JOptionPane
By mew in forum New To JavaReplies: 3Last Post: 12-21-2007, 07:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks