Hi,
I have a problem of modifying Jtree font. No matter what I do or make changes in code I'm unable to change the default font. I got the TreeCellRenderer code from some website. Any suggesion on font change would be really helpful
class MyCellRenderer extends JEditorPane implements TreeCellRenderer
{
JEditorPane dummy = new JEditorPane();
final Font TABLE_FONT = new Font("SansSerif", Font.PLAIN, 12);
final Font TABLE_FONT_SELECTED = new Font("SansSerif", Font.BOLD, 12);
final Font TABLE_FONT_ROOT = new Font("SansSerif", Font.ITALIC, 12);
MyCellRenderer() {
setContentType("text/html");
dummy.setContentType("text/html");
dummy.setFont(new FontUIResource("Helvetica",Font.BOLD,2));
dummy.setFont(new Font("Helvetica",Font.BOLD,2));
dummy.setBackground(UIManager.getColor(test.p6.getBackground()));
}
public Component getTreeCellRendererComponent(javax.swing.JTree tree,Object obj,boolean isSelected,
boolean isExtended,
boolean isLeaf, int
row,
boolean hasFocus)
{
setFont(new Font("Tahoma", Font.BOLD, 12));
setText( (String) obj.toString());
setEditable(false);
setBackground(test.p6.getBackground());
return this;
}
public Dimension getPreferredSize() {
dummy.setText(getText());
dummy.setSize(w - offset, Integer.MAX_VALUE);
Dimension d = dummy.getPreferredScrollableViewportSize();
return new Dimension(w - offset, d.height);
}
}
class MyCellEditor
extends JTextPane
implements TreeCellEditor {
JEditorPane dummy = new JEditorPane();
final Font TABLE_FONT_SELECTED = new Font("SansSerif", Font.BOLD, 12);
public MyCellEditor() {
this.setContentType("text/html");
dummy.setContentType("text/html");
}
public Component getTreeCellEditorComponent(javax.swing.JTree tree,
Object value,
boolean isSelected,
boolean isExpanded,
boolean isLeaf,
int row
)
{
this.setFont(TABLE_FONT_SELECTED);
String txt = value.toString();
this.setText(txt);
this.setEditable(false);
setFont(getFont().deriveFont(Font.BOLD));
this.setBackground(test.p6.getBackground());
return this;
}
public Dimension getPreferredSize() {
dummy.setText(getText());
dummy.setFont(TABLE_FONT_SELECTED);
dummy.setSize(w - offset, Integer.MAX_VALUE);
Dimension d = dummy.getPreferredScrollableViewportSize();
return new Dimension(w - offset, d.height);
}
public void cancelCellEditing() {
this.setEditable(false);
}
public boolean isCellEditable(EventObject event) {
return true;
}
public Object getCellEditorValue() {
setFont(getFont().deriveFont(Font.BOLD));
return this.getText();
}
public boolean shouldSelectCell(EventObject event) {
return false;
}
public void addCellEditorListener(CellEditorListener l) {}
public void removeCellEditorListener(CellEditorListener l) {}
public boolean stopCellEditing() {
return true;
}
}
public void tree4motif(final String data4tree)
{
test.pmotif.removeAll();
//System.out.println(data4tree);
String[] para = data4tree.split("@");
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Motifs");
DefaultMutableTreeNode motif_name = null;
DefaultMutableTreeNode motif_value = null;
DefaultMutableTreeNode motif_qname = null;
DefaultMutableTreeNode motif_ori = null;
DefaultMutableTreeNode motif_style = null;
DefaultMutableTreeNode motif_parentname = null;
DefaultMutableTreeNode motif_parenttransform = null;
DefaultMutableTreeNode Result= null;
DefaultMutableTreeNode Readout= null;
DefaultMutableTreeNode qualifiers = null;
for (String y : para)
{
String[] fin = y.split("#");
for (String data : fin)
{
String[] l = data.split(":");
if(l[0].trim().equals("motif name"))
{
motif_name = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0))+ l[0].trim().substring(1)+": " +l[1].trim());
root.add(motif_name);
}
else if(l[0].trim().equals("value"))
{
motif_value = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0))+ l[0].trim().substring(1)+": " +l[1].trim());
qualifiers.add(motif_value);
}
else if(l[0].trim().equals("name"))
{
motif_qname = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0)) + l[0].trim().substring(1)+": " +l[1].trim());
motif_value.add(motif_qname);
}
else if(l[0].trim().equals("ori"))
{
motif_ori = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0)) + l[0].trim().substring(1)+": " +l[1].trim());
motif_value.add(motif_ori);
}
else if(l[0].trim().equals("style"))
{
motif_style = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0)) + l[0].trim().substring(1)+": " +l[1].trim());
motif_value.add(motif_style);
}
else
{
motif_parentname = new DefaultMutableTreeNode(Character.toUpperCase(l[0].trim().charAt(0)) + l[0].trim().substring(1)+": " +l[1].trim());
motif_name.add(motif_parentname);
qualifiers = new DefaultMutableTreeNode("Qualifiers");
motif_name.add(qualifiers);
}
}
}
motifs_tree = new JTree(root);
Color color = test.p6.getBackground ();
motifs_tree.setBackground(color);
GridBagConstraints gbc1 = new GridBagConstraints ();
gbc1.insets = new Insets(2, 1, 2, 1);
gbc1.fill = GridBagConstraints.BOTH;
gbc1.weightx = 1.0;
gbc1.weighty = 1.0;
gbc1.gridwidth = GridBagConstraints.EAST;
gbc1.anchor = GridBagConstraints.WEST;
if(test.pmotif.getWidth() == 0)
{
w = 280;
}
else
{
w = test.pmotif.getWidth()-60;
}
MyCellRenderer re = new MyCellRenderer();
re.setFont(new Font("Helvetica",Font.BOLD,2));
motifs_tree.setCellRenderer(re);
motifs_tree.setRowHeight( -1);
BasicTreeUI basicTreeUI = (BasicTreeUI) motifs_tree.getUI();
basicTreeUI.setRightChildIndent(15);
basicTreeUI.setLeftChildIndent(9);
Rectangle rectangle = motifs_tree.getPathBounds(motifs_tree.getPathForRow(2));
try
{
xOffset = 2*rectangle.x;
}
catch(Exception das)
{
xOffset = 2*24;
}
test.pmotif.setFont(new Font("Helvetica",Font.BOLD,2));
motifs_tree.setFont(new Font("Helvetica",Font.BOLD,2));
test.pmotif.add(motifs_tree,gbc1);
test.pmotif.setVisible(true);
test.pmotif.revalidate();
expandTree(motifs_tree, true);
}
thanks