LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Unable to check the box - what did I miss, tnx 4 helping out (http://www.linuxquestions.org/questions/programming-9/unable-to-check-the-box-what-did-i-miss-tnx-4-helping-out-801489/)

Thor_2.0 04-12-2010 03:30 AM

Unable to check the box - what did I miss, tnx 4 helping out
 
Hi,

I'm messing with a JTree, I need one that has checkboxes to check/uncheck options, and (later) textboxes to edit options....but for now, the checkboxes dont wanna play.

Here's the source:

Quote:

package Habitat.EnvCtrl;

import java.awt.BorderLayout;import MailPopper.hlprs.CheckBoxNode;
import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

public class PalmTree extends JInternalFrame
{
// private declares
private JTree myTree;
private JDesktopPane jd;
private JScrollPane scroller;
private TreeNode dmtn;
private CheckBoxNodeRenderer cbr = null;

public PalmTree()
{
makeInterface();
}

private void makeInterface()
{
this.setSize(new Dimension(400,400));
this.setPreferredSize(new Dimension(400,400));

cbr = new CheckBoxNodeRenderer();
myTree = new JTree(getTree());
myTree.setCellRenderer(cbr);
myTree.setCellEditor(new CheckBoxNodeEditor(myTree));
myTree.setEditable(true);

jd = new JDesktopPane();
jd.setLocation(0,0);
jd.setLayout(null);
jd.setSize(new Dimension(400,400));
jd.setPreferredSize(new Dimension(400,400));
scroller = new JScrollPane(myTree);
scroller.setSize(new Dimension(400,390));
scroller.setPreferredSize(new Dimension(400,400));
jd.add(scroller);
scroller.setLocation(0,0);
jd.repaint();
this.getContentPane().add(jd);
this.repaint();

}

private DefaultMutableTreeNode getTree()
{
// declares
DefaultMutableTreeNode myTreeToReturn = null;
DefaultMutableTreeNode nodeToAdd = null;
DefaultMutableTreeNode othernode = null;
CheckBoxNode cbn = null;

myTreeToReturn = new DefaultMutableTreeNode("Dome");

nodeToAdd = new DefaultMutableTreeNode("Habitat1");
myTreeToReturn.add(nodeToAdd);
//othernode = new DefaultMutableTreeNode("Access allowed?");
othernode = new CheckBoxNode("Access allowed", false);
nodeToAdd.add(othernode);
othernode = new DefaultMutableTreeNode(getSubTree("Desert", nodeToAdd));
nodeToAdd.add(othernode);
myTreeToReturn.add(nodeToAdd);

nodeToAdd = new DefaultMutableTreeNode("Habitat2");
othernode = new DefaultMutableTreeNode("Access allowed?");
nodeToAdd.add(othernode);
othernode = new DefaultMutableTreeNode(getSubTree("Savannah", nodeToAdd));
nodeToAdd.add(othernode);
myTreeToReturn.add(nodeToAdd);

nodeToAdd = new DefaultMutableTreeNode("Habitat3");
othernode = new DefaultMutableTreeNode("Access allowed?");
nodeToAdd.add(othernode);
othernode = new DefaultMutableTreeNode(getSubTree("Swamp", nodeToAdd));
nodeToAdd.add(othernode);
myTreeToReturn.add(nodeToAdd);

return(myTreeToReturn);
}

private DefaultMutableTreeNode getSubTree(String s, DefaultMutableTreeNode momNode)
{
// declares
DefaultMutableTreeNode myTreeToReturn = null;
DefaultMutableTreeNode nodeToAdd = null;
DefaultMutableTreeNode othernode = null;

//othernode = new DefaultMutableTreeNode("Control module");
//momNode.add(othernode);
// nodes under that new node
othernode = new DefaultMutableTreeNode("Actuators");
nodeToAdd = new DefaultMutableTreeNode("A1:" + s);
othernode.add(nodeToAdd);
nodeToAdd = new DefaultMutableTreeNode("A2:" + s);
othernode.add(nodeToAdd);
nodeToAdd = new DefaultMutableTreeNode("A3:" + s);
othernode.add(nodeToAdd);
momNode.add(othernode);
return(momNode);
}

}
-----------|cut here|----------8<---------------------------
package Habitat.EnvCtrl;

import java.util.Vector;

import javax.swing.tree.DefaultMutableTreeNode;

class CheckBoxNode extends DefaultMutableTreeNode
{
String text;

boolean selected;

public CheckBoxNode(String text, boolean selected) {
this.text = text;
this.selected = selected;
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean newValue) {
selected = newValue;
}

public String getText() {
return text;
}

public void setText(String newValue) {
text = newValue;
}

public String toString() {
return getClass().getName() + "[" + text + "/" + selected + "]";
}
}

class NamedVector extends Vector {
String name;

public NamedVector(String name) {
this.name = name;
}

public NamedVector(String name, Object elements[]) {
this.name = name;
for (int i = 0, n = elements.length; i < n; i++) {
add(elements[i]);
}
}

public String toString() {
return "[" + name + "]";
}
}
-----------|cut here|----------8<---------------------------
package Habitat.EnvCtrl;

import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.event.ChangeEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreePath;

class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {

CheckBoxNodeRenderer renderer = new CheckBoxNodeRenderer();

ChangeEvent changeEvent = null;

JTree tree;

public CheckBoxNodeEditor(JTree tree)
{
this.tree = tree;
}

public Object getCellEditorValue() {
JCheckBox checkbox = renderer.getLeafRenderer();
CheckBoxNode checkBoxNode = new CheckBoxNode(checkbox.getText(),
checkbox.isSelected());
return checkBoxNode;
}

public boolean isCellEditable(EventObject event) {
boolean returnValue = false;
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
TreePath path = tree.getPathForLocation(mouseEvent.getX(),
mouseEvent.getY());
if (path != null) {
Object node = path.getLastPathComponent();
if ((node != null) && (node instanceof DefaultMutableTreeNode)) {
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
Object userObject = treeNode.getUserObject();
returnValue = ((treeNode.isLeaf()) && (userObject instanceof CheckBoxNode));
}
}
}
return returnValue;
}

public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row) {

Component editor = renderer.getTreeCellRendererComponent(tree, value,
true, expanded, leaf, row, true);

// editor always selected / focused
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
if (stopCellEditing()) {
fireEditingStopped();
}
}

/*
@Override
/*
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub

}
*/
};
if (editor instanceof JCheckBox) {
((JCheckBox) editor).addItemListener(itemListener);
}

return editor;
}

/*
@Override
public Component getTreeCellEditorComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row) {
// TODO Auto-generated method stub
return null;
}
*/
}
The rtree builds (albeit I have a superfluous extra checkbox in there I dont know where it comes from...) but I cannot check/uncheck any boxes.

Any tips? Hints?

Tnx!!

Thor

Thor_2.0 04-18-2010 03:05 AM

Hi,

Found the answer...All I had to do was add a CheckBoxNode to the JTree instead of a DefaultMutableTreeNode and set up a proper listener...

Tnx for reading and (if possible) considering an answer.

Wellness to all

Thor


All times are GMT -5. The time now is 02:46 AM.