In customizing tables and trees is the model is important, the definition is never blurred
Such as:
DefaultTableModel mode = new DefaultTableModel ();
JTable table = new JTable (mode);
It is difficult for the next tree to correspond with the table in one by one, and it is not conducive to the display in JScrollPane.
Here's a good way to do it, and it's easy to do it with a tree that's compatible with the table:
DefaultTableModel mode;
JTable table = new JTable ();
mode = (DefaultTableModel mode) Table.getmodel ();
In the next program, you can change the format of the table by simply using the model;
Example:
Package tabletree;
Import java.awt.*;
Import javax.swing.*;
Import javax.swing.event.TreeSelectionEvent;
Import Javax.swing.event.TreeSelectionListener;
Import Javax.swing.table.DefaultTableModel;
Import javax.swing.tree.*;
public class Tabletree extends JPanel implements Treeselectionlistener {
/**
* @param args
*/
JPanel Myjpanel;
JTable table;
JScrollPane myjstree,myjstable;
JTree Myjtree;
Defaultmutabletreenode root,temp;
Object Nodepath;
Defaulttreemodel rs;
String strpath;
int row,col;
Treeselectionmodel Selectionmodel;
DefaultTableModel model;
JFrame Myjframe;
Public Tabletree () {
Super ();
Myjframe = new JFrame ("treetable");
Table = new JTable ();
Model = (DefaultTableModel) Table.getmodel ();
This.setlayout (New BorderLayout ());
This.add (Table,borderlayout.center);
myjstable = new JScrollPane (this);
Myjframe.add (Myjstable,borderlayout.center);
root = new Defaultmutabletreenode ("root");
temp = new Defaultmutabletreenode ("father");
Temp.add (New Defaultmutabletreenode ("Chirld1"));
Temp.add (New Defaultmutabletreenode ("Chirld2"));
Temp.add (New Defaultmutabletreenode ("chirld3"));
Temp.add (New Defaultmutabletreenode ("Chirld4"));
Temp.add (New Defaultmutabletreenode ("Chirld5"));
Root.add (temp);
rs = new Defaulttreemodel (root);
Myjtree = new JTree (RS);
Selectionmodel = Myjtree.getselectionmodel ();
Selectionmodel.setselectionmode (treeselectionmodel.single_tree_selection);
Myjtree.addtreeselectionlistener (this);
Myjstree = new JScrollPane (myjtree);
Myjframe.add (myjstree,borderlayout.west);
Myjframe.setsize (550,400);
Myjframe.setlocation (150, 150);
Myjframe.setvisible (TRUE);
Myjframe.setdefaultcloseoperation (Jframe.exit_on_close);
}
Java Tree and table experience