Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-26-2008, 02:45 AM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
Create different instance of a tablemodel
Hi all,

I need some help!

I got a class MyClassModel who extends AbstractTablemodel. From a JPanel, i got a JTable that i want to take the model of this class. How can i instanciate this class from my JTable

Something like that :
Code:
JTable table = new Jtable(new MyClassModel()), but this not work.
I also want to pass to MyClassModel a different data (an array or something else) to build my table

thank you for your help
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-26-2008, 07:07 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import javax.swing.*; import javax.swing.table.*; public class Test { public static void main(String[] args) { Test test = new Test(); JTable table = new JTable(test.getModel()); JOptionPane.showMessageDialog(null, new JScrollPane(table), "", JOptionPane.PLAIN_MESSAGE); } private AbstractTableModel getModel() { return new AbstractTableModel() { // You must specify at least these 3 methods. // See comments in AbstractTableModel api. public int getRowCount() { return 10; } public int getColumnCount() { return 4; } public Object getValueAt(int row, int col) { return String.valueOf((row+1)) + (col+1); } }; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-26-2008, 02:44 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
Hi,

This is an example of my class:

Code:
public class MyTableModel extends AbstractTableModel{ public MyTableModel(){} public int getRowCount() { return 10; } public int getColumnCount() { return 4; } public Object getValueAt(int row, int col) { return String.valueOf((row+1)) + (col+1); } }
and in my panel:

Code:
//in this table, i want to call a new instance of MyTableModel //to get the model JTable table = new JTable();
I hope this clear

I don't know to do this ! !

thx a lot
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-26-2008, 04:24 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
The fact is.......when i loop more than once in
Code:
for(int i=0;i<2;i++){ JTable table = new JTable(/*pass my new instance here*/); }
than the table take only the last loop to create my model in my table
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-27-2008, 05:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class Test { public static void main(String[] args) { Test test = new Test(); JPanel panel = new JPanel(new GridLayout(1,0,5,5)); for(int i = 0; i < 3; i++) { JTable table = new JTable(test.getModel()); Dimension d = table.getPreferredSize(); d.height = 175; table.setPreferredScrollableViewportSize(d); panel.add(new JScrollPane(table)); } JOptionPane.showMessageDialog(null, new JScrollPane(panel), "", JOptionPane.PLAIN_MESSAGE); } private AbstractTableModel getModel() { return new AbstractTableModel() { // You must specify at least these 3 methods. // See comments in AbstractTableModel api. public int getRowCount() { return 10; } public int getColumnCount() { return 4; } public Object getValueAt(int row, int col) { return String.valueOf((row+1)) + (col+1); } }; } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-27-2008, 02:07 PM
Member
 
Join Date: Mar 2008
Posts: 8
Bill is on a distinguished road
hi,

its not exactly that i want to do. My AbstractTableModel extends my class not a method return an AbstractTableModel, and my panel is in another class

Code:
Public class MyTableModel extends AbstractTablemodel{ }
and my panel
Code:
public class MyPanel extends JPanel{ }
my loop is in this panel class and i want to instantiate the model of MyTableModel from a JTable in MyPanel.

Thanks a lot for your help
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-27-2008, 04:49 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import javax.swing.*; import javax.swing.table.*; public class TryIt { public static void main(String[] args) { JOptionPane.showMessageDialog(null, new JScrollPane(new MyPanel()), "", JOptionPane.PLAIN_MESSAGE); } } class MyPanel extends JPanel { public MyPanel() { super(new GridLayout(1,0,5,5)); for(int i = 0; i < 3; i++) { JTable table = new JTable(new MyTableModel()); Dimension d = table.getPreferredSize(); d.height = 175; table.setPreferredScrollableViewportSize(d); add(new JScrollPane(table)); } } } class MyTableModel extends AbstractTableModel { public int getRowCount() { return 10; } public int getColumnCount() { return 4; } public Object getValueAt(int row, int col) { return String.valueOf((row+1)) + (col+1); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Instantiation using an instance factory method Java Tip Java Tips 0 03-29-2008 01:35 PM
passing an array into an instance lockmac New To Java 1 08-08-2007 10:35 AM
New Instance for SWT srinivasa_v SWT / JFace 1 08-08-2007 02:02 AM
problems with Instance Data paty New To Java 2 08-02-2007 06:45 PM
Instance variable Jack New To Java 2 07-04-2007 05:00 AM


All times are GMT +3. The time now is 01:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org