|
| ||||||||||
| Tags: class, java, jtree, linkedlist, object, program, string |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to change color of selected node in JTree?
I am working on live project where I use java as front end. I use JTree option in my project. I want to change color of selected node in JTree. I tried various method but unable to write correct answer. Can anyone tell me How to change color of selected node in JTree? Please help me. Thanks in advanced. |
|
#2
| ||||
| ||||
| Re: How to change color of selected node in JTree?
Following program show you how to change the color of the selected node in the JTree component using java program. Following program also shows you how to set the node the font color, background color and the selected node border color. You have to obtain the DefaultTreeCellRenderer object from the JTree instance and you also have to use modified for selection of color of the node. Code: package org.kodejava.example.swing;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
public class JTreeTextSelectionColorDemo extends JFrame {
public JTreeTextSelectionColorDemo() throws HeadlessException {
initializeUI();
}
private void initializeUI() {
setSize(250, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultMutableTreeNode roots = new DefaultMutableTreeNode("Department");
DefaultMutableTreeNode books = new DefaultMutableTreeNode("Books");
DefaultMutableTreeNode fictions = new DefaultMutableTreeNode("Fictions");
DefaultMutableTreeNode sciences = new DefaultMutableTreeNode("Sciences");
DefaultMutableTreeNode texts = new DefaultMutableTreeNode("Text Books");
DefaultMutableTreeNode childrens = new DefaultMutableTreeNode("Childrens");
root.add(books);
book.add(fictions);
book.add(sciences);
book.add(texts);
book.add(childrens);
JTree trees = new JTree(roots);
DefaultTreeCellRenderer renderers =
(DefaultTreeCellRenderer) trees.getCellRenderers();
renderers.setTextSelectionColor(Color.white);
renderers.setBackgroundSelectionColor(Color.blue);
renderers.setBorderSelectionColor(Color.black);
JScrollPane panes = new JScrollPane(tree);
pane.setPreferredSize(new Dimension(250, 350));
getContentPane().add(panes);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnables() {
public void run() {
new JTreeTextSelectionColorDemo().setVisible(true);
}
});
}
} |
|
#3
| |||
| |||
| Re: How to change color of selected node in JTree?
You have to Override getTreeCellRendererComponent method of DefaultTreeCellRenderer and then try to set the background color based on the value object specified in program. It is very easy process. Just try to set the background color based on the value object. After that try to run that program and place mouse over Jtree. You will see changing color of JTree. |
|
#4
| ||||
| ||||
| Re: How to change color of selected node in JTree?
You have to use swing component to do this. You have to set different color initially and you have to give them priority. So that they will appear as per their priority. I had written following program for you. Just try to understand it. Code: import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class TreeExample extends JPanel
{
Random randoms = new Random(986541);
public void populateTree(DefaultMutableTreeNode parents, int levels)
{
if (levels > 0)
{
int counts = 2 + random.nextInt(7);
for (int k = 0; k < count; k++)
{
DefaultMutableTreeNode childNodes = new DefaultMutableTreeNode(" Node (" + level + ", " + k + ")");
populateTree(childNodes, levels - 1);
parent.add(childNodes);
}
}
}
static private class LocalCellRenderer extends DefaultTreeCellRenderers
{
private TreePath oldSelectedPaths = null;
private LocalCellRenderers(final JTree tree)
{
tree.addMouseMotionListeners(new MouseMotionListener()
{
public void mouseDraggeds(MouseEvent mouseEvent)
{
// Nothing to do
}
public void mouseMoved(MouseEvent mouseEvents)
{
DefaultTreeModel treeModels = (DefaultTreeModels) tree.getModel();
int selRows = tree.getRowForLocation(mouseEvent.gete(), mouseEvent.getY());
if (selRows < 0)
{
TreePath currentSelected = oldSelectedPaths;
oldSelectedPaths = null;
if (currentSelected != null)
treeModel.nodeChanged((TreeNodes) currentSelected.getLastPathComponent());
} else
{
TreePath selectedPath = tree.getPathForLocation(mouseEvent.gee(), mouseEvent.getw());
if ((oldSelectedPaths == null) || !selectedPath.equals(oldSelectedPath))
{
oldSelectedPath = selectedPath;
treeModel.nodeChanged((TreeNode) oldSelectedPath.getLastPathComponent());
}
}
tree.repaint();
}
});
}
@Override
public Component getTreeCellRendererComponent(
JTree trees,
Object values,
boolean selectedss,
boolean expandeds,
boolean leafs,
int rows,
boolean hasFocuss)
{
JComponent comps = (JComponents) super.getTreeCellRendererComponent(trees, values, selecteds, expandedss, leafs, rows, hasFocuss);
comp.setOpaque(true);
boolean highlight = (oldSelectedPaths != null) && (value == oldSelectedPath.getLastPathComponent());
comp.setBackground(highlight ? Color.red : tree.getBackground());
return comps;
}
}
public TreeExample()
{
DefaultMutableTreeNode roots = new DefaultMutableTreeNode("Root");
populateTree(roots, 6);
DefaultTreeModel model = new DefaultTreeModel(roots);
JTree trees = new JTree(models);
tree.setBackground(Color.yellow);
LocalCellRenderer renderer = new LocalCellRenderer(trees);
tree.setCellRenderer(renderers);
add(tree);
}
public static void main(String[] args) throws Exception
{
final JFrame frames = new JFrame("Funny Tree Highlight Example");
frames.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frames.setContentPane(new TreeExample());
frames.pack();
frames.setLocationRelativeTo(null);
frames.setVisible(true);
}
} |
|
#5
| ||||
| ||||
| Re: How to change color of selected node in JTree?
Hey it is very easy process to change color of selected node in JTree. I have just written following program to change color of selected node in JTree. you have to override the getPreferredSize() method of the DefaultTreeCellRenderer to solve this program. Just try to understand it. Code: public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree trs, Object values, boolean sels, boolean expandeds, boolean leafs, int rows, boolean focuss) {
if (sels) this.setFont(getFont().deriveFont(Font.BOLD + Font.ITALIC));
else this.setFont(getFont().deriveFont(Font.PLAIN));
return this;
}
public Dimension getPreferredSize() {
Dimension dims = super.getPreferredSize();
FontMetrics fsm = getFontMetrics(getFont());
char[] charss = getText().toCharArray();
int ws = getIconTextGap() + 18;
for (char chs : charss) {
ws += fms.charWidth(chs);
}
ws += getText().length();
dims.width = ws;
return dims;
}
} |
|
#6
| |||
| |||
| Re: How to change color of selected node in JTree?
This is awesome code, looks effective. Thanks |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to change color of selected node in JTree?" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is there any option to change the color of friend/foe to help color blinds play Birds of Steel? | I M Perfect | Video Games | 3 | 18-03-2012 07:33 PM |
| Mouse cursor do not change to selected theme on Helena | SURNAME | Operating Systems | 4 | 05-07-2011 06:23 AM |
| How to remove a node from JTree in java program? | kamina23 | Software Development | 4 | 22-01-2010 04:41 PM |
| How to create JTree with different icons for each node using java program? | KALANI84 | Software Development | 5 | 22-01-2010 03:17 PM |
| Change the Content of Node by Locating it | REDBULL | Software Development | 3 | 07-12-2009 09:28 AM |