Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default adding data to JTable

    hi, How can I write this code to add any data in user[] to the JTable named userTable?

    Java Code:
    userTable.setModel( new DefaultTableModel(
                    new Object [][] {
                        {user[0].getId(), user[0].getName(), user[0].getUserName(), user[0].getPassword()},
                        {user[1].getId(), user[1].getName(), user[1].getUserName(), user[1].getPassword()},
                        {user[2].getId(), user[2].getName(), user[2].getUserName(), user[2].getPassword()} 
                    },
                    new String [] {
                        "ID", "Name", "Username", "Password"
                    }
                ));

    it should look like this, but it doesn't work? :(


    Java Code:
    userTable.setModel( new DefaultTableModel(
                    new Object [][] {
                        {user[].getId(), user[].getName(), user[].getUserName(), user[].getPassword()}
                    },
                    new String [] {
                        "ID", "Name", "Username", "Password"
                    }
                ));

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

    Default Re: adding data to JTable

    Let's try to clarify the problem first.

    Are you trying to add a row of data to an existing JTable, one that already displays data?
    If so, then don't try to add anything to the JTable itself, but instead create a DefaultTableModel field that the JTable uses as its model, and add a row to this model via its addRow(...) method.

    If this is not what you're trying to do, then please clarify the problem for us.

  3. #3
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: adding data to JTable

    I want to create a JTable that has 4 column and uncertain number of rows which is related to length of user[] Array, first column should show user[].getId(), second column shows user[].getName(), 3rd column user[].getUsername() and last one should show user[].getPassword(). how can I create its Model?
    Last edited by hafezdivandari; 04-22-2012 at 08:34 PM.

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

    Default Re: adding data to JTable

    One way is to create a DefaultTableModel using the constructor that takes an array of Column names and an int for row count, and pass in your array of four column Strings, and 0 for the row count. Then using a for loop, loop through your user array and inside the loop create an array of 4 objects:

    Java Code:
    Object[] row = new Object[4];
    Then fill the array with the current user object, placing the appropriate field of the object into the appropriate array item, and then call addRow on your DefaultTableModel, adding your row. So in pseudocode:

    Java Code:
    create DefaultTableModel passing in a String array of column names and 0 for row count of 0.
    for each User in my users array
       create an array of Object[4] called row
       fill this array with item's from the ith User
       call addRow on my table model and pass in my row Object array.
    end for
    
    Create your JTable and use this model as the table's model.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: adding data to JTable

    You could check out camickr's Bean Table Model.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: adding data to JTable

    I Wrote this code, But it doesn't work, I mean the constructor works and add the first row but addRow() method in the for loop doesn't work and throws exception.

    Java Code:
             JTable userTable = new JTable();
             DefaultTableModel tableModel = new DefaultTableModel(
                    new String [] {
                        "ID", "Name", "Username", "Password"
                    }, 0);
                
                for (int i = 0; i < ShopStoreInfo.user.length ; i++) {
                    tableModel.addRow(new Object [] {
                       user[i].getId(), user[i].getName(), user[i].getUsername(), user[i].getPassword()
                    });
                }
                
                userTable.setModel(tableModel);

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

    Default Re: adding data to JTable

    It's *always* a good idea when you get an exception or compilation error to post the full text of the error or exception and indicate which line is causing it. Else we have to guess, and our track record for guessing isn't so good.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: adding data to JTable

    Quote Originally Posted by hafezdivandari View Post
    I... throws exception.
    When seeking help with an error or exception, post the entire error message or stack trace. Why should people have to guess at relevant information that is available with you?

    db

    edit Did I really take >6 minutes to read and respond to a PM? Guess I did.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: adding data to JTable

    Here is the entire exception. (the 915 line is the line that addRow() method performs).

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at AdminControlPanel$2.<init>(ShopStore.java:915)
    	at AdminControlPanel.<init>(ShopStore.java:894)
    	at SignIn$1.actionPerformed(ShopStore.java:150)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6463)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    	at java.awt.Component.processEvent(Component.java:6228)
    	at java.awt.Container.processEvent(Container.java:2213)
    	at java.awt.Component.dispatchEventImpl(Component.java:4819)
    	at java.awt.Container.dispatchEventImpl(Container.java:2271)
    	at java.awt.Component.dispatchEvent(Component.java:4645)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4816)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4476)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4406)
    	at java.awt.Container.dispatchEventImpl(Container.java:2257)
    	at java.awt.Window.dispatchEventImpl(Window.java:2677)
    	at java.awt.Component.dispatchEvent(Component.java:4645)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    	at java.awt.EventQueue.access$000(EventQueue.java:101)
    	at java.awt.EventQueue$3.run(EventQueue.java:666)
    	at java.awt.EventQueue$3.run(EventQueue.java:664)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:680)
    	at java.awt.EventQueue$4.run(EventQueue.java:678)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

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

    Default Re: adding data to JTable

    You are using the tableModel variable inside of its anonymous inner class. Don't do that. Make the TableModel object and then have your for loop after the anonymous class code.

  11. #11
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: adding data to JTable

    Thank you!

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

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 07:57 PM
  2. Adding JCheckBox to JTable
    By loveboat.adam@gmail.com in forum New To Java
    Replies: 2
    Last Post: 12-05-2010, 04:39 AM
  3. adding integers in JTable
    By khanzaman in forum AWT / Swing
    Replies: 2
    Last Post: 06-14-2010, 09:12 PM
  4. Adding New JTable in JTable
    By anilkumar_vist in forum New To Java
    Replies: 0
    Last Post: 01-27-2010, 08:27 AM
  5. Replies: 0
    Last Post: 11-24-2009, 01:49 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
  •