Results 1 to 14 of 14
  1. #1
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Question 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();
          
          }
       }

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default

    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);
                }
          }
       }

  3. #3
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Wink

    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

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    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.

  6. #6
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default 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();

    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    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.

  8. #8
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    read the graduate files then after it finishes
    How do you detect the end of file when reading from it?
    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.

  10. #10
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    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");
                   
                   }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    This:
    Java Code:
    catch ( java.io.IOException io )
    {
      System.err.println("io error");
    }
    doesn't give much useful information.

    This is better:
    Java Code:
    catch ( java.io.IOException io )
    {
      io.printStackTrace();
    }

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    when it runs it gives me this:

    Error reading file
    out
    Do you see where in your program that output comes from?

    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.
    The error could be because the data in the file is NOT in the DataInputStream required format.
    As Fubarable says: io.printStackTrace(); will show us what the error is.

  13. #13
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  14. #14
    Nemo1959 is offline Member
    Join Date
    Sep 2008
    Posts
    13
    Rep Power
    0

    Default

    Oh and I added this as well and took out the other one
    Java Code:
    catch ( java.io.IOException io )
    {
      io.printStackTrace();
    }

Similar Threads

  1. Changing Active Window
    By jmHoekst in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 12:21 AM
  2. Dynamically changing the display
    By abhiN in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-22-2008, 11:19 PM
  3. Changing icon of JOptionPane
    By mew in forum New To Java
    Replies: 3
    Last Post: 12-21-2007, 07:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •