1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
public class CommentTreeModel implements TreeModel
{
private String [] tab = null;
public CommentTreeModel(String [] tabls)
{
tab = tabls;
}
public Object getRoot()
{
return tab;
}
public Object getChild(Object parent, int index)
{
if (parent == null)
return (String) "Commentaire";
else
return (String) tab[index];
}
public int getChildCount(Object parent)
{
return tab.length;
}
public boolean isLeaf(Object node)
{
if (node == tab)
return false;
else
return true;
}
public void valueForPathChanged(TreePath path, Object newValue)
{
//rien
}
public int getIndexOfChild(Object parent, Object child)
{
int index = 0;
for (int i=0; i<tab.length; i++)
{
if (tab[i].equals((String)parent))
{
index = i;
break;
}
}
return index;
}
public void addTreeModelListener(TreeModelListener l)
{
//rien
}
public void removeTreeModelListener(TreeModelListener l)
{
//rien
}
public static void main(String [] args)
{
JFrame f = new JFrame("");
f.setSize(500,500);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTree t = new JTree();
String [] s = {"Hello","Bonjour","Au revoir"};
t.setModel(new CommentTreeModel(s));
t.setOpaque(true);
JPanel p = new JPanel(new BorderLayout());
p.add(t);
f.getContentPane().add(p);
f.setVisible(true);
}
} |
Partager