|
|
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.
|
|

03-26-2008, 02:45 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 8
|
|
|
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 :
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
|
|

03-26-2008, 07:07 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
}
};
}
}
|
|

03-26-2008, 02:44 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 8
|
|
Hi,
This is an example of my class:
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:
//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
|
|

03-26-2008, 04:24 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 8
|
|
The fact is.......when i loop more than once in
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
|
|

03-27-2008, 05:34 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
}
};
}
}
|
|

03-27-2008, 02:07 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 8
|
|
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
Public class MyTableModel extends AbstractTablemodel{
}
and my panel
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
|
|

03-27-2008, 04:49 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,189
|
|
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);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|