Skip to main content

A plea for help (JXTreeTable)

8 replies [Last post]
ruanae
Offline
Joined: 2006-10-23
Points: 0

Hi

I had some code that was displaying a JXTreeTable sucessfully with a custom data object holding the non-hierarchical data. I went to tweak it so it would load data from an sql query and now it's stopped working. I've tried goign back to my previous version but I cant get that to work either. I *think* the problem is with my method used to populate the nodes.

<br />
public void createNodes(DefaultMutableTreeNode top)<br />
	{<br />
	    DefaultMutableTreeNode category = null;<br />
	    DefaultMutableTreeNode data = null;</p>
<p>ImageIcon campaignRed = new ImageIcon(getClass().getResource("Images/Status_Icons/campaigns_and_surveys_red.jpg"));<br />
	    ImageIcon campaignGreen = new ImageIcon(getClass().getResource("Images/Status_Icons/campaigns_and_surveys_green.jpg"));<br />
	    ImageIcon campaignBlue = new ImageIcon(getClass().getResource("Images/Status_Icons/campaigns_and_surveys_blue.jpg"));<br />
	    ImageIcon capitalRed = new ImageIcon(getClass().getResource("Images/Status_Icons/capital_works_red.jpg"));<br />
	    ImageIcon capitalBlue = new ImageIcon(getClass().getResource("Images/Status_Icons/capital_works_blue.jpg"));<br />
	    ImageIcon capitalGreen = new ImageIcon(getClass().getResource("Images/Status_Icons/capital_works_green.jpg"));<br />
	    ImageIcon customerRed = new ImageIcon(getClass().getResource("Images/Status_Icons/customer_issues_red.jpg"));<br />
	    ImageIcon customerBlue = new ImageIcon(getClass().getResource("Images/Status_Icons/customer_issues_blue.jpg"));<br />
	    ImageIcon customerGreen = new ImageIcon(getClass().getResource("Images/Status_Icons/customer_issues_green.jpg"));</p>
<p>	    ImageIcon blank = new ImageIcon("");</p>
<p>	    CompoundIcon singleTestIcon = new CompoundIcon(campaignGreen,blank,0);</p>
<p>	    CompoundIcon testIcon =  new CompoundIcon(campaignRed,capitalRed,3);<br />
	    testIcon = new CompoundIcon(testIcon,customerBlue,3);<br />
	    iconList.add(testIcon);</p>
<p>	    CompoundIcon testIcon2 =  new CompoundIcon(campaignBlue,capitalRed,3);<br />
	    testIcon2 = new CompoundIcon(testIcon2,customerGreen,3);<br />
	    iconList.add(testIcon2);<br />
	    CompoundIcon testIcon3 =  new CompoundIcon(campaignGreen,capitalGreen,3);<br />
	    testIcon3 = new CompoundIcon(testIcon3,customerRed,3);<br />
	    iconList.add(testIcon3);<br />
	    CompoundIcon testIcon4 =  new CompoundIcon(campaignGreen,capitalBlue,3);<br />
	    testIcon4 = new CompoundIcon(testIcon4,customerBlue,3);<br />
	    iconList.add(testIcon4);</p>
<p>	    category = new DefaultMutableTreeNode(new TableData("Information",new Object[]{singleTestIcon,testIcon,testIcon2,testIcon3,testIcon4}));<br />
	    top.add(category);<br />
	    data = new DefaultMutableTreeNode(new TableData("Mains Repairs",new Object[]{"Hello","does","this","work","?"}));<br />
	    category.add(data);<br />
	    data = new DefaultMutableTreeNode(new TableData("UGSP Repairs",new String[]{"In","theory","it","should"}));<br />
	    category.add(data);<br />
	    data = new DefaultMutableTreeNode(new TableData("Contacts - Nil Supply",new String[]{"Testing","if","this","creates","the","correct","number","of","columns"}));<br />
	    category.add(data);<br />
	    category = new DefaultMutableTreeNode(new TableData("More Information", new Object[]{testIcon3,testIcon2,testIcon}));<br />
	    top.add(category);<br />
	    data = new DefaultMutableTreeNode(new TableData("Row 4",new Object[]{"Checking","that","this","works"}));<br />
	    category.add(data);<br />
	}<br />

and I know it needs a "root" node to add catergory to but if i pass it the root node from my instance of the treetable
(using

<br />
createNodes((DefaultMutableTreeNode) treeTable.getTreeTableModel().getRoot());<br />

I either get a null pointer exception or a blank table with no rows.

Help? Please?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ruanae
Offline
Joined: 2006-10-23
Points: 0

Okay, so I finally got the code to display some data, I did this by calling my "create nodes" method with a default mutable treenode before passibg that node to teh constructor of the JXTreeTable. So its noe displayign stuff on screen which is a marked improvement.

Whether this will work for a dynamically generated/alterable table I dont know. I may be back with some more questions!

Thanks for the help :-)

jorgenrapp
Offline
Joined: 2004-08-02
Points: 0

OK so from you example above it seems enough to expand the invisible root node.
TreeTable.expandPath(new TreePath(TreeTable.getTreeTableModel().getRoot()));

A more general note on inserting nodes is that you need to notify the model like:
parentNode.add(child1);
parentNode.add(child2);
int[] childIndices = new int[2];
childIndices[0]=0;
childIndices[1]=1;
((DefaultTreeTableModel)TreeTable.getTreeTableModel()).nodesWereInserted((DefaultMutableTreeNode)TreeTable.getTreeTableModel().getRoot(), childIndices);

The same is true for removing nodes...

see this thread for an example of dynamic node handling:

http://forums.java.net/jive/thread.jspa?threadID=13194&messageID=85862#8...

/J

ruanae
Offline
Joined: 2006-10-23
Points: 0

Hi

Thanks for your help.

I'm having a slight problem sending a notification of inserting nodes.

Using

[code]
topNode.add(category);
int[] childIndices = new int[1];
childIndices[0]=0;
((DefaultTreeTableModel)treeTable.getTreeTableModel()).nodesWereInserted(topNode, childIndices);
[/code]

throws a null pointer exception. And I can't figure out why.

Could anyone help?

Thanks in advance :-)

Edit: After a lot of debugging, the problem turned out to be that the variable treeTable was null, not quite sure why. Replacing it with "this" instead solved the problem. Thanks for your help :)

Message was edited by: ruanae

ruanae
Offline
Joined: 2006-10-23
Points: 0

Okay, trying to get it to work with the basic DefaultTreeTableModel I created the following small runnable version:

[code]
import java.awt.Dimension;

import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jdesktop.swingx.JXTreeTable;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;

public class TreeTable {

public JXTreeTable treeTable;
public static JScrollPane scrollPane;

public TreeTable(DefaultMutableTreeNode top)
{

TreeTableModel treeTableModel = new DefaultTreeTableModel(top);
treeTable = new JXTreeTable(treeTableModel);

createNodes(treeTable);

for(int i =1; i<=10; i++)
{
treeTable.addColumn(new TableColumn(i,100));
treeTable.getTableHeader().setReorderingAllowed(false);
}
treeTable.getColumn(0).setPreferredWidth(150);
treeTable.getColumn(0).setHeaderValue("Week Commencing");
scrollPane = new JScrollPane(treeTable);
scrollPane.setPreferredSize(new Dimension(800,400));

}

public void createNodes(JXTreeTable TreeTable)
{

DefaultMutableTreeNode category = null;
DefaultMutableTreeNode data = null;

category = new DefaultMutableTreeNode("Information");
((DefaultMutableTreeNode) TreeTable.getTreeTableModel().getRoot()).add(category);
data = new DefaultMutableTreeNode("Mains Repairs");
category.add(data);
data = new DefaultMutableTreeNode("UGSP Repairs");
category.add(data);
data = new DefaultMutableTreeNode("Contacts - Nil Supply");
category.add(data);
category = new DefaultMutableTreeNode("More Information");
((DefaultMutableTreeNode) TreeTable.getTreeTableModel().getRoot()).add(category);
data = new DefaultMutableTreeNode("Row 4");
category.add(data);

}
}
[/code]

displayed in a window created by

[code]
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.tree.DefaultMutableTreeNode;

public class TestWindow extends JComponent {

public static JFrame mainFrame = new JFrame();
public TreeTable treeTable;

public TestWindow()
{

DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Hello?");

treeTable = new TreeTable(rootNode);

mainFrame.setLayout(new BorderLayout());
mainFrame.add(TreeTable.scrollPane, BorderLayout.CENTER);
mainFrame.setPreferredSize(new Dimension(1250,500));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
}

public static void main (String[] args)
{
new TestWindow();
mainFrame.pack();
mainFrame.setVisible(true);
}

}
[/code]

But which displays a completely empty table. What am I doing wrong?

Thanks

ruanae
Offline
Joined: 2006-10-23
Points: 0

After a bit more investigation (System.out.println is my friend) it appears that the getValueAt method for my custom tree table model is not being called at anypoint.

Not quite sure why.

Again, any ideas? Please?

Thanks

Kleopatra

jdnc-interest@javadesktop.org wrote:
> Hi
>
> I had some code that was displaying a JXTreeTable sucessfully with a custom data object holding the non-hierarchical data. I went to tweak it so it would load data from an sql query and now it's stopped working. I've tried goign back to my previous version but I cant get that to work either.

which tells me that you didn't use a versioning tool

> I *think* the problem is with my method used to populate the nodes.
>

[ code snipped ]

can't see anything glaring - try to make it work with a plain ol' JTree
(the TreeTableModel _is_ a TreeModel). If that's working and using the
same in a JXTreeTable doesn work, you are welcome to come back with a
small (!) runnable (!) example - there might be a problem with
JXTreeTable and maybe somebody (not necessarily me :-) has seen it and
can help.

Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

ruanae
Offline
Joined: 2006-10-23
Points: 0

> jdnc-interest@javadesktop.org wrote:
> > Hi
> >
> > I had some code that was displaying a JXTreeTable
> sucessfully with a custom data object holding the
> non-hierarchical data. I went to tweak it so it would
> load data from an sql query and now it's stopped
> working. I've tried goign back to my previous version
> but I cant get that to work either.
>
> which tells me that you didn't use a versioning tool
>
>

Unfortunatly I dont have one available. I thought i had commented out bits rather than deletign them, but apparantly not...

> > I *think* the problem is with my method used to
> o populate the nodes.
> >
>
> [ code snipped ]
>
> can't see anything glaring - try to make it work with
> a plain ol' JTree
> (the TreeTableModel _is_ a TreeModel). If that's
> working and using the
> same in a JXTreeTable doesn work, you are welcome to
> come back with a
> small (!) runnable (!) example - there might be a
> problem with
> JXTreeTable and maybe somebody (not necessarily me
> :-) has seen it and
> can help.
>
> Jeanette

The problem is coming up with a small runnable example, it all kinda depends on other stuff, which in turn depends on stuff etc etc. I'll see what I can do.

WHat i have found out is that apparantly the child nodes are being added to the root, the arrays making up the custom data structure have data in and i'm no logner getting a null pointer exception. What I'm not geting is anything displayed in my table...

Any ideas?

Kleopatra

jdnc-interest@javadesktop.org wrote:

>
> The problem is coming up with a small runnable example, it all kinda depends on other stuff, which in turn depends on stuff etc etc. I'll see what I can do.
>

on the bright side: you'll learn a lot in the process, maybe even find
the exact reason for your problem :-) Strip everything down to the
barest minimum, my guess is that it's somewhere in your custom model.

Good luck - my day's over right now
Jeanette

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net