KidsLovePC Contact Us
  Home   PHP  Graphics ASP  JavaScript   J2EE  HTML  JAVA   Swing   Webtip  Forums  Lyric  Free sheet music  Ringtones
java,jtable,jtree,swing Return to JAVA JTree list

Best books about JAVA programming:

JAVA JTree Diabled icon

The following code disable JTree node dynamically. Disabled icon has different icons assigned. This example has three files:,,,,

/*Example modified from

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import javax.swing.border.*;

public class DisabledNodeTreeExample extends JFrame {
public DisabledNodeTreeExample() {
super("DisabledNode TreeExample");

String[] strs = {"swing", // 0
"plaf", // 1
"basic", // 2
"metal", // 3
"JTree", // 4
"plaf", // 5
"motif"}; // 6

DisabledNode[] nodes = new DisabledNode[strs.length];
for (int i=0;i<strs.length;i++) {
nodes[i] = new DisabledNode(strs[i]);

final JTree tree = new JTree( nodes[0] ) {
public boolean isPathEditable(TreePath path) {
DisabledNode node = (DisabledNode)path.getLastPathComponent();
if (node.isEnabled()) {
return isEditable();
} else {
return false;

DisabledRenderer renderer = new DisabledRenderer();
renderer.setDisabledLeafIcon(new DisabledLeafIcon());
renderer.setDisabledOpenIcon(new DisabledFolderIcon());
renderer.setDisabledClosedIcon(new DisabledFolderIcon());

JScrollPane sp = new JScrollPane(tree);
ButtonPanel bp = new ButtonPanel(tree);
getContentPane().add(sp, BorderLayout.CENTER);
getContentPane().add(bp, BorderLayout.EAST);

class ButtonPanel extends JPanel {
JTree tree;
DefaultTreeModel model;

ButtonPanel(JTree tree) {
this.tree = tree;
model = (DefaultTreeModel)tree.getModel();
setLayout(new GridLayout(2,1));
setBorder(new TitledBorder("Change Node"));
JButton enabledButton = new JButton("Enabled");
JButton disabledButton = new JButton("Disabled");
enabledButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setNodeEnabled( true );
disabledButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setNodeEnabled( false );

private void setNodeEnabled(boolean enabled) {
TreePath[] path = tree.getSelectionPaths();
if (path == null) return;
DisabledNode node = null;
for (int i=0;i<path.length;i++) {
node = (DisabledNode)path[i].getLastPathComponent();
//if (node.isRoot()) {
// System.out.println("refused: root node");
//} else {
int beforeChildCount = node.getChildCount();
int afterChildCount = node.getChildCount();
if (beforeChildCount == afterChildCount) {
} else {

public static void main(String args[]) {
DisabledNodeTreeExample frame = new DisabledNodeTreeExample();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
frame.setSize(300, 180);




Sponsored link
Java JTabbedPane
JTabbedPane Preview List
Java JComboBox
JComboBox Preview List
Java JTooltip
JTooltip Preview List
Java JList
JList Preview List
Java JTree
JTree Preview List 1
JTree Preview List 2
JTable Preview List 1
JTable Preview List 2
JTable Preview List 3
JTable Preview List 4
JTable Preview List 5
JTable Preview List 6
JTable Preview List 7
JAVA Jslide
JSlide Preview List



Best books about JAVA programming from Amazon
Professional Java, JDK 5 Edition (Paperback Simply Java Programming: An Application-Driven Tutorial Approach (Paperback) Effective Java Programming Language Guide (Paperback) Head First Java, 2nd Edition (Paperback) Java : An Introduction to Problem Solving and Programming (4th Edition)


Printer Inkjet A Z lyricsLearn ChineseHome|PHP|Swing|HTML|JavaScript|ASP|JAVA|Forum|WebTip|Contact Us
Copyright © All rights reserved(new). Privacy|Copyright
designed by

Please help us cover the web site maintenance fee.
barcode scanner Thermal Printer credit card machine bar code printer