Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data in Swing Tables

 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In how many ways can we display data from database? (how many methods exists ?). The rows of the JTable will,obvious, not be fixed beforehand. Can anyone give me idea/codes?

Maki Jav
 
Jason Steele
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please rephrase the question.
 
Maki Jav
Ranch Hand
Posts: 446
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope that this would be clearer:
Which methods are available in swing api to display data from a database
to a JTable. The point is that number of JTable rows are not hardcoded. So I need to know the methods which I can use for dynamic presentation

Maki Jav
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will probably want to create an implementation of the TableModel interface to wrap the rows in your database. You can do this in several ways:
  • implement TableModel directly
  • extend AbstractTableModel
  • extend DefaultTableModel
  • use DefaulTableModel directly


  • I would recommend first looking at "extend AbstractTableModel". Also, make sure to take a look at the Javadoc and/or the JTable tutorial.
    [ January 24, 2004: Message edited by: Ken Krebs ]
     
    Jason Steele
    Ranch Hand
    Posts: 100
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you could extend the DefaultTableModel, but if you just want to know how to update a dynamic JTable, you can do the following:

    Then, whenever you want to add rows, do:

    To remove all rows:


    Hope this helps
     
    Maki Jav
    Ranch Hand
    Posts: 446
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thnaks for replying... So there are four ways I can use JTable to do the task.
    WHy do you think, kerbs, that extending AbstractTableModel is better?
    Thanks for the code Steele
    Maki Jav
     
    Ken Krebs
    Ranch Hand
    Posts: 451
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I find it to be relatively easy for most of my needs, at least easier than trying to mash DefaultTableModel into something usable or trying to implement the interface myself. That is the purpose of AbstractTableModel, i.e. it provides simple default implementations that you can selectively override. There are situations where DefaultTableModel or a subclass of it might be easier. You have to judge that for yourself.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic