• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding a table to a frame  RSS feed

 
Nigel Patching
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a class A which extends JFrame. It creates an object of a class B which extends JTable. The constructor from class B creates a JTable object using the line:
JTable table = new JTable(tableModel);
When this object is added to the JFrame in class A, a blank rectangle appears. If I create the JTable using this line in the class B constructor:
super (new tableModel());
the table appears.
I would like to create the table using the first method. Would anyone know why the table doesn't appear?
I think I might be creating a JTable object but not returning it to the calling class A (because constructors don't return anything).
 
Laurent Leonard
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I make a remark ?
Extension is not the only way to use an object.
You say I extend the JFrame. Why ?
Does the new class is more specialized than the original ?
If it's the case, ok, let's extend it, but if not, avoid it.
Class A extends JFrame
{
JTable myTable; // to make it accessible in the class A
TableModel myTableModel; // idem
A()
{
super();
// you can use different layouts if you want.
// why not using JTable() instead of an extension of it.
myTable = new JTable();
myTableModel = new MyDataModel(); // extends the TableModel
myTable.setModel(myTableModel);
this.getContentPane().add(myTable);
}
}
------------------
Laurent Leonard
Laurent.Leonard@advalvas.be
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!