2

In java I query a db and retrieve a result set, which I want to display in a scrolling pane.

Along with displaying the the results of the query, I want to have a checkbox on the left side of each row in the scrolling pane.

Adding the checkboxes is the part that I don't know how to do. Any suggestions on how to do this would be greatly appreciated.

I am using Swing and my IDE is Netbeans 7.1.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Riggster
  • 107
  • 3
  • 15

2 Answers2

4

Use JTable palcing it into a JScrollPane The link how to convert ResultSet to TableModel http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel

StanislavL
  • 56,971
  • 9
  • 68
  • 98
4

In addition to @Stan's example, the default renderer and editor for Boolean is a JCheckbox, so return Boolean for the column's class. There's an example here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You may want to open a new question. See also [*Table From Database*](http://tips4java.wordpress.com/2009/03/12/table-from-database/). – trashgod May 11 '12 at 22:44
  • Thanks for the example. I modified your example in the link above to create a table using a ResultSet from a MySQL table. It works beautifully, for the most part. There are a couple questions I have, though. Most pressing is that when I sort columns composed of strings the sort works just fine, but when I try to sort columns of type: java.sql.Date, java.sql.Time, Boolean, or Double I get an error. (Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String). Also, is there a way to make cells not editable by the user? – Riggster May 11 '12 at 22:51
  • You can return `false` from `isCellEditable()`. – trashgod May 11 '12 at 23:05
  • to make all of the cells in the table not editable, I used the following code: 'JTable table = new JTable(model) { public boolean isCellEditable(int row, int column) { return false; }; };' – Riggster May 12 '12 at 03:49
  • No, override `isCellEditable()` in the `TableModel`, not the view. – trashgod May 12 '12 at 03:52
  • Thanks...I am now overriding the TableModel – Riggster May 12 '12 at 23:16
  • Just for the record, I had to be [reminded](http://stackoverflow.com/a/9155689/230513) of this myself, not once but _twice_! :-) – trashgod May 13 '12 at 00:20