JTable + MySQL – Java

September 27, 2009
Filed under: Featured, JAVA, MySql, MySql 5.0, MySQL 5.1, Springs 

JTable + MySQL Java. JTable + MySQL. 7 Hours Ago. If in big problem, I want a to add the ResultSet of MySQL in JTable.Table should in Panel. and There should be autosizing of the result.




It’s a very common requirement, and there are a number of good sample solutions on the web (basically you have to loop down the whole column to find the widest entry, then use that size to set the column width).
You may find this useful:

Help with Code Tags
JAVA Syntax (Toggle Plain Text)
  1. public void packColumns(JTable table) {
  2. DefaultTableColumnModel colModel = (DefaultTableColumnModel) table
  3. .getColumnModel();
  4. for (int cNumber = 0; cNumber < table.getColumnCount(); cNumber++) {
  5. TableColumn col = colModel.getColumn(cNumber);
  6. int width = 0;
  7. // Get width of column header
  8. TableCellRenderer renderer = col.getHeaderRenderer();
  9. if (renderer == null) {
  10. renderer = table.getTableHeader().getDefaultRenderer();
  11. }
  12. Component comp = renderer.getTableCellRendererComponent(table, col
  13. .getHeaderValue(), false, false, 0, 0);
  14. width = comp.getPreferredSize().width;
  15. // Get maximum width of column data
  16. for (int r = 0; r < table.getRowCount(); r++) {
  17. renderer = table.getCellRenderer(r, cNumber);
  18. comp = renderer.getTableCellRendererComponent(table, table
  19. .getValueAt(r, cNumber), false, false, r, cNumber);
  20. width = Math.max(width, comp.getPreferredSize().width);
  21. }
  22. col.setPreferredWidth(width + 2); // Add margin
  23. }
  24. }
More Info : click here

Comments

Tell me what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!





*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image