3 Replies Latest reply: Oct 17, 2012 7:51 AM by 967000 RSS

    Cant see the Jtable in my form , data is being retrieved from db.

    967000
      Hey,
      i cant see the table contents at all! The code is correct! I know that!
      Only two constructors are provided for Jtable one with Objects and other with Vectors! I have used Vectors properly.
      Please helpme since i cant see the GUI i.e the table at all. The data is being retrieved properly from the database.
      I have given the output below.
      package com.frame;
      
      import com.dao.SearchUserDAO;
      import java.sql.*;
      import java.util.Vector;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTable;
      
      /*
       * To change this template, choose Tools | Templates
       * and open the template in the editor.
       */
      
      /**
       *
       * @author yuta
       */
      public class AddToAccount extends javax.swing.JFrame {
      
          /**
           * Creates new form AddToAccount
           */
          public AddToAccount() {
              initComponents();
              System.out.println(SearchUserDAO.custid);
              ViewAccount();
              
              
          }
      
          /**
           * 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.
           */
          @SuppressWarnings("unchecked")
          // <editor-fold defaultstate="collapsed" desc="Generated Code">
          private void initComponents() {
      
              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              itemcodeaddtxt = new javax.swing.JTextField();
              jButton1 = new javax.swing.JButton();
              tablepanel = new javax.swing.JPanel();
              scrollPane = new javax.swing.JScrollPane();
              table = new javax.swing.JTable();
      
              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      
              jLabel1.setText("Item Code:");
      
              jButton1.setText("Add to Account");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      jButton1ActionPerformed(evt);
                  }
              });
      
              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                      .addContainerGap(477, Short.MAX_VALUE)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                          .addComponent(jButton1)
                          .addGroup(jPanel1Layout.createSequentialGroup()
                              .addComponent(jLabel1)
                              .addGap(18, 18, 18)
                              .addComponent(itemcodeaddtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)))
                      .addGap(14, 14, 14))
              );
              jPanel1Layout.setVerticalGroup(
                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                          .addComponent(jLabel1)
                          .addComponent(itemcodeaddtxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGap(18, 18, 18)
                      .addComponent(jButton1)
                      .addContainerGap(16, Short.MAX_VALUE))
              );
      
              table.setModel(new javax.swing.table.DefaultTableModel(
                  new Object [][] {
                      {null, null, null, null},
                      {null, null, null, null},
                      {null, null, null, null},
                      {null, null, null, null}
                  },
                  new String [] {
                      "Item Code", "Item Name", "Item Price", "Item Price"
                  }
              ) {
                  Class[] types = new Class [] {
                      java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
                  };
      
                  public Class getColumnClass(int columnIndex) {
                      return types [columnIndex];
                  }
              });
              scrollPane.setViewportView(table);
      
              javax.swing.GroupLayout tablepanelLayout = new javax.swing.GroupLayout(tablepanel);
              tablepanel.setLayout(tablepanelLayout);
              tablepanelLayout.setHorizontalGroup(
                  tablepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(tablepanelLayout.createSequentialGroup()
                      .addGap(30, 30, 30)
                      .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 714, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap(38, Short.MAX_VALUE))
              );
              tablepanelLayout.setVerticalGroup(
                  tablepanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(tablepanelLayout.createSequentialGroup()
                      .addGap(31, 31, 31)
                      .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap(26, Short.MAX_VALUE))
              );
      
              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addContainerGap()
                      .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                      .addGap(175, 175, 175))
                  .addGroup(layout.createSequentialGroup()
                      .addGap(47, 47, 47)
                      .addComponent(tablepanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap())
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                      .addComponent(tablepanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addContainerGap(59, Short.MAX_VALUE))
              );
      
              pack();
          }// </editor-fold>
      
          private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
              // TODO add your handling code here:
              
          }                                        
      
          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              /*
               * Set the Nimbus look and feel
               */
              //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
              /*
               * If Nimbus (introduced in Java SE 6) is not available, stay with the
               * default look and feel. For details see
               * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
               */
              try {
                  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                      if ("Nimbus".equals(info.getName())) {
                          javax.swing.UIManager.setLookAndFeel(info.getClassName());
                          break;
                      }
                  }
              } catch (ClassNotFoundException ex) {
                  java.util.logging.Logger.getLogger(AddToAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (InstantiationException ex) {
                  java.util.logging.Logger.getLogger(AddToAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (IllegalAccessException ex) {
                  java.util.logging.Logger.getLogger(AddToAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                  java.util.logging.Logger.getLogger(AddToAccount.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
              }
              //</editor-fold>
      
              /*
               * Create and display the form
               */
              java.awt.EventQueue.invokeLater(new Runnable() {
      
                  public void run() {
                      new AddToAccount().setVisible(true);
                  }
              });
          }
          // Variables declaration - do not modify
          private javax.swing.JTextField itemcodeaddtxt;
          private javax.swing.JButton jButton1;
          private javax.swing.JLabel jLabel1;
          private javax.swing.JPanel jPanel1;
          public javax.swing.JScrollPane scrollPane;
          public javax.swing.JTable table;
          public javax.swing.JPanel tablepanel;
          // End of variables declaration
      
          private void ViewAccount() {
            
         
      
                
                
             
              *try {*
      *                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");*
      *                    Connection conn = DriverManager.getConnection("jdbc:odbc:XE",*
      *                              "system", "tiger");*
      *                    System.out.println("Connection Successfull");*
      *                    System.out.println(conn);*
      *                    String q = "select * from items";*
      *                    *
      *                    PreparedStatement ptmt2 ;*
      *                    ptmt2 = conn.prepareStatement(q);*
      *                    ResultSet rec = ptmt2.executeQuery();*
      *     ResultSetMetaData rsmd=ptmt2.getMetaData();*
      
      *                    // Statement stmt = conn.createStatement();*
      
      *                    Vector colnames=new Vector<>(); 
      *                    for(int i=1;i<=rsmd.getColumnCount();i++)*
      *                         colnames.add(rsmd.getColumnName(i));*
      *                    *
      *                    System.out.println("Colnames: "+colnames);*
      *                    System.out.println("viewing items");*
      *                    Vector data=new Vector<>();*
      *                    while (rec.next()) {*
      *                         *
      *                    //     System.out.println(rec.getString(1) + "" + rec.getString(2));*
      
      *                         Vector row=new Vector<>();*
      *                         row.addElement(rec.getString(1));*
      *                         row.addElement(rec.getString(2));*
      *                         row.addElement(rec.getInt(3));*
      *                         row.addElement(rec.getInt(4));*
      *                         /*for(int i=1;i<=rsmd.getColumnCount();i++)*
      *                         {*
      *                              row.addElement(rec.getString(i));*
      *                              *
      *                              System.out.println("hello from obj"+rec.getObject(i));*
      *                         }*/*
      *                              *
      *                         data.add(row);*
      *                         System.out.println("row data:"+row);*
      *                    }*
      *                    *
                                
                                      
      *                    if(table!=null)*
      *                    {*
      *                         tablepanel.remove(scrollPane);*
      *                         *
      *                         scrollPane=null;*
      *                         validate();*
      *                         *
      *                    }               *
      
      *                    table=new JTable(data, colnames);*
      *                    scrollPane=new JScrollPane(table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);*
      *                    tablepanel.add(scrollPane);*
      *                    validate();*
                                      
      *                    *
      *               } catch (ClassNotFoundException | SQLException e1) {*
      *                    *
      *                    System.out.println(e1);*
      *               }*
          *}*
      }
      OUTPUT:

      run:
      Connection Successfull
      Logged in!
      Connecion Successfull
      sun.jdbc.odbc.JdbcOdbcConnection@162db22
      cust001
      cust001
      Connection Successfull
      sun.jdbc.odbc.JdbcOdbcConnection@1f31079
      sun.jdbc.odbc.JdbcOdbcResultSetMetaData@a00025
      +Colnames:thru vector [ITEMID, ITEMCODE, ITEMPRICE, ITEMQUAN]+
      viewing items
      +row data:[soap, 1223, 10, 12]+
      +row data:[soap, 1223, 10, 12]+
      +row data:[vimbar, 14578, 20, 44]+
      +row data:[null, null, 0, 0]+
      +row data:[soap, 1233, 10, 12]+
      row data:[11, 11, 11, 11]
      row data:[11, 11, 11, 11]
      row data:[11, 2, 2, 2]
      +row data:[dove soap, 123, 10, 12]+
      +row data:[sugar, 123, 10, 1]+
      BUILD SUCCESSFUL (total time: 17 seconds)

      I just cant see the GUI.

      Edited by: Yukta Lolap on Oct 16, 2012 8:27 AM

      Edited by: sabre150 on Oct 16, 2012 4:35 PM

      Moderator action : fixed faulty code tags
        • 1. Re: Cant see the Jtable in my form , data is being retrieved from db.
          967000
          this code works perfectly fine in Eclipse!Can anyone please tell me as to why this doesn't work in Net beans?
          • 2. Re: Cant see the Jtable in my form , data is being retrieved from db.
            gimbal2
            No, because its not working in Eclipse either - it works in Java, Eclipse only kicks it off for you. Try to run it from the command line using nothing but the JDK/runtime, can you make that work?
            • 3. Re: Cant see the Jtable in my form , data is being retrieved from db.
              967000
              if(e.getSource() == d.view)
                        {
                             try {
                                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                                  Connection conn = DriverManager.getConnection("jdbc:odbc:XE",
                                            "system", "tiger");
                                  System.out.println("Connection Successfull");
                                  System.out.println(conn);
                                  String q = "select * from items";
                                  
                                  PreparedStatement ptmt2 = null;
                                  ptmt2 = conn.prepareStatement(q);
                                  ResultSet rec = ptmt2.executeQuery();
                   ResultSetMetaData rsmd=ptmt2.getMetaData();

                                  // Statement stmt = conn.createStatement();

                                  Vector colnames=new Vector<>();
                                  for(int i=1;i<=rsmd.getColumnCount();i++)
                                       colnames.add(rsmd.getColumnName(i));
                                  
                                  System.out.println("Colnames: "+colnames);
                                  System.out.println("viewing items");
                                  Vector data=new Vector<>();
                                  while (rec.next()) {
                                       
                                  //     System.out.println(rec.getString(1) + "" + rec.getString(2));

                                       Vector row=new Vector<>();
                                       row.addElement(rec.getString(1));
                                       row.addElement(rec.getString(2));
                                       row.addElement(rec.getInt(3));
                                       row.addElement(rec.getInt(4));
                                       /*for(int i=1;i<=rsmd.getColumnCount();i++)
                                       {
                                            row.addElement(rec.getString(i));
                                            
                                            System.out.println("hello from obj"+rec.getObject(i));
                                       }*/
                                            
                                       data.add(row);
                                       System.out.println(row);
                                  }
                                  
                                  if(d.table!=null)
                                  {
                                       d.tablepanel.remove(d.scrollPane);
                                       
                                       d.scrollPane=null;
                                       d.validate();
                                       
                                  }
                                  

                                  d.table=new JTable(data, colnames);
                                  d.scrollPane=new JScrollPane(d.table,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                                  d.tablepanel.add(d.scrollPane);
                                  d.validate();
                                  
                             } catch (ClassNotFoundException e1) {
                                  // TODO Auto-generated catch block
                                  e1.printStackTrace();
                                  
                                  System.out.println(e);
                             } catch (SQLException e1) {
                                  // TODO Auto-generated catch block
                                  e1.printStackTrace();
                                  System.out.println(e);
                             }

                        }


              The above code works perfectly invoked on a button click. here i havent used any layout as such. Its just a button this works on eclipse! not in netbeans.