Results 1 to 13 of 13
  1. #1
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default Regarding JTable

    Hi,
    i am not getting the column names at top of the table. Heres the code, i really dont understand the problem. So please help. And also please tell me how to refresh it when i am trying to do the old values are not erased from the table. The new values are added below it.
    try
    {
    conn = DriverManager.getConnection("jdbc:odbc:addissue", "library","adeeb");
    ps = conn.prepareStatement("SELECT * FROM expired WHERE rollno=?",ResultSet.TYPE_SCROLL_SENSITIVE,ResultSe t.CONCUR_UPDATABLE);
    ps.setString(1,ftf.getText().toUpperCase());

    String colnames[] = {"ROLLNO","NAME","BOOK","BOOK ID","ISSUED","PRES_DATE","DAYS"};
    DefaultTableModel amodel = (DefaultTableModel) tb.getModel();
    amodel.setColumnIdentifiers(colnames);
    rs=ps.executeQuery();

    ResultSetMetaData rsmd = rs.getMetaData();
    int colno = rsmd.getColumnCount();
    while(rs.next())
    {
    Object[] row = new Object[colno];
    for(int i=0;i<colno;i++)
    {
    row[i]=rs.getObject(i+1);
    }
    amodel.addRow(row);
    }
    tb.setModel(amodel);
    tb.revalidate();
    }

    catch(Exception ce)
    {
    System.out.println(ce);
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The error is likely in the code that you're not showing us. How do you display the table? Do you simply place it into a JPanel? If so nothing is telling the column names to be drawn. Better to put it into a JScrollPanel, either that or place it into a JPanel BorderLayout.CENTER and place the column headers into the same panel BorderLayout.NORTH.

  3. #3
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default

    Thanks for replying, but see the below code i am unable to add the table to JScrollPane. Now Please Guide me

    JButton view = new JButton("View");
    JPanel btnp = new JPanel();
    btnp.add(view);

    view.addActionListener(new ActionListener()
    {


    public void actionPerformed(ActionEvent e)
    {
    Vector columnNames = new Vector();
    Vector data = new Vector();
    try
    {
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    Class.forName( driver );
    Connection connection = DriverManager.getConnection( "jdbc:odbc:addissue","library", "bismillah" );
    //tb.repaint();
    String sql = "Select * from expired ORDER BY rollno ASC";
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery( sql );
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();

    // Get column names

    for (int i = 1; i <= columns; i++)
    {
    columnNames.addElement( md.getColumnName(i) );
    }

    // Get row data

    while (rs.next())
    {
    Vector row = new Vector(columns);

    for (int i = 1; i <= columns; i++)
    {
    row.addElement( rs.getObject(i) );
    }

    data.addElement( row );
    }


    rs.close();
    stmt.close();
    JTable tab = new JTable(data, columnNames);
    }
    catch(Exception ce)
    {
    System.out.println( ce );
    }


    }
    });

    JScrollPane jsp = new JScrollPane(tab);

    jp2.setLayout(new BorderLayout());
    jp2.add(jsp);
    jp2.add(btnp, BorderLayout.SOUTH);

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

    Default

    Since you have use DefaulteTableModel, setModel() can be helpful on the table. I'm not remember the exact way of it. Better to check it on documentation.

  5. #5
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default

    Hi,
    thanks very much for all your efforts and help. I have got the table but when i click the button the old records are not erased and the new once are followed by them. I want to get the records freshly. Please see the table code i have used the DefaultTableModel and i cant understand where to add the functions like revalidate() so that i wont face the above problem

    code:


    JButton view = new JButton("View");
    JPanel btnp = new JPanel();
    btnp.add(view);
    //final JPanel vpanel = new JPanel();
    tab = new JTable();
    view.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    conn = DriverManager.getConnection("jdbc dbc:addissue", "library","adeeb");
    ps = conn.prepareStatement("SELECT * FROM expired");

    String colnames[] = {"ROLLNO","NAME","BOOK","BOOK ID","ISSUED","PRES_DATE","DAYS"};
    DefaultTableModel model = (DefaultTableModel) tab.getModel();
    model.setColumnIdentifiers(colnames);
    rs=ps.executeQuery();
    ResultSetMetaData rsmd = rs.getMetaData();
    int colno = rsmd.getColumnCount();
    // ((DefaultTableModel)tab.getModel()).fireTableDataC hanged();
    while(rs.next())
    {
    Object[] row = new Object[colno];
    for(int i=0;i<colno;i++)
    {
    row[i]=rs.getObject(i+1);
    }
    model.addRow(row);
    }
    tab.setModel(model);

    }

    catch(Exception ce)
    {
    System.out.println(ce);
    }

    }
    });
    JScrollPane jsp = new JScrollPane(tab);
    jp2.setLayout(new BorderLayout());
    jp2.add(jsp);
    jp2.add(btnp, BorderLayout.SOUTH);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You've posted here long enough that you should know about and start using code tags to allow your code to retain its formatting. No one wants to read unformatted code. To do this you place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]

  7. #7
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default

    Hi,
    thanks very much for all your efforts and help. I have got the table but when i click the button the old records are not erased and the new once are followed by them. I want to get the records freshly. Please see the table code i have used the DefaultTableModel and i cant understand where to add the functions like revalidate() so that i wont face the above problem
    Java Code:
    [code]
     JButton view = new JButton("View");
    JPanel btnp = new JPanel();
    btnp.add(view);
    //final JPanel vpanel = new JPanel();
    tab = new JTable();
    view.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    try
    {
    conn = DriverManager.getConnection("jdbc dbc:addissue", "library","adeeb");
    ps = conn.prepareStatement("SELECT * FROM expired");
    
    String colnames[] = {"ROLLNO","NAME","BOOK","BOOK ID","ISSUED","PRES_DATE","DAYS"};
    DefaultTableModel model = (DefaultTableModel) tab.getModel();
    model.setColumnIdentifiers(colnames);
    rs=ps.executeQuery();
    ResultSetMetaData rsmd = rs.getMetaData();
    int colno = rsmd.getColumnCount();
    // ((DefaultTableModel)tab.getModel()).fireTableDataC hanged();
    while(rs.next())
    {
    Object[] row = new Object[colno];
    for(int i=0;i<colno;i++)
    {
    row[i]=rs.getObject(i+1);
    }
    model.addRow(row);
    }
    tab.setModel(model);
    
    }
    
    catch(Exception ce)
    {
    System.out.println(ce);
    }
    
    }
    });
    JScrollPane jsp = new JScrollPane(tab);
    jp2.setLayout(new BorderLayout());
    jp2.add(jsp);
    jp2.add(btnp, BorderLayout.SOUTH);
    [/code]
    Last edited by adeeb; 06-18-2008 at 04:10 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    you do know that you can use all the code tags in the world, but if your code is not formatted to begin with, the code tags won't work. Should be common sense, no?

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

    Default

    Yes it is. It's much better to format the code in the way that others can read easily. Line spaces, tab spaces and all of stuff make big difference.

    Regarding your question, do you know what revalidate() do? Can't you identify the line of the code where start to write data to the table? This is your code though. :)

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Interesting reply by Bob Prime in your post in the JavaRanch: Regarding JPanel (Swing / AWT / SWT / JFace forum at JavaRanch)

    I hope you take it to heart.

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

  12. #12
    adeeb is offline Member
    Join Date
    May 2008
    Posts
    29
    Rep Power
    0

    Default

    Hey,
    please reply me no. Please give my answer

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

    Default

    Better to check it by yourself, on different locations of data written to the table. I can't test this, because I don't want to add more tables and drivers in my PC and mess.

    At least try it just after executing the query.

Similar Threads

  1. JTable to txt File
    By Tzaphiel in forum New To Java
    Replies: 1
    Last Post: 04-17-2010, 12:47 PM
  2. Help to implement JTable
    By adeeb in forum AWT / Swing
    Replies: 0
    Last Post: 06-04-2008, 07:26 PM
  3. Replies: 2
    Last Post: 05-15-2008, 11: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, 01:26 PM
  5. Help with JTable
    By fernando in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07: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
  •