JTree - how to setBackground to node - java

I read a lot of tutorials but no it helps me. I have jTree which works and setForeground works perfectly but when I want to setBackground so jTree is without changes. Can you help me how it should be write. Thanks.
import java.awt.Color;
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class UrlNodeRenderer extends DefaultTreeCellRenderer {
public static Icon icon = null;
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
setOpenIcon(icon);
setClosedIcon(icon);
setLeafIcon(icon);
UrlTreeNode node = (UrlTreeNode) (((DefaultMutableTreeNode) value).getUserObject());
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
return this;
}
}

You need to use setOpaque(true); on your TreeCellRenderer to change background color. You can add it to constructor:
public UrlNodeRenderer() {
icon = new ImageIcon(getClass().getResource("icon.png"));
setOpaque(true);
}
EDIT:
You need to change background color in false and true cases like next:
private class Renderer extends DefaultTreeCellRenderer{
public Renderer() {
setOpaque(true);
}
#Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
setBackground(selected ? Color.LIGHT_GRAY :
(leaf ? Color.GRAY : tree.getBackground()));
return this;
}
}
In your case:
if(node.isContainsPhrase()) {
setForeground(Color.BLUE);
setBackground(Color.PINK); // doesn't works
}
you set color to all nodes, because you never set color for false case.

Related

Strikethrough a node in a JTree

In my project i have a Jtree with custom node (which extends DefaultMutableTreeNode). Each node is associated with a boolean value. When the boolean is False i woul like to strike the texte of my node. So for example :
node 1
node1.1
node1.2
node 2
node2.1
...
I tried to create a new Font but i don't find any properties to strike the text and i only managed to add my custom font to the whole tree and not node by node.
I think that i should use the TreeRenderer but i can't find any method to help me strike the node.
Does someone have an idea on i can do it ?
Check out the example below. For keeping it simple, I am just striking through the selected node. You need to, of course, use your own check on the value.
package snippet;
import java.awt.Component;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultTreeCellRenderer;
public class JTreeTest extends JFrame {
private final class StrikeoutCellRenderer extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
#Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus) {
Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
Font font = c.getFont();
Map attributes = font.getAttributes();
if(sel)
attributes.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
else
attributes.remove(TextAttribute.STRIKETHROUGH);
Font newFont = new Font(attributes);
c.setFont(newFont);
return c;
}
}
private static final long serialVersionUID = 1L;
public JTreeTest() {
super(JTreeTest.class.getName());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents();
}
private void initComponents() {
JTree tree = new JTree();
tree.setCellRenderer(new StrikeoutCellRenderer());
add(tree);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override public void run() {
JTreeTest t = new JTreeTest();
t.setVisible(true);
}
});
}
}
Note that even if the node doesn't need a strike through, you need to reset the attribute, since a single component is used for painting all the nodes.
The simplest way would be to define a renderer (extending DefaultTreeCellRenderer) and call setText() passing html string like this "<html><u>node1.2</u></html>" as value for the strikethrough.

TreeCellRenderer: how to set background color?

I've written a custom TreeCellRenderer in order to change a components appearance. Everything works fine, except that setBackground has no effect. The code is definitely executed as the foreground color always changes correctly. Since selected items are rendered in blue and deselected item in white (without having written that code myself), I assume that my changes are overridden by JTree. So what would be the proper way to change the background color?
This is essentially my code:
JTree tree = new JTree();
tree.setCellRenderer(new MyCellRenderer());
///////
public class MyCellRenderer extends DefaultTreeCellRenderer{
#Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean isSelected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
JComponent c = (JComponent) super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
MyData data = (MyData)node.getUserObject();
if(data.isX()){
c.setForeground(Color.blue);
c.setBackground(Color.gray);
}
return c;
}
}
Try adding a call to c.setOpaque(true).

JTree set background of node to non-opaque

Please have a look at the SSCCE. How can I make the non-selected tree nodes' background transparent. At the moment the background of non-selected nodes is white. My cell renderer, however, should paint it non-opaque if it is not selected (and green when selected...what it does). In the end I want non-selected nodes to be just text without background, since the area which is red in the SSCCE has a gradient fill in my application.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class SimpleTree extends JFrame
{
public static void main(final String[] args)
{
new SimpleTree();
}
public SimpleTree()
{
super("Creating a Simple JTree");
final Container content = this.getContentPane();
content.setBackground(Color.RED);
final Object[] hierarchy = { "javax.swing", "javax.swing.border", "javax.swing.colorchooser", "javax.swing.event", "javax.swing.filechooser", new Object[] { "javax.swing.plaf", "javax.swing.plaf.basic", "javax.swing.plaf.metal", "javax.swing.plaf.multi" }, "javax.swing.table",
new Object[] { "javax.swing.text", new Object[] { "javax.swing.text.html", "javax.swing.text.html.parser" }, "javax.swing.text.rtf" }, "javax.swing.tree", "javax.swing.undo" };
final DefaultMutableTreeNode root = this.processHierarchy(hierarchy);
final JTree tree = new JTree(root);
tree.setOpaque(false);
tree.setCellRenderer(new MyCellRenderer());
final JScrollPane scroller = new JScrollPane(tree);
scroller.getViewport().setOpaque(false);
scroller.setOpaque(false);
content.add(scroller, BorderLayout.CENTER);
this.setSize(275, 300);
this.setVisible(true);
}
/**
* Small routine that will make node out of the first entry in the array,
* then make nodes out of subsequent entries and make them child nodes of
* the first one. The process is repeated recursively for entries that are
* arrays.
*/
private DefaultMutableTreeNode processHierarchy(final Object[] hierarchy)
{
final DefaultMutableTreeNode node = new DefaultMutableTreeNode(hierarchy[0]);
DefaultMutableTreeNode child;
for (int i = 1; i < hierarchy.length; i++)
{
final Object nodeSpecifier = hierarchy[i];
if (nodeSpecifier instanceof Object[]) // Ie node with children
child = this.processHierarchy((Object[]) nodeSpecifier);
else
child = new DefaultMutableTreeNode(nodeSpecifier); // Ie Leaf
node.add(child);
}
return (node);
}
public class MyCellRenderer extends DefaultTreeCellRenderer
{
#Override
public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus)
{
final Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
final DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (value));
this.setText(value.toString());
if (sel)
{
this.setOpaque(true);
this.setBackground(Color.GREEN);
}
else
{
this.setOpaque(false);
this.setBackground(null);
}
return ret;
}
}
}
You should override getBackgroundNonSelectionColor,getBackgroundSelectionColor and getBackground of DefaultTreeCellRenderer and return appropriate values like so:
public class MyCellRenderer extends DefaultTreeCellRenderer {
#Override
public Color getBackgroundNonSelectionColor() {
return (null);
}
#Override
public Color getBackgroundSelectionColor() {
return Color.GREEN;
}
#Override
public Color getBackground() {
return (null);
}
#Override
public Component getTreeCellRendererComponent(final JTree tree, final Object value, final boolean sel, final boolean expanded, final boolean leaf, final int row, final boolean hasFocus) {
final Component ret = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
final DefaultMutableTreeNode node = ((DefaultMutableTreeNode) (value));
this.setText(value.toString());
return ret;
}
}
which will produce:
Other suggestions:
Create and manipulate Swing components on Event Dispatch Thread.
Dont extend JFrame unnecessarily rather create an instance and use that.
Dont call setSize on JFrame rather use a correct LayoutManager and/or override getPreferredSize() and call pack() on JFrame before setting it visible but after adding all components.
Remember to call JFrame#setDefaultCloseOperation with either DISPOSE_ON_CLOSE or EXIT_ON_CLOSE (DISPOSE_XXX is usually preferred unless using Timers as this will allow main(String[] args) to continue its execution after Gui has been closed).
To avoid background refilling, just put UIManager.put("Tree.rendererFillBackground", false); before new SimpleTree(); or after super("Creating a Simple JTree");.

JTable: How to update cell using custom editor by pop-up input dialog box?

I am learning from this oracle tutorial that uses TableDialogEditDemo.java example class
I wrote a custom cell Renderer and Editor for JTable.
I register them to this Oracle TableDialogEditDemo.java class
...
...
//Set up renderer and editor for the Favorite Color column.
table.setDefaultRenderer(Color.class,
new ColorRenderer(true));
table.setDefaultEditor(Color.class,
new ColorEditor());
TableColumn c = table.getColumnModel().getColumn(2);
c.setCellRenderer(new CellStringRenderer()); //My custom Renderer
c.setCellEditor(new CellStringEditor()); // My custom Editor
//Add the scroll pane to this panel.
add(scrollPane);
...
...
(Updated description)
When I click on a cell an input dialogue box pops up and that is OK, and when I type a text and click "OK" the cell in the JTable is updated, but text is not displayed/rendered correctly, I have to click on any other cell to make the text content displayed correctly in the cell.
What is the wrong with my code?.
My Renderer
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
public class CellStringRenderer extends JLabel implements TableCellRenderer
{
public CellStringRenderer()
{
this.setOpaque(true);
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
String stringValue = (String) value;
this.setText(stringValue);
return this;
}
}
My Editor (Updated)
import java.awt.Component;
import javax.swing.*;
import javax.swing.table.TableCellEditor;
public class CellStringEditor extends AbstractCellEditor
implements TableCellEditor
{
String input;
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
if (isSelected)
{
JOptionPane dialog = new JOptionPane();
input = dialog.showInputDialog(null, "new value");
return dialog;
}
return null;
}
#Override
public Object getCellEditorValue()
{
return input;
}
}
I am The author of this question, and I solved it. I will provide the solution so that others can get help from it.
I was trying to write a custom renderer and a custom editor to use with JTable.
The renderer simply uses JLabel to display data. It is already the standard component for JTable.
The editor is a dialogue box that appears when clicking on the cell that I want to edit.
Here is the solution:
The classes that will remain unchanged are the three classes from Oracle + my custom renderer class
1.TableDialogEditDemo.java.
2.ColorEditor.java
3.ColorRenderer.java
4.CellStringRenderer class provided above in the question body (my class)
The class that will be updated is the custom editor class (I changed its name from "CellStringEditor" to "DialogStringEditor"
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
public class DialogStringEditor extends AbstractCellEditor
implements TableCellEditor,
ActionListener
{
String newInput;
String oldValue;
JButton button;
static final String EDIT = "edit";
public DialogStringEditor()
{
button = new JButton();
button.setBackground(Color.WHITE);
button.setActionCommand(EDIT);
button.addActionListener(this);
button.setBorderPainted(false);
}
#Override
public void actionPerformed(ActionEvent e)
{
if (EDIT.equals(e.getActionCommand()))
{
newInput = JOptionPane.showInputDialog("Edit", oldValue);
if (newInput == null)
{
newInput = oldValue;
}
fireEditingStopped();
}
}
#Override
public Object getCellEditorValue()
{
return newInput;
}
#Override
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
{
newInput = (String) value;
oldValue = (String) value;
return button;
}
}
This will work ok
You can also make a little update for the renderer class "CellStringRenderer" to control how are selction and unselection colors of the cell appear.
update this method:
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
String stringValue = (String) value;
this.setText(stringValue);
if (isSelected)
{
this.setBackground(table.getSelectionBackground());
this.setForeground(table.getSelectionForeground());
} else
{
this.setBackground(table.getBackground());
this.setForeground(table.getForeground());
}
return this;
}
}
Regards.

how to set JTable header background color when a cell is selected

I can successfully set the background color of the entire JTable header.
generally, this looks like this:
m_table.getTableHeader().setBackground(new Color(205,209,235));
where i am getting stuck is when a cell is selected, I have a ListSelectionListener...
I want to set the background of the currently selected columns in the header to another color. (not the background of the current cell, which Is pretty easy)
update:
i created this:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import com.ee.common.StringUtil;
public class myHeaderRenderer extends DefaultTableCellRenderer
{
#Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if( table != null )
{
if ( isSelected ) {
table.getTableHeader().setBackground(Color.orange);
}
else {
table.getTableHeader().setBackground(new Color(205,209,235));
}
}
return null;
}
}
and i try to set it up like this:
class WorksheetTable extends JTable implements EEObjectSource, EEObjectTarget {
public WorksheetTable(WorksheetTableModel model) {
super(model);
setTableHeader(new EditableJTableHeader(getColumnModel()));
}
#Override
public void createDefaultColumnsFromModel() {
super.createDefaultColumnsFromModel();
setTableHeader(new EditableJTableHeader(getColumnModel()));
getTableHeader().setToolTipText("Double Click to Edit, Right Click for more options");
for (int i=0; i<getColumnCount(); i++) {
getColumnModel().getColumn(i).setPreferredWidth(150);
getColumnModel().getColumn(i).setWidth(150);
getColumnModel().getColumn(i).setHeaderRenderer(new myHeaderRenderer());
}
i get Runtime null pointers when the table first paints... missing something silly..
You can go on a JTable:
jTable.getColumModel().getColumn(columnIndex).setHeaderRenderer()
And you can supply you own cell renderer to render headers.

Categories