How to create a JTree from DefaultTableModel?

Dear friends:

I have a DefaultTableModel in following code, I can create a JTable from this DefaultTableModel , But I hope to create a JTree from this DefaultTableModel also, How can I do it?

I tried a lot of way to use

myTree= new JTree(model.addRow(rowData));

etc to create a JTree, but fails, what is wrong?

Any suggestion?

Thanks

sunny

package com.com;

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JTree;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.event.ListSelectionEvent;

import javax.swing.event.ListSelectionListener;

import javax.swing.table.DefaultTableModel;

publicclass ModelJTable0extends JPanel{

protected DefaultTableModel model;

protected JTable tbl1, tbl2;

JTree myTree;

protectedString[] columnNames ={

"First Name",

"Last Name",

"Sport",

"# of Years",

"Vegetarian"};

protected Object[][] data ={

{"Socrates","","469-399 B.C."},

{"Thomas","Aquinas","1225-1274"},

{"Soren","Kierkegaard","1813-1855"},

{"Immanuel","Kant","1724-1804"},

{"Friedrich","Nietzsche","1844-1900"},

{"Hannah","Arendt","1906-1975"}};

public ModelJTable0(){

super();

model =new DefaultTableModel();

for (int i = 0; i < columnNames.length; i++ ){

model.addColumn(columnNames[i]);

}

for (int i = 0; i < data.length; i++ ){

model.addRow(data[i]);

}

tbl1 =new JTable(model);

tbl2 =new JTable(model);

myTree=new JTree(model.addRow(rowData));

JButton addButton =new JButton("Add");

addButton.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

String[] philosopher ={"","",""};

model.addRow(philosopher);

}

});

JButton removeButton =new JButton("Remove");

removeButton.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent event){

model.removeRow(tbl1.getSelectedRow());

}

});

JPanel inputPanel =new JPanel();

inputPanel.add(addButton);

inputPanel.add(removeButton);

JScrollPane scr1 =new JScrollPane(tbl1);

JScrollPane scr2 =new JScrollPane(tbl2);

JPanel pnl =new JPanel();

pnl.setMinimumSize(new Dimension(200,300));

tbl1.getSelectionModel().addListSelectionListener(new ListSelectionListener()

{

publicvoid valueChanged(ListSelectionEvent lse)

{

if (tbl1.getSelectedRow()!=-1)

tbl2.clearSelection();

//revalidate();

}

}

);

tbl2.getSelectionModel().addListSelectionListener(new ListSelectionListener()

{

publicvoid valueChanged(ListSelectionEvent lse)

{

if (tbl2.getSelectedRow()!=-1)

tbl1.clearSelection();

//revalidate();

}

}

);

//pnl.setLayout(new FlowLayout());

pnl.setLayout(new BorderLayout());

pnl.add(scr1, BorderLayout.WEST);

pnl.add(scr2, BorderLayout.CENTER);

pnl.add(inputPanel, BorderLayout.NORTH);

this.add(pnl);

}

publicstaticvoid main(String args[]){

ModelJTable0 c =new ModelJTable0();

JFrame frm =new JFrame();

frm.setSize(920,300);

frm.getContentPane().add(c);

frm.setVisible(true);

}

}

sunnymanman

Message was edited by:

sunnymanman

Message was edited by:

sunnymanman

[7687 byte] By [sunnymanmana] at [2007-11-26 22:37:55]
# 1
You're actually using a 'void' return value as a tree model so that clearly won't work, and in any case a table model is not a tree model. You need to place your data in a tree model.Or - as Nietzsche would put it - give up, it's all pointless :o)
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hello,

i don't have the time to read your code now but i guess the best would be, if you created you own Model implementing both, the TreeModel and TableModel interfaces. So you have table or treeview on your data.

Implementing a own model often looks scary but its easy and way less affort than doing complicated stuff with DefaultTree/Table models

jEti182a at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 3
Yes, what I should have said is that DefaultTableModel is not a TreeModel.
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks, so what kind of TableModel can be accepted by both JTree and JTable so that they can share the same DataModel?
sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 5
A model which implements both TreeModel and TableModel. There is no such class in the JDK, you would have to write it. Or you could write an Adapter pattern class to convert one to the other.
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thanks, ic, actually I only need to craete a JTree that extracts the First Column of this JTable, not all columns, so any other way to do it?Thanks
sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 7
Yes, put the same data in a tree model. As was suggested a while ago :o)
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 8

Yes, I follow you,

I create following code to craete this tree:

MutableTreeNode root = new DefaultMutableTreeNode(columnNames[0]);

MutableTreeNode[] bNode =null;

for (int i = 0; i<data.length; i++ ) {

bNode[i] = new DefaultMutableTreeNode(data[i]);

root.insert(bNode[i], i);

}

final DefaultTreeModel model1 = new DefaultTreeModel(root);

final JTree tree = new JTree(model1);

but cannot run, it said

Exception in thread "main" java.lang.NullPointerException

at com.com.ModelJTable0.><init>(ModelJTable0.java:72)

at com.com.ModelJTable0.main(ModelJTable0.java:169)

what is wrong?

thanks

sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 9
Might help if you were to point out which line is number 72.
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 10
bNode = new DefaultMutableTreeNode(data);is line No 72Message was edited by: sunnymanman
sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 11
ChangeMutableTreeNode[] bNode =null;toMutableTreeNode[] bNode = new MutableTreeNode[data.length];
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 12
great!! success!but I have one more question, how can I let Jtree and this JTable to share same data, I mean once JTable is updated/deleted/inserted with new row, then Jtree here will be reflected automatically?Really appreciated
sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 13
You can use a TableModelEvent generated by the table model.
itchyscratchya at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 14
I google for a while, still confused how to do it, can you give me some more details?Thanks
sunnymanmana at 2007-7-10 11:49:24 > top of Java-index,Desktop,Core GUI APIs...
# 15
Not sure what you were googling for but this was the first result of my first search: http://java.sun.com/docs/books/tutorial/uiswing/events/tablemodellistener.html
itchyscratchya at 2007-7-21 19:09:35 > top of Java-index,Desktop,Core GUI APIs...
# 16
Thanks a lot, I really found this one, nut i missed last one.Message was edited by: sunnymanman
sunnymanmana at 2007-7-21 19:09:35 > top of Java-index,Desktop,Core GUI APIs...