Results 1 to 5 of 5
  1. #1
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default JTable - Please Help

    Hello,

    I am having problems with my code below. I do not get any compilation errors. However, when I run the code, I only see a Blank frame. Please help.

    Java Code:
    import java.awt.Container;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.sql.Statement;
    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;
    
    public class BooksDatabase extends JFrame {
    	
    	//JDBC driver name and database URL
    	static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
    	static final String DATABASE_URL = "jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\Books.mdb";
    	private Connection con;
    	private Statement statement;
    	private JTable databaseTable;
    	private Container container;
    	private String [] AttributeNames;
    	private Object[][] myArray=null;
    	 
    	int numberOfColumns, numberOfRows;
    	 
    	public BooksDatabase(){
    		super("Books");
    		//databaseTable.setVisible(false);
    		container = getContentPane();
            container.setLayout(null);
    		Query();
    		
    	}		
    			
    	private void init_Components() //initializes components that are going to be used.
    	{
    	        databaseTable = new JTable();
    	        JScrollPane myPane = new JScrollPane(databaseTable,
    					   JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
    					  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);   
    	        
    	        databaseTable.setModel(new DefaultTableModel(
    	            new Object [][] { {null},{null}, {null}, {null}},new String [] {""} ) {
    	              /**
    					 * 
    					 */
    					private static final long serialVersionUID = 1L;
    				@SuppressWarnings("unchecked")
    				Class[] types = new Class [] { java.lang.Object.class};
    	              boolean[]canEdit=new boolean[]{false};
    	                   
    	              @SuppressWarnings("unchecked")
    				public Class getColumnClass(int columnIndex) {
    	                    return types [columnIndex];
    	              }
    	           
    	              public boolean isCellEditable(int rowIndex, int columnIndex) {
    	                    return canEdit [columnIndex];
    	              }
    	        });
    
    	        myPane.setViewportView(databaseTable);
    	        myPane.setVisible(true);
    	        add(myPane);
    	       //jScrollPane1.setBounds(3, 120, 460, 150);
    	      
    	     
    	        pack();
    	}//end method init_Components
    	
    	
    
    	private void Query() {
    		try {
    			
    
    			//specify location of database on filesystem
    			System.setProperty( "C:\\Books.mdb","null" );
    			
    			//load database driver class
    			Class.forName(JDBC_DRIVER);
    			
    			//establish connection to database
    			con = DriverManager.getConnection(DATABASE_URL);
    	
    			//create statement for querying database
    			statement = con.createStatement();
    			
    			ResultSet resultSet = statement.executeQuery("SELECT authorid, firstName, lastName FROM Authors");
    			ResultSetMetaData metaData = resultSet.getMetaData();
    			numberOfColumns = metaData.getColumnCount();
    			//int numberOfRows = 0;
                AttributeNames=new String[numberOfColumns];
                for(int i=0;i<numberOfColumns;i++)
                    AttributeNames[i]=metaData.getColumnName(i+1);
                myArray=new Object[10000][numberOfColumns];
                int R=0;
                while(resultSet.next()) {
                    for(int C=1; C<=numberOfColumns;C++)
                        myArray[R][C-1]=resultSet.getObject(C);
                    R++;
                }
                resultSet.close();
                numberOfRows=R;
                Object[][] tempArray=myArray;
                myArray=new Object[numberOfRows][numberOfColumns];
                for(R=0;R<numberOfRows;R++)
                    for(int C=0;C<numberOfColumns;C++)
                        myArray[R][C]=tempArray[R][C];
                databaseTable.setModel(new ResultSetTableModel());//sets this table to model ResultSetTableModel
                databaseTable.setVisible(true);//Makes the Table Visible
            }//end try
            catch(Exception e) {
                e.printStackTrace();
            }//end catch
            
         // ensures database connection is closed when user quits application
            addWindowListener(
            
               new WindowAdapter() 
               {
                  // disconnects from database and exit when window has closed
                  public void windowClosed( WindowEvent event )
                  {
                	  //disconnectFromDatabase();
                     System.exit( 0 );
                  } // end method windowClosed
               } // end WindowAdapter inner class
            ); // end call to addWindowListener
            
    		
    	}//end BooksDatabase Constructor
    			
    }//end BooksDatabase Class
    //ResultSetTableModel.java
    Java Code:
    public class ResultSetTableModel extends AbstractTableModel {
    	
    	 private static final long serialVersionUID=317L;
    	 private int numberOfRows, numberOfColumns;
    	 String [] AttributeNames;
    	 Object[][] myArray=null;
    	 
         public int getColumnCount() {
             return (numberOfColumns);
         }
         public int getRowCount() {
             return(numberOfRows);
         }
         public String getColumnName(int i) {
             return (AttributeNames[i]);
         }
         public Object getValueAt(int row, int column) {
             return(myArray[row][column]);
         }
         public Class getColumnClass(int c) {
             return getValueAt(0, c).getClass();
         }
         public boolean isCellEditable(int row, int col) {
             //Note that the data/cell address is constant,
             //no matter where the cell appears onscreen.
             if (col < 3) {
                 return false;
             } else {
                 return true;
             }
         }
         public void setValueAt(Object value, int row, int col) {
             myArray[row][col] = value;
             fireTableCellUpdated(row, col);
         }       
    
    }
    Java Code:
    import javax.swing.JFrame;
    
    
    public class databaseTest {
    	 public static void main( String args[] )
    	   { 
    	      BooksDatabase bd = new BooksDatabase(); 
    	      bd.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    	     // bd.setSize( 325, 100 ); // set frame size
    	      bd.setVisible( true ); // display frame
    	   } // end main
    
    }
    Thank YOu

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

  4. #4
    BeeGee is offline Member
    Join Date
    Apr 2010
    Posts
    33
    Rep Power
    0

    Default

    I got some of the code from a reference textbook but I wrote the rest myself. I am using the database set up by the authors Deitel Deitel.
    Thanks.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Fine. First of all just forget about the database part and look at the UI. Better to write a simple application first of all with a JTable and dummy values. r035198x send you a link about layouts. Read it completely. When you design a UI in Java you've follow some patterns to place controls.

Similar Threads

  1. JTable
    By amitabh in forum NetBeans
    Replies: 2
    Last Post: 05-28-2010, 02:28 PM
  2. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  3. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  4. How to add in a new row in Jtable?
    By Ry4n in forum AWT / Swing
    Replies: 0
    Last Post: 01-18-2008, 12:26 PM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 06:57 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
  •