Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: How to printing a Jtable

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to printing a Jtable

    I'M having problem on how to print a JTable that retrieves data from the database. ff is my code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.JTable;
    /*
    * NewJFrame.java
    *
    * Created on January 5, 2009, 8:58 AM
    */

    /**
    *
    * @author ATAGHER B T ace
    */
    public class NewJFrame extends javax.swing.JFrame {
    String select = "SELECT * FROM ver;";
    String url = "jdbc:mysql://localhost:3306/test";
    String driver ="com.mysql.jdbc.Driver";
    String user = "root";
    String password = "val";
    Statement stmt ;


    /** Creates new form NewJFrame */
    public NewJFrame() {
    try{
    Class.forName(driver);
    }catch(java.lang.ClassNotFoundException ex){
    System.err.println("class not found");
    System.err.println(ex.getMessage());
    }
    Connection con;
    try{
    con=DriverManager.getConnection(url, user, password);
    stmt = con.createStatement();
    }catch(SQLException ex){ex.printStackTrace();}


    initComponents();
    reload();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
    jScrollPane1 = new javax.swing.JScrollPane();
    jTable1 = new javax.swing.JTable();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    jTable1.setModel(new javax.swing.table.DefaultTableModel(
    new Object [][] {
    {null, null},
    {null, null},
    {null, null},
    {null, null}
    },
    new String [] {
    "name", "sex"
    }
    ) {
    Class[] types = new Class [] {
    java.lang.String.class, java.lang.String.class
    };
    boolean[] canEdit = new boolean [] {
    false, false
    };

    public Class getColumnClass(int columnIndex) {
    return types [columnIndex];
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
    return canEdit [columnIndex];
    }
    });
    jScrollPane1.setViewportView(jTable1);

    jButton1.setText("print");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .addContainerGap()
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 354, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(36, Short.MAX_VALUE))
    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
    .addContainerGap(255, Short.MAX_VALUE)
    .add(jButton1)
    .add(90, 90, 90))
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(37, 37, 37)
    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    .add(22, 22, 22)
    .add(jButton1)
    .addContainerGap(37, Short.MAX_VALUE))
    );
    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    dispose();
    }//GEN-LAST:event_jButton1ActionPerformed


    private void reload() {

    jScrollPane1.getViewport().remove(jTable1);
    String [] tablehead ={ "name", "sex"};
    String[][] tablebody = new String[getrows()][2];
    int j = 0;
    try{
    ResultSet rs = stmt.executeQuery(select);
    while(rs.next()){
    tablebody[j][0] = rs.getString("name");
    tablebody[j][1] = rs.getString("sex");
    j++;


    }
    }catch(Exception ex){}

    jTable1 = new JTable(tablebody, tablehead);
    jScrollPane1.getViewport().add(jTable1);
    repaint();

    }
    int getrows(){
    int i = 0;
    try{
    ResultSet rs = stmt.executeQuery(select);
    while(rs.next()){
    i++;
    }
    }catch(Exception ex){}

    return i;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    }
    Last edited by hundu; June 29th, 2009 at 07:07 AM. Reason: not yet solved


Similar Threads

  1. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM
  2. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 04:49 AM
  3. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM
  4. Problem of getting result than SQL to JTable
    By MS_Dark in forum Exceptions
    Replies: 1
    Last Post: March 10th, 2009, 06:26 AM