Anil Philip

+ Follow
since Sep 06, 2003
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Anil Philip

I just wondered what resources (books, newsgroups mock exams etc) are there to help one prepare for the certification exam:

Sun Certified Mobile Application Developer for the Java 2 Platform, Micro Edition, Version 1.0 (CX-310-110)
15 years ago
It is fixed!

I had to simply setSelectionPath to null. Anand, God bless you, I dont know if that's what you meant, but that fixed it.
16 years ago
I have reproduced the problem in the toy program below. Click on any button, you will see the exception at bottom.

16 years ago

Originally posted by Anand Loni:

before removing node from parent u have to find if it has any sibling or not if no sibling then assign sibling as parent.

sibling = parent;

Sorry, but this doesnt make sense. What does that code do?
16 years ago
I have been stuck for more than a few days on this problem. Tried to get help elsewhere, without luck - wondered if anyone here would have insight into this. I promise I shall update the forum if I get a solution.

Please Note that node is NOT null otherwise NP would have been thrown in the node.getParent(). I am mystified to get this NP exception within the Swing lib. Any insights?


I wonder if it may be because when I create the JPanel which contains the tree, I dont have the root node created. So I instantiate the tree model with null;

later on, when the root node is available, I use

This works fine during display of the tree, but when I try to remove the node, for some reason, it may not have updated the root value (?). Just guessing... But I wonder how to fix it. Can I add the Scroll Pane with null first and then with the actual JTree later on? (there is no method in JScrollPane to reset the Component)

When I try to remove the node differently - using the methods in DefaultMutableTreeNode,

I still get this exception in the treeModel.nodesWereRemoved() call.
Any insight?

16 years ago
Sorry, but your advice was incorrect - it was not necessary to implement the TreeCellRenderer interface. Returning the Box, rendered it automatically since the Box is a JComponent and contains JComponents. The code in my thread above displays the compound node just fine. Simply extending DefaultTreeCellRenderer worked!

Originally posted by Paul Clapham:
1. If you don't want your rendering component to BE a JLabel then clearly you shouldn't extend something that IS a JLabel. Just implement the TreeCellRenderer interface instead. The API documentation for TreeCellRenderer claims the tutorial has an example of using it.

[ February 11, 2006: Message edited by: Anil Philip ]
[ February 12, 2006: Message edited by: Anil Philip ]
16 years ago
I am able to retrieve the user object now and am working on the other questions.

I am surprised that it is not stated in the JDK documentation that value is a DefaultMutableTreeNode:

getTreeCellRendererComponent Configures the renderer based on the passed in components. The value is set from messaging the tree with convertValueToText, which ultimately invokes toString on value. The foreground color is set based on the selection and the icon is set based on on leaf and expanded.

16 years ago
I tried:

and got:
16 years ago
I am using class VisualNode below as my user object
Ok, so I am using my own renderer because this is a compound node with JButtons, JTabbedPane, JTextArea in a Box.
When I create the node, I do a:

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(new VisualNode("My Name"));

I have 3 questions:
1) DefaultTreeCellRenderer extends JLabel, but VisualNode contains several JComponents. So is it correct for MyCellRenderer to extend DefaultTreeCellRenderer?
2) How do I access the user object? In the renderer, I want to be able to get this user object so that I can return the Box component.
3) What is the parameter "Object value" for a Compound node? If I add the Editor, how will it know which it stands for? (the title or the details)

Originally posted by Paul Clapham:
Here's the tutorial on JTree:

It includes some examples of TreeCellRenderers, which are what you are asking about. Most of the examples you'll find have the renderer extending JLabel, because commonly a JLabel is sufficient to render a tree node. But in your case the renderer will have to be some other component, maybe JPanel or a customized subclass.

16 years ago
I have 2 JTextAreas and some JButtons in a Box layout - this is one node. I wish to display these nodes in a JTree. The Text Areas are editable.
Does anyone know how I can do this? I could not find any examples of this (for compound Swing components) on the web.

[ January 28, 2006: Message edited by: Anil Philip ]
16 years ago
java version "1.5.0_04"
When drawing JComponent Text Areas and dynamically scrolling, Java gets confused, gets the shivers and freezes when the viewport cannot get its arms around the canvas.
Possible problem at JViewport.scrollRectToVisible(). I guess it's being called under the covers and doesnt know where to scroll to.

When painting non-text area components eg. graphics circles, it is ok.

Have provided example code. This code is based on the ScrollDemo2 example provided in the Sun Java

Anil Philip
juwo LLC
for good news go to

Usage: run program and repeatedly click the left mouse button near right boundary to create a new JComponent node each time and to force scrolling area to increase in size to the right.
When the first node crosses the left boundary, then the scroll pane gets confused, gets the shivers and hangs.
The other scroll modes are a bit better - but very slow leaving the toolbar (in the oroginal application) unpainted sometimes.


* to show possible bug when in the default BLIT_SCROLL_MODE and with JTextArea components.
* author: Anil Philip. juwo LLC.
* Usage: run program and repeatedly click the left mouse button near right boundary to
* create a new JComponent node each time and to force scrolling area to increase in size to the right.
* When the first node crosses the left boundary, then the scroll pane gets confused, gets the shivers
and hangs.
* The other scroll modes are a bit better - but very slow leaving the toolbar (in the oroginal
* unpainted sometimes.
* This code is based on the ScrollDemo2 example provided in the Sun Java Tutorial (written by John
Vella, a tutorial reader).

import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/* is a 1.5 application that requires no other files. */
public class ScrollDemo2WithBug extends JPanel {
private Dimension area; //indicates area taken up by graphics

private Vector circles; //coordinates used to draw graphics

private Vector components;

private JPanel drawingPane;

public ScrollDemo2WithBug() {
super(new BorderLayout());

area = new Dimension(0, 0);
circles = new Vector();
components = new Vector();

//Set up the instructions.
JLabel instructionsLeft = new JLabel(
"Click left mouse button to place a circle.");
JLabel instructionsRight = new JLabel(
"Click right mouse button to clear drawing area.");
JPanel instructionPanel = new JPanel(new GridLayout(0, 1));

//Set up the drawing area.
drawingPane = new DrawingPane();

drawingPane.setPreferredSize(new Dimension(200, 200));

//Put the drawing area in a scroll pane.
JScrollPane scroller = new JScrollPane(drawingPane);
// scroller.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
if(scroller.getViewport().getScrollMode() == JViewport.BACKINGSTORE_SCROLL_MODE)
if(scroller.getViewport().getScrollMode() == JViewport.BLIT_SCROLL_MODE)
if(scroller.getViewport().getScrollMode() == JViewport.SIMPLE_SCROLL_MODE)

//Lay out this demo.
add(instructionPanel, BorderLayout.PAGE_START);
add(scroller, BorderLayout.CENTER);

/** The component inside the scroll pane. */
public class DrawingPane extends JPanel implements MouseListener {
private class VisualNode {
int x = 0;

int y = 0;

int id = 0;

public VisualNode(int id, int x, int y) { = id;
this.x = x;
this.y = y;
titlePanel.add(new JButton("Hi!"));
Box box = Box.createVerticalBox();
Box titlePanel = Box.createHorizontalBox();
JTextArea title = new JTextArea(1, 10); // 1 rows x 10 cols
Box nodePanel = Box.createVerticalBox();

public void paintNode(Graphics g) {
int ix = (int) x + ScrollDemo2WithBug.this.getInsets().left;
int iy = (int) y + ScrollDemo2WithBug.this.getInsets().top;
title.setText(id + " (" + ix + "," + iy + ") ");
box.setBounds(ix, iy, box.getPreferredSize().width, box

int n = 0;

DrawingPane() {


protected void paintComponent(Graphics g) {
g.fill3DRect(10, 10, 25, 25, true);
Point point;
for (int i = 0; i < circles.size(); i++) {
point = (Point) circles.elementAt(i);
VisualNode node = (VisualNode) components.get(i);


//Handle mouse events.
public void mouseReleased(MouseEvent e) {
final int W = 100;
final int H = 100;
boolean changed = false;
if (SwingUtilities.isRightMouseButton(e)) {
//This will clear the graphic objects.
area.width = 0;
area.height = 0;
changed = true;
} else {
int x = e.getX() - W / 2;
int y = e.getY() - H / 2;
if (x < 0)
x = 0;
if (y < 0)
y = 0;
Point point = new Point(x, y);
VisualNode node = new VisualNode(circles.size(), point.x,
// add(node);
drawingPane.scrollRectToVisible(new Rectangle(x, y, W, H));

int this_width = (x + W + 2);
if (this_width > area.width) {
area.width = this_width;
changed = true;

int this_height = (y + H + 2);
if (this_height > area.height) {
area.height = this_height;
changed = true;
if (changed) {
//Update client's preferred size because
//the area taken up by the graphics has
//gotten larger or smaller (if cleared).

//Let the scroll pane know to update itself
//and its scrollbars.

public void mouseClicked(MouseEvent e) {

public void mouseEntered(MouseEvent e) {

public void mouseExited(MouseEvent e) {

public void mousePressed(MouseEvent e) {

* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
private static void createAndShowGUI() {
//Make sure we have nice window decorations.

//Create and set up the window.
JFrame frame = new JFrame("ScrollDemo2");

//Create and set up the content pane.
JComponent newContentPane = new ScrollDemo2WithBug();
newContentPane.setOpaque(true); //content panes must be opaque

//Display the window.
frame.setSize(800, 600);

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
17 years ago
what is this book about?
18 years ago
I am new to JSP programming and am not clear about the differences between the session and request objects.
- When are they created,
- how long do they last,
- what is the scope of objects created therein?
18 years ago
What kind of questions are asked on the Essay exam?

Originally posted by Mike Southgate:
I've submitted the assignment and taken the exam this morning. Thanks everyone for all your help. How long does it take to hear back, normally. The instructions say up to 6 weeks, but I don't think I can last that long