Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-11-2008, 10:03 AM
Member
 
Join Date: Nov 2008
Posts: 1
Rep Power: 0
partha_adt is on a distinguished road
Default Regarding JTree with JCheckBox and JRadioButton
Everyone,
I am a new one for swing application.
I have to construct a JTree with JCheckBox and JRadioButton.If we select the JRadioButton at that time only one node is selected.If we select the JCheckBox we select more than one.I have attached my code.In my code I did not write for selecting the node.In my code pp1have one child i need to modify that JRadioButton to JCheckBox.And pp2 have two childs.I need to modify the pp4 JRadioButton into JCheckBox.can u please resolve it?.Otherwise send the code.

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class TreeSample implements TreeSelectionListener{

public static JTree tree;
public static JScrollPane scrollPane;
public TreePath treePath;
public static JFrame frame;

public TreeSample(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
new TreeSample().treeCreation();
frame = new JFrame("Tree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.getContentPane().add(scrollPane);
frame.setSize(250, 250);
frame.show();
}

private JTree treeCreation() {
DefaultMutableTreeNode pp= new DefaultMutableTreeNode("pp",true);
DefaultMutableTreeNode pp1= new DefaultMutableTreeNode("pp1",true);
DefaultMutableTreeNode pp2= new DefaultMutableTreeNode("pp2",true);
DefaultMutableTreeNode pp3= new DefaultMutableTreeNode("pp3",true);
DefaultMutableTreeNode pp4= new DefaultMutableTreeNode("pp4",true);
DefaultMutableTreeNode pp5= new DefaultMutableTreeNode("pp5",true);
DefaultMutableTreeNode pp6= new DefaultMutableTreeNode("pp6",true);

pp.insert(pp1,0);
pp.insert(pp2,1);
pp1.add(pp3);
pp2.insert(pp4,0);
pp2.insert(pp5,1);
pp5.add(pp6);
tree = new JTree(configuration);
scrollPane = new JScrollPane(tree);
CheckBoxRender render = new CheckBoxRender();
tree.setCellRenderer(render);
//tree.addTreeSelectionListener(this);
return tree;
}

public void valueChanged(TreeSelectionEvent event) {
treePath = event.getNewLeadSelectionPath();
System.out.println(treePath);
}
}

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;

public class CheckBoxRender extends TreeSample implements TreeCellRenderer {

private JCheckBox parentRenderer = new JCheckBox();
private JRadioButton leafRenderer = new JRadioButton();

Color selectionForeground, selectionBackground, textForeground, textBackground;

public CheckBoxRender() {
Font fontValue;
fontValue = UIManager.getFont("Tree.Font");
if (fontValue != null) {
leafRenderer.setFont(fontValue);
}
selectionForeground = UIManager.getColor("Tree.selectionForeground");
selectionBackground = UIManager.getColor("Tree.selectionBackground");
textForeground = UIManager.getColor("Tree.textForeground");
textBackground = UIManager.getColor("Tree.textBackground");
TreeSample.tree.addTreeSelectionListener(this);
}

public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,boolean hasFocus) {
Component returnValue=null;
if (leaf) {
String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
leafRenderer.setText(stringValue);
if (selected) {
leafRenderer.setForeground(selectionForeground);
leafRenderer.setBackground(selectionBackground);
}
else {
leafRenderer.setForeground(textForeground);
leafRenderer.setBackground(textBackground);
}
returnValue = leafRenderer;
}
else {
String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, false);
parentRenderer.setText(stringValue);

if (selected) {
parentRenderer.setForeground(selectionForeground);
parentRenderer.setBackground(selectionBackground);
}
else {
parentRenderer.setForeground(textForeground);
parentRenderer.setBackground(textBackground);
}
returnValue= parentRenderer;
}
return returnValue;
}

public void valueChanged(TreeSelectionEvent event) {
treePath = event.getNewLeadSelectionPath();
System.out.println(treePath);
}
}

Thanks in advance my dear friends.

Regards,
P.Parthasarathi
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Move JTree item to another JTree. Melki AWT / Swing 7 09-08-2008 08:21 AM
Issue with Jcheckbox on JTableheader ram_76uk AWT / Swing 2 07-30-2008 08:24 AM
JRadioButton set object color King8654 AWT / Swing 4 04-24-2008 09:52 AM
Can we add JCheckBox array into JTextArea? nancyhung AWT / Swing 1 02-17-2008 12:07 AM
jcheckbox issues need help. thanks. carlos123 New To Java 3 11-05-2007 10:37 PM


All times are GMT +2. The time now is 03:25 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org