Results 1 to 12 of 12
Thread: adding data to JTable
- 04-22-2012, 04:16 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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" } ));
-
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.
- 04-22-2012, 08:26 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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.
-
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:
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:Object[] row = new Object[4];
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.
- 04-22-2012, 09:26 PM #5
Re: adding data to JTable
You could check out camickr's Bean Table Model.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-22-2012, 10:14 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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);
-
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.
- 04-22-2012, 10:38 PM #8
Re: adding data to JTable
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-23-2012, 06:35 PM #9
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
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)
-
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.
- 04-23-2012, 07:54 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: adding data to JTable
Thank you!
-
Similar Threads
-
JTable validate already existing JTable data and set Background colour issue
By pi4r0n in forum AWT / SwingReplies: 4Last Post: 04-02-2012, 07:57 PM -
Adding JCheckBox to JTable
By loveboat.adam@gmail.com in forum New To JavaReplies: 2Last Post: 12-05-2010, 04:39 AM -
adding integers in JTable
By khanzaman in forum AWT / SwingReplies: 2Last Post: 06-14-2010, 09:12 PM -
Adding New JTable in JTable
By anilkumar_vist in forum New To JavaReplies: 0Last Post: 01-27-2010, 08:27 AM -
java project help, reading in from a file and adding data to JTable
By Ekul in forum New To JavaReplies: 0Last Post: 11-24-2009, 01:49 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks