JTreemake a renderer
It – Hardware & Software | Armand Cosentino | 4 viewsThe connected dollar of JTree
This time, we will have decided to keep thinking concerning the connected dollar of JTree.The connected dollar of JTree is prepared as the interface, javax.swing.tree.TreeCellRenderer.Has been prepared also the class which calls this interface “DefaultTreeCellRenderer” of the mounting being completed, it is designed in such a way that it can also customize just extremely the basic part of the indication of JTree simply by the fact that this is used.Actually it will try doing.
package jp.allabout;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.tree.*;
public class SampleApp extends JFrame {
JTree tree;
public SampleApp () {
this.setSize (new Dimension (300,200));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tree = new JTree(makeModel());
tree.setCellRenderer(makeRenderer());
this.add(tree,BorderLayout.CENTER);
}
public TreeModel makeModel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(“the root of the tree”);
DefaultTreeModel model = new DefaultTreeModel(root);
for(int i = 0;i < 5;i++){
DefaultMutableTreeNode node = new DefaultMutableTreeNode(i + “the second node”);
root.add(node);
for(int j = 0;j < 3;j++)
node.add(new DefaultMutableTreeNode(j + “the second sub-node”));
}
return model;
}
public TreeCellRenderer makeRenderer(){
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
/ ClosedIcon
BufferedImage closeImg = new BufferedImage(16,16,BufferedImage.TYPE_INT_RGB);
Graphics g = closeImg.getGraphics();
g.setColor(tree.getBackground());
g.fillRect(0, 0, 16, 16);
g.setColor(Color.BLUE);
g.fillOval(2, 2, 12, 12);
g.dispose();
renderer.setClosedIcon(new ImageIcon(closeImg));
/ OpenIcon
BufferedImage openImg = new BufferedImage(16,16,BufferedImage.TYPE_INT_RGB);
g = openImg.getGraphics();
g.setColor(tree.getBackground());
g.fillRect(0, 0, 16, 16);
g.setColor(Color.BLUE);
g.drawOval(2, 2, 12, 12);
g.dispose();
renderer.setOpenIcon(new ImageIcon(openImg));
/ LeafIcon
BufferedImage leafImg = new BufferedImage(16,16,BufferedImage.TYPE_INT_RGB);
g = leafImg.getGraphics();
g.setColor(tree.getBackground());
g.fillRect(0, 0, 16, 16);
g.setColor(Color.RED);
g.fillOval(2, 2, 12, 12);
g.dispose();
renderer.setLeafIcon(new ImageIcon(leafImg));
return renderer;
}
public static void main(String[] args) {
new SampleApp().setVisible(true);
}
}
you define your own rendererJTree. |
DefaultTreeCellRenderercustomize the icon of the cell is
here, the icon to display a customized depending on the situation of each node in the tree. has a child node, nodes in the closed state was painted blue yen, nodes in the open blue circle outline only, on the node that has no child nodes are shown as a red circle and each icon.
here, DefaultTreeCellRenderercreation of the makeRenderer we are carving a method called. here, firstDefaultTreeCellRendereri have begun to create an instance from.
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
and, for instance that you created, we will perform a variety of settings. here, 3has set the icon for one. this is, BufferedImageprepare the image to display as, with thisImageIconcreates an instance, in the method to set the iconDefaultTreeCellRendererput an icon on the, has been working on a procedure called.
as an example, with subnodes, let’s look at the processing of the node icon to display the closed. firstBufferedImagei have to prepare the.
BufferedImage closeImg = new BufferedImage(16,16,BufferedImage.TYPE_INT_RGB);
BufferedImagethe size of the16×16be the first. icon from, so be careful you will also have cases that can not be greater than that displayed. once you have the instance, from thereGraphicsget the, the drawing of the icon.
Graphics g = closeImg.getGraphics();
g.setColor(tree.getBackground());
g.fillRect(0, 0, 16, 16);
g.setColor(Color.BLUE);
g.fillOval(2, 2, 12, 12);
g.dispose();
getGraphicsinGraphicsgets the instance, will draw. at the enddisposeinGraphicsdiscard the. this area, you’ll notice from the self-explanatory without having to use the basics of graphic.
renderer.setClosedIcon(new ImageIcon(closeImg));
at the end, setClosedIcon the method, ImageIconset the instance as an icon. ImageIconis, argumentBufferedImageyou create an instance of the specified. in this, image was prepared as an icon has been set, so is.
DefaultTreeCellRendererto the, this setClosedIcon in addition to the, setOpenIcon setLeafIcon there are methods for setting the icon, such as. by taking advantage of these, you can easily customize the icon.
other, state of each node (whether it has been selected, such as) there is also a method to set the background and text color depending on the. if you customize the display a very simple, yes i can have to learn the methods of this degree.
TreeCellRenderertrying to make your own
in, to customize more flexible what to do? it, JListas was done at the, TreeCellRendererit is good if you define your own. as we did before, extends JPanelas a class thatTreeCellRendererdefine the, do i fix the display in the drawing process there. in, let’s try this also.
package jp.allabout;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.tree.*;
public class SampleApp extends JFrame {
JTree tree;
public SampleApp(){
this.setSize(new Dimension(300,200));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tree = new JTree(makeModel());
tree.setCellRenderer(new MyTreeCellRenderer());
JScrollPane scroll = new JScrollPane(tree);
this.add(scroll,BorderLayout.CENTER);
}
public TreeModel makeModel(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(“the root of the tree”);
DefaultTreeModel model = new DefaultTreeModel(root);
for(int i = 0;i < 5;i++){
DefaultMutableTreeNode node = new DefaultMutableTreeNode(i + “the second node”);
root.add(node);
for(int j = 0;j < 3;j++)
node.add(new DefaultMutableTreeNode(j + “the second sub-node”));
}
return model;
}
public static void main(String[] args) {
new SampleApp().setVisible(true);
}
}
class MyTreeCellRenderer extends JPanel implements TreeCellRenderer {
private static final long serialVersionUID = 1L;
private int index;
private TreeNode node;
private boolean isSelected,isExpanded,isLeaf,hasFocus;
@Override
public Component getTreeCellRendererComponent(JTree tree, Object object,
boolean isSelected, boolean isExpanded, boolean isLeaf,
int index, boolean hasFocus) {
this.index = index;
this.node = (TreeNode)object;
this.isSelected = isSelected;
this.isExpanded = isExpanded;
this.isLeaf = isLeaf;
this.hasFocus = hasFocus;
this.setBackground(tree.getBackground());
this.setMinimumSize(new Dimension(100,22));
this.setPreferredSize(new Dimension(200,22));
return this;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isSelected){
g.setColor(new Color(255,200,200));
} else if (isExpanded){
g.setColor(new Color(200,200,200));
} else if (isLeaf){
g.setColor(new Color(200,255,200));
} else {
g.setColor(new Color(200,200,255));
}
g.fillOval (index * 20, 1, 20 and 20);
g.setFont (new Font (“Serif” and Font.BOLD, 16));
g.setColor (new Color (0,0,100));
g.drawString (node.toString (), 5 and 18);
if (isSelected) {
g.setColor (new Color (255,0,0,100));
g.drawRect (0 and 0, this.getWidth n/a , this.getHeight n/a ;
}
}
}
Defining TreeCellRenderer individually, it customizes indication. |
Here, the basic part of the connected dollar which was made with JList is reused.”As for the node which at this connected dollar, “has the sub node, closes blue circle” “the node which sub node has, opened with the node which does not have the circle sub node of the gray” circle of green” “with the node which is selected has indicated red circle” respectively.In addition the node which is selected, in order to be easy to know, has tried to show the fact that it is selected with the red closing line.
thisTreeCellRendererin, in the method, such as:, we now return to generate the components you want to set as the renderer.
public Component getTreeCellRendererComponent(JTree tree, Object object,
boolean isSelected, boolean isExpanded, boolean isLeaf,
int index, boolean hasFocus)
JTree tree——this is usedJTree.
Object object——this node (TreeNode) object that will be the.
boolean isSelected——whether or not they are selected.
boolean isExpanded——whether or not that is open.
boolean isLeaf——whether or not that does not have a subnode.
int index——the index number.
boolean hasFocus——whether or not with a focus.
JListthis is similar to the renderer when the, it has increased additional arguments. about the state of the nodebooleanvalue has been added to some. depending on the value of these, paintComponenti have changed the image is drawn in.
Incoming search terms:
- jtree keep color of the node after it has new color (1)
- jtree subnode are blue (1)
Additional items from "It – Hardware & Software"
- Lenovo ThinkPad X60
- BOSE SoundDock review! take advantage of more! iPod mini
- pcDtrying to take advantage of the drive
- PHP5installing and configuring
- 10less than ten thousand yen good find the! KDL-16M1
- to reduce the size of the image material3one+αmethod of let’s reduce the image
- 2the title of the line1line fit to2ways
- world firstSideShowequipped withASUS W5Fe
- with easy installation, good sound easy to enjoy YHT-S401
- Office Spesialist Word, ExcelofExpertfull marks! her senior2reason for the perfect score for both subjects