HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Mastering the JTable"
Vol. 6, Issue 1, p. 56

	

Listing 1
import javax.swing.*;
import java.awt.*;

public class BradyGirls extends JPanel{
   static JTable myTable;

BradyGirls(){
   myTable = new JTable(3,2);
   JScrollPane myPane = new JScrollPane(myTable,
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
 		JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   add(myPane);
   myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));


} 

public static void main(String args[]){
   JFrame myFrame = new JFrame("Brady Bunch Girls");
   myFrame.getContentPane().add(new BradyGirls());
   myFrame.setVisible(true);
   myFrame.pack();
   myTable.setValueAt("Marsha",0,0);  
   myTable.setValueAt("Jan",1,0);
   myTable.setValueAt("Cindy",2,0);
   myTable.setValueAt(new Integer(18),0,1);  
   myTable.setValueAt(new Integer(17),1,1);
   myTable.setValueAt(new Integer(16),2,1);
   } 
}
 
Listing 2

import javax.swing.*;
import java.awt.*;
import java.sql.*;

public class BradyGirls extends JPanel{
   static Connection Ex1Con;
   static Statement Ex1Stmt;
   static ResultSet Ex1rs;
   static JTable myTable;
  
BradyGirls(){
   myTable = new JTable(3,2);
   JScrollPane myPane = new JScrollPane(myTable,
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
   JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
   add(myPane);
   myTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
   }

public static void main(String args[]) throws SQLException{
   JFrame myFrame = new JFrame("Brady Girls Table");
   myFrame.getContentPane().add(new BradyGirls());
   myFrame.setVisible(true);
   myFrame.pack();

   //Initialize and load the JDBC-ODBC driver.
   try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   } catch(java.lang.ClassNotFoundException e) {
      	   System.err.print("ClassNotFoundException: ");
     		   System.err.println(e.getMessage());
   }
   String url = "jdbc:odbc:bradygirls";
   Ex1Con= DriverManager.getConnection(url, "", "");
   Ex1Stmt = Ex1Con.createStatement();
   Ex1rs = Ex1Stmt.executeQuery( "SELECT name, age FROM bradygirls ORDER BY name");
   int  li_row = 0;
   while (Ex1rs.next()) {
      myTable.setValueAt(Ex1rs.getString(1),li_row,0);
         myTable.setValueAt(Ex1rs.getString(2),li_row,1);
         li_row ++;
      } // while
   } 
} 



  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.