Skip to main content

JXTreeTable dynamic update

5 replies [Last post]
anandb4ever
Offline
Joined: 2010-07-19

Hi,

I have got a problem on this component : JXTreeTable. I want to change its data (add a row to top) and refresh this tree but I have no method to do that.

My JXTreeTable is describe by a model (extends AbstractTreeTableModel).

Any ideas????

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
greglev
Offline
Joined: 2010-08-20

I hope, can help you with something. the model is DefaultTreeTableModel. I dont know, how I get a format here.

[code]public class TestTreeTableFrame extends JFrame{

private JXTreeTable tb;
private TestTreeTableModel dm;
JButton addButton = new JButton("New...");

public TestTreeTableFrame(){
...
initComponents();
...
}
public void initComponents(){

tb = new JXTreeTable();
dm = new TestTreeTableModel();
tb.setTreeTableModel(dm);

addButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){

TreePath treePath = tb.getPathForRow(tb.getSelectedRow());
// add a new Node in root, when tree isn´t selected
if(treePath == null){
dm.addRow();
}else{// add a new Node, where a node in tree is selected
TestData newRow = new TestData(1, "aa");
dm.getData().add(newRow);

TestTreeNode tn = new TestTreeNode(newRow);
dm.insertNodeInto(tn,(MutableTreeTableNode) treePath.
getPathComponent(treePath.getPathCount()-1),
((MutableTreeTableNode)treePath.getPathComponent(
treePath.getPathCount()-1)).getChildCount());
//As a general rule, application code never ever calls updateUI()
//tb.updateUI()
}
}
});

}
}[/code]

[code]class TestTreeTableModel extends DefaultTreeTableModel {
// may be, you need the values of the treetable
private final List data = new ArrayList();

public TestTreeTableModel(){
super(new TestTreeNode());
}

@Override
public int getColumnCount(){
return ((TreeTableNode)this.root).getColumnCount();
}
@Override
public String getColumnName( int column ){
switch( column ){
case 0: return "TreeName";
case 1: return "Value";
case 2: return "Number of children";
default: return "Unknown";
}
}
@Override
public Class getColumnClass(int columnIndex){
switch (columnIndex){
case 0:
return String.class;
case 1:
return String.class;
case 2:
return Integer.class;
}
return super.getColumnClass(columnIndex);
}

public List getData(){
return data;
}
// add a new Node in root
//
public void addRow(){
TestData newRow = new TestData(1, "Value 1");
this.data.add(newRow);

MutableTreeTableNode rootNode = (MutableTreeTableNode)this.root;
TestTreeNode tn = new TestTreeNode(newRow);
int n = rootNode.getChildCount();
insertNodeInto(tn, rootNode, n);
}

@Override
public boolean isCellEditable(Object node, int column){
return true;
/**
only für column 2, you can write
if(column == 2)
return true;
return false;
**/
}
public MutableTreeTableNode getRoot(){
return (MutableTreeTableNode)this.root;
}
}[/code]

[code]public class TestData {
int number;
String name;
String value;
public TestData(int number, String name){
super();
this.number = number;
this.name= name;
this.value= "something";
}
}[/code]

Message was edited by: greglev

kleopatra
Offline
Joined: 2003-06-11

> I hope, can help you with something. the model is
> DefaultTreeTableModel. I dont know, how I get a
> format here.

you can format code by taggin it with [ code ] .... [ /code ] - without the spaces. Please complain to the site admin that this isn't documented anywhere ;-)

as to your help - it's well-meant but _wrong advice_ Children, don't follow the example:

>
>
>
> private JXTreeTable tb;
>
> tb.updateUI();
>
>

As a general rule, application code never ever calls updateUI(). If it looks like being necessary, than something is severely wrong. Most often that wrongness is a misbehaved model which doesn't fire the correct events. In your case, that's a missing fireChildInserted in addRow.

Cheers
Jeanette

greglev
Offline
Joined: 2010-08-20

Hi Jeanette, Thank you very much for the note, I've already corrected

kleopatra
Offline
Joined: 2003-06-11

great :-)

The last thing to do: would you please edit the code section to not exceed the "normal" width (simply add line breaks to the string comment)

Thanks
Jeanette

kleopatra
Offline
Joined: 2003-06-11

same as you would do in a plain JTree ;-) Add the node to the model and all will update automagically.

CU
Jeanette

PS: usually, the probabilty of getting an answer is inversely proportional to the number of question marks