Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
04-12-2010, 03:30 AM
#1
Senior Member
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
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
04-18-2010, 03:05 AM
#2
Senior Member
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Original Poster
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 11:40 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News