• Post Reply Bookmark Topic Watch Topic
  • New Topic

writing Queries on JTable  RSS feed

 
L Ganesh
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

i have a doubt regarding Jtable
can we write queries based on the values of jtable?

like if you think a jtable as a database table,
can we prepare some queries and write to make it work on the jtables?

 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not out of the box, but it should be possible. I have written a ResultSet wrapper around a JTable, but it doesn't have any filtering.

Writing a Statement / PreparedStatement for JTables, perhaps even Connection, should be possible, but it's not that easy. Especially the parsing of SQL queries can be tricky.
 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if by "writing queries" you mean to sort and/or filter the data in the JTable, do a serch for :

javax.swing.table.TableRowSorter
javax.swing.RowSorter
javax.swing.RowFilter

RowFilter.regexFilter() lets you create filters using regular expressions.
 
L Ganesh
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my intention to write queries on jtable is to select a subset of values and display pie/bar charts out of it.

 
Moojid Hamid
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable gets its data from TableModel. I am assuming the TableModel in your case is not database backed, or otherwise you would simply get the data from the database. There has to be some kind of underlying Collection object (instance variable of TableModel) that is holding the data. I found this open source project that could help you write queries against the underlying collection at http://josql.sourceforge.net/

I have never used it but it seems like exactly what you are looking for.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!