File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes self-generating JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "self-generating JTable" Watch "self-generating JTable" New topic
Author

self-generating JTable

marcia gold
Greenhorn

Joined: Aug 04, 2000
Posts: 2
Hi,
I am trying to create a table that adds rows and columns to itself as the viewer scrolls down and right in the JScrollPane. I want to do something like a JTree that "creates" itself as you open each node. I know how to do this for a JTree, but when I try what seems to be the same way (overriding the getChild and getChildCount methods for the JTree, which I thought might be like overriding the getColumnCount and getRowCount for a JTable) what happens is that the JTable goes wild and generates itself forever (or at least until it runs out of memory), so it never gets to display, I just get an out of memory runtime error. Can anyone tell me what I'm doing wrong?
Thanks in advance,
marcia
cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
Hi,
I am not very sure I entirely understand what you mean, but if you create a table, then add to JScrollPane, JSP will take care of scrolling. If you want to add columns, rows after the table is created, do like this:
1. create a defaulttablemodel -- dtm
2. create a JTable(dtm)
3. add JTable to the scrollPane.
in various methods, use dtm.addRow(), and it will upodate the table as well.
hope it helps.
Cheers.
marcia gold
Greenhorn

Joined: Aug 04, 2000
Posts: 2
Dear cata lin,
Thank you for your reply, actually, I know that part. Here's what I'm trying to do:
In a tree, you can "explicitly" or "implicitly" define a JTree:
"explicit" JTree definition:
private static JTree createExplicitTree()
{
// Create the tree root
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
// Create the first level of branches
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Branch A");
root.add(node);
node = new DefaultMutableTreeNode("Leaf B", false);
root.add(node);
node = new DefaultMutableTreeNode("Branch C");
root.add(node);
// Create the second level of branches
...
JTree explicitTree = new JTree(root);
return explicitTree;
}
"implicit" JTree definition:
private static JTree createImplicitTree()
{
/**
* Class ImpTreMod
* Subclass of DefaultTreeModel used to create an "infinite" binary tree.
*/
class ImpTreMod extends DefaultTreeModel
{
private DefaultMutableTreeNode treeroot;
private Vector treeModelListeners = new Vector();

/**
* Constructor: ImpTreMod
* @param TreeNode root
* The constructor for ImpTreMod calls the super class
* (DefaultTreeModel) constructor and saves root.
*/
private ImpTreMod(TreeNode root)
{
super(root);
treeroot = (DefaultMutableTreeNode) root;

}
/**
* Method: getRoot
* returns the root of the tree on request
*/
public Object getRoot()
{
return treeroot;
}
/**
* Method: getChild
* @param parent The parent node to be searched
* @param index The location to look for the child
* This method returns the child at the location given by index. The
* modification that I made adds two children if they are not already
* there.
*/
public Object getChild(Object parent, int index)
{
DefaultMutableTreeNode p = (DefaultMutableTreeNode)parent;
int[] intarray = {0,1};
try
{
p.getChildAt(index);
}
catch (ArrayIndexOutOfBoundsException e)
{
DefaultMutableTreeNode c = new DefaultMutableTreeNode("0");
insertNodeInto(c, p, 0);
c = new DefaultMutableTreeNode("1");
insertNodeInto(c, p, 1);
}
finally
{
}
return p.getChildAt(index);
}
/**
* Method: getChildCount
* @param parent The parent node to be searched
* This method returns number of children at this node. In a binary tree,
* the number is always 2.
*/
public int getChildCount(Object parent)
{
return 2;
}
/**
* Method: isLeaf
* @param node The node to be examined
* This method returns whether or not this node is a leaf (ie no
* children). In a binary tree, the answer is always false.
*/
public boolean isLeaf(Object node)
{
return false;
}
/**
* Method: getIndexOfChild
* @param parent The parent node to be searched
* @param child The child node to be searched for
* This method returns the index of the location of the child.
*/
public int getIndexOfChild(Object parent, Object child)
{
DefaultMutableTreeNode p = (DefaultMutableTreeNode)parent;
return p.getIndex((DefaultMutableTreeNode)child);
}
/**
* Method: addTreeModelListener
* @param l The TreeModelListener to be added.
* This method adds a TreeModelListener to the invoking tree model.
*/
public void addTreeModelListener(TreeModelListener l)
{
treeModelListeners.addElement(l);
}
/**
* Method: removeTreeModelListener
* @param l The TreeModelListener to be removed.
* This method removes a previously added TreeModelListener.
*/
public void removeTreeModelListener(TreeModelListener l)
{
treeModelListeners.removeElement(l);
}
}
DefaultMutableTreeNode aroot = new DefaultMutableTreeNode("0");
JTree implicitTree = new JTree(new ImpTreMod(aroot));
return implicitTree;
}
Note that in the implicit JTree definition, I have created a JTree that extends forever as long as you keep opening the tree nodes.
This same ideology? can be applied to JTables:
"explicit" JTable definition:
/**
* Method: createExplicitTable()
* this method creates an "explicit" table, that is, a table where the cell values
* are added by explicitly identifying them and adding each cell value into the
* table all at the same time.
*/
private static JTable createExplicitTable()
{
JTable explicitTable = new JTable(21,21);
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++)
{
explicitTable.setValueAt(new Integer(i*j), i, j);
}
}
explicitTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return explicitTable;
}
and now the "implicit" JTable definition:
/**
* Method: createImplicitTable()
* this method creates an "implicit" table, that is, a table where the cell values
* are created by a table model when the table is created. In this case, I have a
* fixed value for i, the table row and column count, but presumably i could be
* some value that is read from a database or other external source. I would like
* to make this table increase in size via an equation, like the tree, but I haven't
* figured out how to do this yet.
*/
private static JTable createImplicitTable()
{
class ImpTabMod extends DefaultTableModel
{
private int i = 21;

public ImpTabMod()
{
super();
}
public int getColumnCount()
{
return i;
}
public int getRowCount()
{
return i;
}
public Object getValueAt(int row, int col)
{ return new Integer(row*col); }
}
TableModel tableModel = new ImpTabMod();
JTable implicitTable = new JTable(tableModel);
implicitTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return implicitTable;
}
But in the implicit JTable, I'm still stuck with a 21x21 JTable. What I want is a JTable that automatically increases in size every time the user scrolls to the end of the table, i.e. you can never reach the end of the table. I suspect that I can't do this without adding ActionListeners to the JScrollPane scrollbar or something like that. I had hoped that there was some elegant, self-contained way to do it like there is for the JTree.
Am I being clear?, thanks for any help here,
marcia
cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
Hi,
Now I got it . It looks like you have to provide custom methods for JScrollPane - when scrolldown , a new row is added. The link gave may some hints about what is to be done, I hope it helps.
Before Java, I did MFC, and CScrollView had methods for getting the position of the cursor on the scroll bar.
http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html
This is where, I think, you should have a look. JScrollBar looks interesting.
I checked, you can add adjustment listener to the scroll bar.
Lin.
[This message has been edited by cata lin (edited August 08, 2000).]
[This message has been edited by cata lin (edited August 10, 2000).]
zulfiqar raza
Ranch Hand

Joined: Oct 18, 2000
Posts: 81

[This message has been edited by zulfiqar raza (edited March 15, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: self-generating JTable
 
Similar Threads
JTree & JTable
JTree and JTable and Database
DND from JTree to JTable
JTable
JTree