Results 1 to 4 of 4
  1. #1
    IamKira is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default How to clone a JTable?

    Hi,

    I would like to create a ByVal copy of a JTable, however, the TableModel is not being cloned; I mean the clone and the original Table do have the same model and changing one changes the other too. Do I need to extend the DefaultTableModel and make it implement the CloneAble Interface as well? (And thus every object a JTable uses?) Or is there a nicer way to do this?

    Thanks for help!

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Welcome IamKira, to Java Forums! ;)

    The DefaultTableModel represents it's data as references to objects. The Object class's clone method is protected. Also, it would be impractical to implement clone methods for every different class of every instance in the table's data model. The DefaultTableModel is represented by the JTable component using the toString() method. What I would do, is to instantiate a new DefaultTableModel object and populate it with the string representations of the objects in the original data modal. You can make this your clone() method for the DefaultTableModel extension if you like. ;) However, all the objects will now be strings, but that's a quick solution. :D

    Hope this helped. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    IamKira is offline Member
    Join Date
    Jul 2009
    Posts
    11
    Rep Power
    0

    Default

    Okay that should make it, I'm using only strings. thanks!

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Cool. It's a pleasure IamKira! :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

Similar Threads

  1. Help with cast, equals and clone
    By nellaf in forum New To Java
    Replies: 1
    Last Post: 04-19-2009, 07:57 AM
  2. How to clone an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:46 PM
  3. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 10:47 AM
  4. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:46 AM
  5. clone problem
    By feniger in forum New To Java
    Replies: 13
    Last Post: 01-13-2008, 11:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •