Article View: borland.public.jbuilder.java.swing
Article #10392Re: JCheckBoxes and two icons in a JTree
From: Amazone
Date: Tue, 21 Sep 1999 00:00
Date: Tue, 21 Sep 1999 00:00
213 lines
5903 bytes
5903 bytes
Hiya !
Have you seen the example called SampleTree in
"JBuilder3\samples\jdk\demo\jfc\SampleTree" ??
That's where i started. I wanted a node with 2 strings (label-textfield)
I created my own object (replacing SampleData in the example) and my own
TreeCellRenderer.
For what i can see, you can create any object you want and display it in the
tree, as long as you give a paint method if it's tricky.
If you have any hints to share, or any questions, feel free !!!
Pascale
package SampleTree;
import java.awt.*;
import javax.swing.*;
public class PanelNoeud extends JPanel
{
BorderLayout borderLayout1 = new BorderLayout();
JLabel Label = new JLabel();
JTextField TextField = new JTextField();
public PanelNoeud()
{
try
{
jbInit();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public PanelNoeud(Color Couleur_Etq, Color Couleur_Val, String Etiquette)
{
try
{
jbInit();
Label.setText(Etiquette + ": ");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.setLayout(borderLayout1);
Label.setBackground(Color.white);
this.setBackground(Color.white);
this.add(Label, BorderLayout.WEST);
this.add(TextField, BorderLayout.CENTER);
Label.setText("Etiquette: ");
TextField.setText("Valeur");
}
// public void paint(Graphics g, boolean isSelected)
// {
/*
Rectangle rectLabel = Label.getBounds();
g.fillRect(rectLabel.x, rectLabel.y, rectLabel.height -1,
rectLabel.width -1);
Label.paint(g);
Rectangle rectTextField = TextField.getBounds();
g.fillRect(rectLabel.x + rectTextField.x, rectLabel.y + rectTextField.y,
rectTextField.height -1, rectTextField.width -1);
TextField.paint(g);
*/
// }
// public void setBounds(int x, int y, int w, int h)
// {
// TextField.setBounds(x, y, w, h);
// }
public void setString(String newString)
{
TextField.setText(newString);
}
public String toString ()
{
return TextField.getText();
}
}
package SampleTree;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;
import javax.swing.JTree;
import java.awt.Component;
import java.awt.Color;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.Rectangle;
public class MyFirstRenderer extends PanelNoeud implements TreeCellRenderer
{
/** Color to use for the background when selected. */
static protected final Color SelectedBackgroundColor = Color.yellow;
protected boolean selected;
/** Font used if the string to be displayed isn't a font. */
static protected Font defaultFont;
static
{
try
{
defaultFont = new Font("SansSerif", 0, 12);
}
catch (Exception e) {}
}
public MyFirstRenderer()
{
}
public Component getTreeCellRendererComponent
(JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row,
boolean hasFocus)
{
Font font;
String stringValue = tree.convertValueToText(value, selected, expanded,
leaf, row, hasFocus);
/* Set the text. */
TextField.setText(stringValue);
PanelNoeud userObject =
(PanelNoeud)((DefaultMutableTreeNode)value).getUserObject();
if(hasFocus)
setForeground(Color.cyan);
else
setForeground(Color.black);
if(userObject.getFont() == null)
setFont(defaultFont);
else
setFont(userObject.getFont());
/* Update the selected flag for the next paint. */
this.selected = selected;
return this;
}
public void paint(Graphics g)
{
Color bColor;
// Icon currentI = getIcon();
if(selected)
bColor = SelectedBackgroundColor;
else if(getParent().getParent() != null)
/* Pick background color up from parent (which will come from
the JTree we're contained in). */
bColor = getParent().getBackground();
else
bColor = getBackground();
g.setColor(bColor);
// if(currentI != null && getText() != null) {
// int offset = (currentI.getIconWidth() + getIconTextGap());
// g.fillRect(offset, 0, getWidth() - 1 - offset,
// getHeight() - 1);
// }
// else
// g.fillRect(0, 0, getWidth()-1, getHeight()-1);
super.paint(g);
}
}
Alex wrote in message <7s5ms0$8o620@forums.borland.com>...
>Hello,
>
>I want to show two different JTrees and I have a problem with each of them:
>
>The fisrt one shall show two icons (and text) for some nodes. That�s
already
>the problem. I can set different icons depending on the node�s state
>(expanded or not, ...) but I don�t know how to show two icons at the same
>time.
>
>
>
>
>I want my second JTree to show a JCheckBox next to the node�s icon. I have
>simply no idea how to do this .
>
>
>Would you please help me and tell me how to do that or where I can find an
>exmple ?
>
>
>Thank you all very much,
>Alex.
>
>
Technical Details
Message-ID:
<pjMF3.3775$j6.48878@carnaval.risq.qc.ca>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<7s5ms0$8o620@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 111d45,73c24ac400999e17 X-Google-Attributes: gid111d45,public From: "Amazone" <DeepCover@videotron.ca> Subject: Re: JCheckBoxes and two icons in a JTree Date: 1999/09/21 Message-ID: <pjMF3.3775$j6.48878@carnaval.risq.qc.ca> X-Deja-AN: 527829648 References: <7s5ms0$8o620@forums.borland.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 X-Trace: carnaval.risq.qc.ca 937922901 132.208.210.61 (Tue, 21 Sep 1999 10:08:21 EDT) Organization: UQAM http://www.uqam.ca/ NNTP-Posting-Date: Tue, 21 Sep 1999 10:08:21 EDT Newsgroups: borland.public.jbuilder.java.swing