Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-15-2008, 06:21 PM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
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("jdbcdbc: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);
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-15-2008, 06:38 PM
Senior Member
 
Join Date: Jun 2008
Posts: 175
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-16-2008, 07:09 AM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
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( "jdbcdbc: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);
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-16-2008, 07:27 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-16-2008, 11:45 AM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-16-2008, 01:38 PM
Senior Member
 
Join Date: Jun 2008
Posts: 175
Fubarable is on a distinguished road
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 [code] at the top of your block of code and the tag [/code] at the bottom, like so:

Code:
[code] // your code block goes here. // note the differences between the tag at the top vs the bottom. [/code]
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-16-2008, 02:51 PM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
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:
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);

Last edited by adeeb : 06-18-2008 at 04:10 PM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-17-2008, 04:19 AM
Senior Member
 
Join Date: Jun 2008
Posts: 175
Fubarable is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-17-2008, 04:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-17-2008, 05:06 AM
Senior Member
 
Join Date: Jun 2008
Posts: 175
Fubarable is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-17-2008, 05:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Same story going on there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-18-2008, 04:12 PM
Member
 
Join Date: May 2008
Posts: 29
adeeb is on a distinguished road
Hey,
please reply me no. Please give my answer
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-19-2008, 08:39 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help to implement JTable adeeb AWT / Swing 0 06-04-2008 07:26 PM
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help salmanpirzada1 Advanced Java 2 05-15-2008 11:15 AM
How to add in a new row in Jtable? Ry4n AWT / Swing 0 01-18-2008 01:26 PM
JTable to txt File Tzaphiel New To Java 0 12-17-2007 05:15 PM
Help with JTable fernando AWT / Swing 1 08-07-2007 07:57 AM


All times are GMT +3. The time now is 12:27 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org