LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-12-2010, 03:30 AM   #1
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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
 
Old 04-18-2010, 03:05 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766

Original Poster
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
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
 
  


Reply

Tags
java



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kde 4.4 beta available for testing - tnx vbatts :) ponce Slackware 1 12-09-2009 12:45 AM
SQL - need some help, tnx ButterflyMelissa Programming 3 08-30-2009 01:57 PM
Unable to check audio device box in user preferences Cydge Linux - Newbie 0 07-11-2009 03:13 PM
LXer: Check if Compiz will run well on your Linux Box with Compiz Check LXer Syndicated Linux News 0 05-05-2008 01:20 AM
cant run startx... pls help... tnx in advance kublador Linux - Newbie 1 08-16-2002 04:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration