csunlistek
asked on
JTextArea as JTree node label
Hi
I want my JTree to display a JTextArea as its node label.
For example:
When a node minimizes, only the 1st line of the text string is shown.
When a node expandes, the entire multi-line text string is shown.
It would look this:
Root +----LABEL A line 1
| LABEL A line 2
| .....
|
+----LABEL B Line 1
I try extending javax.swing.tree.DefaultTr eeCellRend erer
but I could not get it to display a JTextArea as the node label.
How should I approach this problem ?
Is there any sample source code out there ?
Any commercial UI javabean that can do this ?
I would appreciate any comment or suggestion.
I want my JTree to display a JTextArea as its node label.
For example:
When a node minimizes, only the 1st line of the text string is shown.
When a node expandes, the entire multi-line text string is shown.
It would look this:
Root +----LABEL A line 1
| LABEL A line 2
| .....
|
+----LABEL B Line 1
I try extending javax.swing.tree.DefaultTr
but I could not get it to display a JTextArea as the node label.
How should I approach this problem ?
Is there any sample source code out there ?
Any commercial UI javabean that can do this ?
I would appreciate any comment or suggestion.
ASKER
Yes, here is my latest test code. tested under JDK 1.4.2.
It implements TreeCellRenderer.
It was at least able to display a JTextArea as a tree node
but it has the following problems:
1. resizing. How do I get the width of JTextArea to fit the width of the frame ?
2. border. How do I draw the border around JTextArea to show the boundary of text ?
Thanks for any suggestion. sample code would be useful.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MyJTree extends JPanel {
public MyJTree() {
setLayout(new BorderLayout());
// init data model
String dummyText="";
for (int i = 0; i < 10; i++) dummyText +=" ABCDE"; // dummy text string
//for (int i = 0; i < 10; i++) dummyText +=" ABCDE \n"; // shows multiline here
DefaultMutableTreeNode root = new DefaultMutableTreeNode("ro ot");
for (int i = 0; i < 3; i++) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Ch ild "+i + dummyText);
root.add( child );
for (int j= 0; j < 3; j++)
child.add( new DefaultMutableTreeNode("Gr andChild "+i+"."+ j + dummyText));
}
DefaultTreeModel model = new DefaultTreeModel(root);
// create tree UI
JTree tree = new JTree(model);
// Change the cell renderer
tree.setCellRenderer(new CellRenderer());
// Put tree in a scrollable pane
JScrollPane sp = new JScrollPane(tree);
add(sp, BorderLayout.CENTER);
}
// my cell renderer
class CellRenderer implements TreeCellRenderer {
JTextArea renderer;
CellRenderer () {
renderer = new JTextArea();
renderer.setWrapStyleWord( true);
renderer.setLineWrap(true) ;
renderer.setOpaque (true);
}
public Component getTreeCellRendererCompone nt(JTree tree,
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
// Change background color based on selected state
Color background = (selected ? Color.lightGray : Color.white);
renderer.setBackground(bac kground);
String text = value.toString();
if (expanded) { // in expanded state, display all text
renderer.setText(text);
renderer.setPreferredSize( new Dimension(200,80));
} else { // in minimized state, display 1st 10 chars
renderer.setText( (text.length()>10 ? text.substring(0, 10) : text) );
renderer.setPreferredSize( new Dimension(200,20));
}
return renderer;
}
}
public static void main(String[] args) {
MyJTree t = new MyJTree();
JFrame frame = new JFrame();
frame.getContentPane().add (t);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
It implements TreeCellRenderer.
It was at least able to display a JTextArea as a tree node
but it has the following problems:
1. resizing. How do I get the width of JTextArea to fit the width of the frame ?
2. border. How do I draw the border around JTextArea to show the boundary of text ?
Thanks for any suggestion. sample code would be useful.
--------------------------
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
public class MyJTree extends JPanel {
public MyJTree() {
setLayout(new BorderLayout());
// init data model
String dummyText="";
for (int i = 0; i < 10; i++) dummyText +=" ABCDE"; // dummy text string
//for (int i = 0; i < 10; i++) dummyText +=" ABCDE \n"; // shows multiline here
DefaultMutableTreeNode root = new DefaultMutableTreeNode("ro
for (int i = 0; i < 3; i++) {
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Ch
root.add( child );
for (int j= 0; j < 3; j++)
child.add( new DefaultMutableTreeNode("Gr
}
DefaultTreeModel model = new DefaultTreeModel(root);
// create tree UI
JTree tree = new JTree(model);
// Change the cell renderer
tree.setCellRenderer(new CellRenderer());
// Put tree in a scrollable pane
JScrollPane sp = new JScrollPane(tree);
add(sp, BorderLayout.CENTER);
}
// my cell renderer
class CellRenderer implements TreeCellRenderer {
JTextArea renderer;
CellRenderer () {
renderer = new JTextArea();
renderer.setWrapStyleWord(
renderer.setLineWrap(true)
renderer.setOpaque (true);
}
public Component getTreeCellRendererCompone
Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
// Change background color based on selected state
Color background = (selected ? Color.lightGray : Color.white);
renderer.setBackground(bac
String text = value.toString();
if (expanded) { // in expanded state, display all text
renderer.setText(text);
renderer.setPreferredSize(
} else { // in minimized state, display 1st 10 chars
renderer.setText( (text.length()>10 ? text.substring(0, 10) : text) );
renderer.setPreferredSize(
}
return renderer;
}
}
public static void main(String[] args) {
MyJTree t = new MyJTree();
JFrame frame = new JFrame();
frame.getContentPane().add
frame.setSize(300, 300);
frame.setVisible(true);
}
}
ASKER
One more question to add to above:
3. Editing. How do I make the JTextArea as tree node editable ?
It appears the CellRenderer approach is designed for display text, not for in-place text editing.
Maybe I approach the whole problem in the wrong way ???
3. Editing. How do I make the JTextArea as tree node editable ?
It appears the CellRenderer approach is designed for display text, not for in-place text editing.
Maybe I approach the whole problem in the wrong way ???
You need to create a TreeCellEditor, as well as a TreeCellRenderer.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
objects referenced website is fine. go there and download that java file.
ASKER
The sample codes do not work exactly as I had in mind (editing, border, auto JTextArea width adjustment, line wrap, etc) but they provide a good starting point, enough to get me going again.
Thanks for all the help.
Thanks for all the help.
(:
I try extending javax.swing.tree.DefaultTr
but I could not get it to display a JTextArea as the node label.
>>
Can you post your code?