Hello everyone!
I created a custom JTree Renderer for my tree because I wanted it to have an icon near each node.
Here is a sample of my code for the custom renderer.
and then I Override the getTreeCellRend ererComponent(s mall sample just to see the structure of my code) :
finally, the tree is shown like this:
the photo is also attached.
You can see that the nodes are horizontally and vertically cut!
Does anyone have any ideas what I should do so that the nodes appear properly? I printed the Scroll Pane,Tree, ViewPort preferred sizes and all are smaller than the size they are given.
Thanks in advance!
I created a custom JTree Renderer for my tree because I wanted it to have an icon near each node.
Here is a sample of my code for the custom renderer.
Code:
public TreeRenderer() { renderer = new JPanel(); renderer.setLayout(new BoxLayout(renderer, BoxLayout.X_AXIS)); titleLabel = new JLabel(" "); titleLabel.setForeground(Color.black); titleLabel.setFont(new Font("Calibri", Font.PLAIN, 24)); renderer.add(titleLabel); buttonLabel = new JLabel(" "); renderer.add(buttonLabel); }
Code:
@Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { Component returnValue = null; if ((value != null) && (value instanceof DefaultMutableTreeNode)) { Object userObject = ((DefaultMutableTreeNode) value) .getUserObject(); if (userObject instanceof PlugwiseNode) { PlugwiseNode node = (PlugwiseNode) userObject; if(!node.title.equals("Home Lights")) titleLabel.setIcon(createImageIcon("http://bytes.com/images/plugwisenode.png",""));//oti icon 8elw to vazw edw else titleLabel.setIcon(createImageIcon("http://bytes.com/images/office lights.png","")); titleLabel.setText(node.title+ " "); buttonLabel.setIcon(createImageIcon("http://bytes.com/images/jtree green.png","")); if (selected) { renderer.setOpaque(true); renderer.setBackground(backgroundSelectionColor); } else { renderer.setOpaque(false); } renderer.setEnabled(tree.isEnabled()); returnValue = renderer; } } if (returnValue == null) { defaultRenderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); defaultRenderer.setBackgroundSelectionColor(backgroundSelectionColor); defaultRenderer.setFont(new Font("Calibri", Font.PLAIN, 24)); }

the photo is also attached.
You can see that the nodes are horizontally and vertically cut!
Does anyone have any ideas what I should do so that the nodes appear properly? I printed the Scroll Pane,Tree, ViewPort preferred sizes and all are smaller than the size they are given.
Thanks in advance!
Comment