Results 1 to 4 of 4
  1. #1
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default Adding Rows to a JTable

    {SOLVED}

    Hey everyone,

    I am trying to add a row (or more) to a JTable on a button push, with the following method (explanation below):

    Java Code:
      

    The jTable and DefaultTableModel were created in NetBeans Swing pallette with some customisation to allow me to access toe DefaultModelTable:

    Java Code:
      

    The table starts out with no rows, although the column headers are visible. When the button is pushed, the code iterates through a LinkedList<Item> (Item is a superclass to Power) stored in the static Basket class, and takes data from each item to add it to the column.

    I have confirmed that there are items are correctly stored in the basket.
    The data is converted to Strings in one way or another and should be added to the table as an array using the tableModel1.addRow() method.

    My trace statements all fire, so the code definitely reaches this line when the code is ran, but the row is not added to the table for some reason, because when I print out the row count it remains zero.

    I am not getting any exceptions or error messages.

    I am also aware that fireTableDataChanged() isn't necessary when using addRow(), it didn't help with or without.

    Help would be greatly appreciated!
    Last edited by mlad; 05-04-2011 at 10:45 PM. Reason: code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I wonder if you have more than one DefaultTableModel object, one that is used by the displayed JTable and that has no rows, and another distinct DefaultTableModel object that is not displayed but that gets rows added to it. We will need to see more code I think to solve this, and the best code to create and post here (if possible) is an SSCCE.

  3. #3
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    You must have excellent debugging intuition! I was actually using 3 JTables and 3 DefaultDataModels, unfortunately named jTable1, 2 and 3 and tableModel1, 2 and 3; I took two out to push my post towards SSCCE.

    I was testing adding rows on jTable2 only, but had assigned it tableModel3. Spotted this after reading your suggestion. It is working now, thank you!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Jtable rows
    By riddhishah28 in forum AWT / Swing
    Replies: 3
    Last Post: 02-18-2011, 07:24 AM
  2. Remove Jtable rows
    By anilkumar_vist in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2010, 09:03 AM
  3. JTable insertRows() just adding blank rows
    By Jacob_ in forum AWT / Swing
    Replies: 4
    Last Post: 10-10-2009, 04:56 AM
  4. Highlight rows of a jtable
    By simmi in forum AWT / Swing
    Replies: 5
    Last Post: 08-20-2009, 02:13 PM
  5. display rows in jtable
    By osval in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 09:54 PM

Posting Permissions

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