aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Jtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jtable" Watch "Jtable" New topic
Author

Jtable

Jincy Thomas
Greenhorn

Joined: Apr 28, 2011
Posts: 4
hi everyone...

hey i am creating an interface which has a default table component where rows are added dynamically by the user using a button. The problem is when i create a new file , i want to delete the old table and load a fresh table. But it isnt working.. plzzzzz helps as soon as possible...

thank you....

Here is the code i used to create the initial table

private static DefaultTableModel model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("PRODUCT");
model.addColumn("COST");
private static JTable table = new JTable(model);
JScrollPane descriptionscrollPane = new JScrollPane(table);
public static void addingRow() {
model.insertRow(row_count++, new Object[] {""});
table.setCellSelectionEnabled(true);
}



this is the code i tried to delete the table and load a new model, but doesnt work.


bottomPane.remove(descriptionscrollPane);
descriptionscrollPane=null;
table=null;
model=null;
bottomPane.validate();
bottomPane.repaint();
model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("PRODUCT");
model.addColumn("COST");
table = new JTable(model);
descriptionscrollPane = new JScrollPane(table);
table.validate();
table.repaint();

Note: descriptionscrollPane inturn is a component in JPanel called bottomPane


Ginger
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
A JTable presents the data that is held in a model. When the model changes the JTable reacts to the changes and repaints itself. Changes include structural changes. But one could argue that a new structure implies a new model. What it doesn't imply: The need for a new component to present the model.

So you should either just update the model or you should create a new model and set it in the JTable. There should be no need to remove and create the presentation object, i.e. the JTable itself.
Jincy Thomas
Greenhorn

Joined: Apr 28, 2011
Posts: 4
thanks Hauke.... But i tried that also , it is not working , can you please give a code to do it. please help me ....
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
can you please give a code to do it.

Jincy Thomas
Greenhorn

Joined: Apr 28, 2011
Posts: 4
Thank you rob..... i used used the code you gave. So this is my new piece of code. But still its not working. I just want to load a mew model on a click of a button.




model = new DefaultTableModel();
model.addColumn("ID");
model.addColumn("NAME");
model.addColumn("PRODUCT");
model.addColumn("COST");
table.setModel(model);
table.validate();
table.repaint();
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
There is no need to validate() or repaint() the table. All you need to do is set the model. Your model should only have column headers since you didn't add any data to the model.

If you need more help then you need to post a SSCCE that demonstrates the problem.

And don't forget to use the "Code" button to mark your code so it will retain its original formatting.
Abhilash Etikala
Ranch Hand

Joined: Nov 08, 2009
Posts: 38

I think there is a problem with jScrollPane...you should remove the viewPort component...like this..

and since bottomPane inturn is a descriptionscrollPane...why to again create a scrollpane object...directly you can set the viewPort view as the newly created table...

I think.. to delete and recreate the table no need to recreate the JScrollPane..

Please correct me if my understanding is wrong...

Thank You,
Abhilash
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

No, Abhilash, you shouldn't remove the viewPort component or any other. Rob Camick has already provided the correct approach to replacing data in a JTable using setModel(...).


luck, db
There are no new questions, but there may be new answers.
Jincy Thomas
Greenhorn

Joined: Apr 28, 2011
Posts: 4
hi frnds....

I tried all possible things but its not working yet before loading the new model i am printing the number of rows in the old model and after loading also i am printing the count of rows in the new model . Unfortunately both are the same indicating that the new model in not loaded. And i am getting "java.lang.ArrayIndexOutOfBoundsException" .




This is the output i get in the console

before row count 2
after row count 2
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


please help me . stuck with this problem from 2 days.

Thanking you in advance......
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
stuck with this problem from 2 days


I still don't see a SSCCE anywhere so I don't expect you will be getting any more help since we are not mind readers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jtable