?
Solved

display JTable on JPanel

Posted on 2005-03-16
9
Medium Priority
?
5,267 Views
Last Modified: 2013-11-23
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
Comment
Question by:komlaaa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1200 total points
ID: 13553423
You don't appear to add it to your frame
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13553438
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
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 800 total points
ID: 13553450
Remark:
I think you want
>>         examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Courses"));
to be
             examsPanel.setBorder(new TitledBorder(new EtchedBorder(),"Exams"));
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 800 total points
ID: 13553463
objects means the analog of what you do for the coursePanel:

>>       JScrollPane scroller  = new JScrollPane( courseTable );
>>       coursePanel.add( scroller );
       
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13553469
>> ... 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
 
LVL 92

Expert Comment

by:objects
ID: 13553503
> So, you're going to miss that one in your exams table

The sames already being done in the courses
0
 

Author Comment

by:komlaaa
ID: 13553556
>>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
 

Author Comment

by:komlaaa
ID: 13553569
Good Night centinelles!!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13553576
Thanks. Good night ;°)
0

Featured Post

Introducing Priority Question

Increase expert visibility of your issues by participating in Priority Question, our latest feature for Premium and Team Account holders. Adjust the priority of your question to get emergent issues in front of subject-matter experts for help when you need it most.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question