Hallo
Mein JTree möchte sich einfach nicht korrekt anzeigen lassen. Momentan sieht das so aus:
[ ] [Cyberware, Bioware]
|
| <----- Hier ist einfach eine Zeile leer, wenn man draufklickt ändert sich untere Zeile zu Cyberware
|
+-- Bioware
Den Code dazu ist hier:
Panel mit JTree:
Der Code für das TreeModel:
Es werden garantiert keine Null-Objekte an den Tree geliefert. Was habe ich falsch gemacht?
Vielen Dank im Voraus
Mein JTree möchte sich einfach nicht korrekt anzeigen lassen. Momentan sieht das so aus:
[ ] [Cyberware, Bioware]
|
| <----- Hier ist einfach eine Zeile leer, wenn man draufklickt ändert sich untere Zeile zu Cyberware
|
+-- Bioware
Den Code dazu ist hier:
Panel mit JTree:
Code:
public class SRImplantListPanel extends JPanel {
private JButton addButton;
private JButton upButton;
private JButton downButton;
private JPanel implantEditPanel;
private JTree tree;
private SRImplantsTreeModel treeModel;
private JFrame parent;
private SRImplantsList implants;
/**
*
*/
public SRImplantListPanel(JFrame parent, SRImplantsList implants) {
super(new BorderLayout());
this.parent = parent;
this.implants = implants;
this.setName(I18N.getString(I18N.getPanelsResourceBundle(),
"implantsListPanel"));
initComponents();
addComponents();
addListeners();
}
private void initComponents() {
addButton = I18N.createJButton("add");
upButton = new JButton();
upButton.setIcon(IconLoader
.loadIcon("toolbarButtonGraphics/navigation/Up16.gif"));
downButton = new JButton();
downButton.setIcon(IconLoader
.loadIcon("toolbarButtonGraphics/navigation/Down16.gif"));
treeModel = new SRImplantsTreeModel(implants);
tree = new JTree();
tree.setModel(treeModel);
tree.setRootVisible(true);
implantEditPanel = new JPanel(new GridLayout(1, 1));
}
private void addComponents() {
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
new JScrollPane(tree), new JScrollPane(implantEditPanel));
this.add(splitPane);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(addButton);
this.add(buttonPanel, BorderLayout.SOUTH);
JPanel navigationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
Box box = new Box(BoxLayout.Y_AXIS);
box.add(Box.createVerticalGlue());
box.add(upButton);
box.add(downButton);
box.add(Box.createVerticalGlue());
navigationPanel.add(box);
this.add(box, BorderLayout.WEST);
}
private void addListeners() {
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addItem();
}
});
upButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setItemPosition(-1);
}
});
downButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setItemPosition(+1);
}
});
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
Object node = tree.getLastSelectedPathComponent();
if (node == null || node instanceof SRImplantsList) return;
if (tree.getSelectionPath().getPathCount()>1){
Object parent = tree.getSelectionPath().getPathComponent(tree.getSelectionPath().getPathCount() - 2);
editItem(node, parent);
}
}
});
}
private void setItemPosition(int position) {
treeModel.move(tree.getSelectionPath(), position);
}
private void editItem(Object item, Object parent) {
if (item instanceof SRImplant) {
implantEditPanel.removeAll();
SRImplantEditPanel panel;
if (parent instanceof SRImplantGroup) {
panel = new SRImplantEditPanel((SRImplant) item,
(SRImplantGroup) parent);
} else if (parent instanceof SRImplant) {
panel = new SRImplantEditPanel((SRImplant) item,
((SRImplant) parent).getContainedImplants());
} else {
return;
}
implantEditPanel.add(panel);
implantEditPanel.validate();
addImplantEditPanelListeners(panel);
} else if (item instanceof SRImplantGroup) {
implantEditPanel.removeAll();
implantEditPanel.validate();
implantEditPanel.repaint();
}
}
private void addImplantEditPanelListeners(SRImplantEditPanel panel) {
panel.removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Object node = tree.getLastSelectedPathComponent();
Object parent = tree.getSelectionPath().getPathComponent(
tree.getSelectionPath().getPathCount() - 2);
if (parent instanceof SRImplant) {
((SRImplant) parent).getContainedImplants().remove(node);
} else if (parent instanceof SRImplantGroup) {
((SRImplantGroup) parent).remove(node);
}
implantEditPanel.removeAll();
implantEditPanel.validate();
implantEditPanel.repaint();
}
});
panel.saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
}
private void addItem() {
Object parent = tree.getLastSelectedPathComponent();
SRImplant implant = treeModel.addImplant(tree.getSelectionPath());
editItem(implant, parent);
}
}
Der Code für das TreeModel:
Code:
public class SRImplantsTreeModel implements TreeModel {
private Vector treeModelListeners = new Vector();
private SRImplantsList implants;
public SRImplantsTreeModel(SRImplantsList implants) {
this.implants = implants;
}
//////////////// Fire events //////////////////////////////////////////////
/**
* The event is TreeStructureChanged with the root as path, i.e. the whole
* tree has changed.
*/
protected void fireTreeStructureChanged(SRImplantsList implants) {
int len = treeModelListeners.size();
TreeModelEvent e = new TreeModelEvent(this,
new Object[] { this.implants});
for (int i = 0; i < len; i++) {
((TreeModelListener) treeModelListeners.elementAt(i))
.treeStructureChanged(e);
}
this.implants = implants;
}
protected void fireTreeStructureChanged(TreePath path) {
int len = treeModelListeners.size();
TreeModelEvent e = new TreeModelEvent(this, path);
for (int i = 0; i < len; i++) {
((TreeModelListener) treeModelListeners.elementAt(i))
.treeStructureChanged(e);
}
}
//////////////// TreeModel interface implementation ///////////////////////
/**
* Adds a listener for the TreeModelEvent posted after the tree changes.
*/
public void addTreeModelListener(TreeModelListener l) {
treeModelListeners.addElement(l);
}
/**
* Returns the child of parent at index index in the parent's child array.
*/
public Object getChild(Object parent, int index) {
Object object = "Error";
if (parent instanceof SRImplantTreeObject) {
object = ((SRImplantTreeObject) parent).get(index);
} else if (parent instanceof SRImplantsList) {
object = ((SRImplantsList) parent).get(index);
}
return object;
}
/**
* Returns the number of children of parent.
*/
public int getChildCount(Object parent) {
if (parent instanceof SRImplantTreeObject) {
return ((SRImplantTreeObject) parent).getSize();
} else {
return ((SRImplantsList) parent).size() ;
}
}
/**
* Returns the index of child in parent.
*/
public int getIndexOfChild(Object parent, Object child) {
if (parent instanceof SRImplantTreeObject) {
return -1; //((SRImplantTreeObject) parent).indexOf(child);
} else {
return ((SRImplantsList) parent).indexOf(child);
}
}
/**
* Returns the root of the tree.
*/
public Object getRoot() {
return implants;
}
/**
* Returns true if node is a leaf.
*/
public boolean isLeaf(Object node) {
if (node instanceof SRImplantTreeObject) {
return (((SRImplantTreeObject) node).getSize() == 0);
} else {
return false;
}
}
/**
* Removes a listener previously added with addTreeModelListener().
*/
public void removeTreeModelListener(TreeModelListener l) {
treeModelListeners.removeElement(l);
}
/**
* Messaged when the user has altered the value for the item identified by
* path to newValue. Not used by this model.
*/
public void valueForPathChanged(TreePath path, Object newValue) {
System.out.println("*** valueForPathChanged : " + path + " --> "
+ newValue);
if (newValue instanceof String) System.out.println("Yeah!!");
}
/**
* @author schill
*/
public SRImplant addImplant(TreePath path) {
SRImplant implant = ((SRImplantTreeObject) path.getLastPathComponent())
.addNewImplant();
this.fireTreeStructureChanged(implants);
return implant;
}
/**
* @author schill
*/
public void move(TreePath path, int position) {
((SRImplantTreeObject) path.getPathComponent(path.getPathCount() - 2))
.move((SRImplantTreeObject) path.getLastPathComponent(),
position);
this.fireTreeStructureChanged(path);
}
}
Es werden garantiert keine Null-Objekte an den Tree geliefert. Was habe ich falsch gemacht?
Vielen Dank im Voraus