• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5269
  • Last Modified:

display JTable on JPanel

Hello,

Here is my GUI: http://people.clarkson.edu/~havenssm/gui3.doc
For some reason i could display my examTable on examPanel even though i am not getting any error message.
I am able to print the examID: System.out.println("examRs: " + examRs.getInt( 1));
I use the same displayJTables to display course Tables.
Below are the relevant code:

public class StudentSupportServices extends JFrame
{
    private StudentPanel student;
    private JPanel coursePanel;
    private JTable courseTable;
    private JPanel examsPanel;
    private JTable examsTable;
    private ProfessorPanel professor;
    private ExamPanel exam;
    private ControlPanel controls;
    private String url;
    private JTextArea output;
    private Connection accessCon;// = null;
    private Container c;
    private JButton findExam;
   
    /** Creates a new instance of StudentInformations */
    public StudentSupportServices( )
    {
     
        super("Student Support Services");
        if (!startApplication() ) System.exit(0);

        c = getContentPane();
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        fileMenu.add(new RenewDatabase());
       
        menuBar.add( fileMenu );
        setJMenuBar(menuBar);
       
        output = new JTextArea();
        output.setEditable(false);
       
         findExam =  new JButton( "Get Exams",
            new ImageIcon(getClass().getResource("images/Edit24.gif")));
            findExam.setMnemonic('E');      
            findExam.addActionListener(new FindExam());
       
        //setup window size
      //  Dimension wndSize = this.getToolkit().getScreenSize();
      //set the position to screen center and size to half screen size.
     //   setBounds(1, 1, wndSize.width -  wndSize.width/8 , wndSize.height/2);
        //setLocationRelativeTo(null);
         
         setDefaultCloseOperation( EXIT_ON_CLOSE );

         try
        {
         //Connect to ess database.  
            url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" +
            "C:/accessDb/Students.mdb";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            accessCon = DriverManager.getConnection( url );
            output.append("Connection successful\n");
         
        }
     
        catch( SQLException sqlex )
        {
            //process SQLException
            sqlex.printStackTrace();
            output.append("Connection Uncessful\n" + sqlex.toString() );
        }
          catch( Exception ex )
        {
            //process remaining Exception here
            ex.printStackTrace();
            output.append( ex.toString() );
        }
      //  course = new CoursePanel();
        student = new StudentPanel(accessCon );
        professor = new ProfessorPanel();
        exam = new ExamPanel();
       
        JPanel StudentProfessor = new JPanel();
        StudentProfessor.setLayout(new GridLayout(1,2));
        StudentProfessor.add( student );
        StudentProfessor.add( professor );
        courseTable = new JTable(7,7);
        examsTable = new JTable();
       
         // Allow only single a selection
 
        JPanel StudentCourseProfessorExamPanel = new JPanel();
        StudentCourseProfessorExamPanel.setLayout( new GridLayout(3,1) );
        StudentCourseProfessorExamPanel.add( StudentProfessor );  
       
        coursePanel = new JPanel();
        coursePanel.setLayout(new BorderLayout());
        coursePanel.setBorder(new TitledBorder(new EtchedBorder(),"Courses"));
        StudentCourseProfessorExamPanel.add( coursePanel );
       
        examsPanel = new JPanel();
        examsPanel.setLayout(new BorderLayout());
        examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Courses"));
        StudentCourseProfessorExamPanel.add( examsPanel );

        c.setLayout( new BorderLayout() );
        c.add( StudentCourseProfessorExamPanel ) ;
        controls = new ControlPanel(accessCon, student,professor,this, findExam);
        c.add( controls, BorderLayout.NORTH );
        c.add( output, BorderLayout.SOUTH );

       
       pack();
       show();
    }///////////////////// END OF CONSTRUCTOR
   
    //tell the user the state of any request
   public void setQueryInfo(String text)
   {
       output.setText("");
       output.append( text );
   }
   
     
   public void showErrorMessages( String err )
   {
       
       Toolkit.getDefaultToolkit().beep();
       JOptionPane.showMessageDialog(this,err, "Find Course",
        JOptionPane.ERROR_MESSAGE );

   }
   
   //check if there is a compenent on the panel already.
   //if yes, remove it and place a the current one.
   public void setCoursePanel( ResultSet rs )
   {
       if( coursePanel.getComponentCount() > 0 )
           coursePanel.removeAll();
       try{
           displayJTables( rs, courseTable );
          }
       catch(SQLException sqlex )
       {
           ;
       }
       //courseTable.setModel( new DefaultTableModel(rows, colHeads) );
       JScrollPane scroller  = new JScrollPane( courseTable );
       coursePanel.add( scroller );
       
   }
 

////////////  AN INNER CLASS HANDLING HOW EXAM TABLE IS DISPLAYED //////
   class FindExam implements ActionListener
   {
       private int cID;
       private int profID;
    //   public JButton findExam;
       
       public FindExam()
       {
           
       }
   
 
      public void actionPerformed(ActionEvent e)
        {
            //cehck to see if there is a compenent on panel already. If yes, remove it
            // and place a new one.
 
             if(examsPanel.getComponentCount() > 0 )
              examsPanel.removeAll();
             
            if(coursePanel.getComponentCount() > 0 )
            {
                if( courseTable.getSelectedRowCount() == 1 )
                {
                cID = Integer.parseInt( ""+courseTable.getValueAt(courseTable.getSelectedRow(), 0 ) );
                profID = Integer.parseInt("" +courseTable.getValueAt(courseTable.getSelectedRow(), 4));
            }
            else
            {      
                showErrorMessages("You must first select a course.");
            }
       }
      else
      {
          showErrorMessages("1.)Find the couses\n 2.) select the a course\n" +
          "3.) And find the exams for that course.");
      }  

             System.out.println("class Num:" + cID);
             System.out.println("profID: " + profID);
             
             Statement profStm = null;
             Statement examStm = null;
             ResultSet profRs = null;
             ResultSet examRs = null;
     
             try{
                 profStm = accessCon.createStatement();
                 examStm = accessCon.createStatement();
                 
                 profRs = profStm.executeQuery("SELECT * FROM professor WHERE "+
                "professorID ="  + profID + ";");
                 
                 examRs = examStm.executeQuery("SELECT * FROM Exam WHERE CourseID" +
                 "=" + cID +";");
                 if(examRs.next())
                 System.out.println("examRs: " + examRs.getInt( 1));
                if(profRs == null) System.out.println ("no prof found\n");
                 
                professor.displayProfessor( profRs,  profID );
                displayJTables( examRs, examsTable  );
                setQueryInfo( "\nQuery Successful" );
         
        }
        catch ( SQLException sqlex )
          {
             sqlex.printStackTrace();
          }
            finally
            {
                try
                {
                    if( profStm != null ) profStm.close();
                    if( examStm != null ) examStm.close();
//                   if( profRs != null )  profRs.close();
               //    if( examRs != null ) examRs.close();
                }
                catch (SQLException sqlex)
                {
                    sqlex.printStackTrace();
                }
            }
       
     }
     
     
 }////////////// END OF INNER CLASS FindExam /////////////////////////////////

   public void displayJTables( ResultSet rs, JTable table )
     throws SQLException
     {
            //position to first record
             boolean moreRecords = rs.next();
             
             //if there is no records, display a message
             if( !moreRecords )
             {
                 showErrorMessages("No course found for that student");
                 return;
             }
             
             Vector columnHeads = new Vector();
             Vector rows = new Vector();
           
         try
         {    
            //get column heads
             ResultSetMetaData rsmd = rs.getMetaData();
             for( int i = 1; i <= rsmd.getColumnCount(); ++i )
                 columnHeads.addElement( rsmd.getColumnName( i ) );
             
             //get row data
             do {
                rows.addElement( getNextRow(rs, rsmd) );
            }while( rs.next() );
          table.setModel( new DefaultTableModel(rows, columnHeads) );
          }
         catch (SQLException sqlex )
         {
             sqlex.printStackTrace();
         }
         
     }

}
0
komlaaa
Asked:
komlaaa
  • 5
  • 2
  • 2
3 Solutions
 
objectsCommented:
You don't appear to add it to your frame
0
 
zzynxSoftware engineerCommented:
I think
>>  if (examRs.next())
>>      System.out.println("examRs: " + examRs.getInt( 1));

could be a problem.

Remove that and do the logging inside displayJTables()
0
 
zzynxSoftware engineerCommented:
Remark:
I think you want
>>         examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Courses"));
to be
             examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Exams"));
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
objects means the analog of what you do for the coursePanel:

>>       JScrollPane scroller  = new JScrollPane( courseTable );
>>       coursePanel.add( scroller );
       
0
 
zzynxSoftware engineerCommented:
>> ... could be a problem.
By doing this
>>  if (examRs.next())
you move already one record in the result set.
So, you're going to miss that one in your exams table
0
 
objectsCommented:
> So, you're going to miss that one in your exams table

The sames already being done in the courses
0
 
komlaaaAuthor Commented:
>>You don't appear to add it to your frame
true. I am not supposed to be awake a this time.

>>The sames already being done in the courses
true.

>>
>>         examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Courses"));
to be
             examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Exams"));
>>
Thanks, that is the price u pay when u copy and paste --- remember Objects was right last time on that :) :)

0
 
komlaaaAuthor Commented:
Good Night centinelles!!
0
 
zzynxSoftware engineerCommented:
Thanks. Good night ;°)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now