![]() |
| | Themen-Optionen | Thema durchsuchen |
| | Nach oben #1 |
| Gast
Beiträge: n/a
|
Hallo zusammen! Ich wollte den Pfad meines JTree in dem ich mich grad befinde Fett unterlegen, weiss aber nicht wie. Fett unterlegen kann man mit Code: setFont(getFont().deriveFont(Font.BOLD)); Dachte man kommt etwa so an den kompletten Pfad Code: public TreeNode[] getPathToRoot(TreeNode aNode); Der Code meiner Klasse: Code:
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
/**
* The type TreeIconRenderer is responsible for changing the tree icons.
*
* @author smokie */
public class TreeIconRenderer extends DefaultTreeCellRenderer {
// declaring the Icons
Icon leafIcon;
ImageIcon openIcon;
ImageIcon closedIcon;
public TreeIconRenderer() {
super();
// getting image box
leafIcon = new ImageIcon(getClass().getResource("/images/system/box.gif"));
// getting image arrow down
openIcon = new ImageIcon(getClass().getResource("/images/system/arrow_down.gif"));
// getting image arrow right
closedIcon = new ImageIcon(getClass().getResource("/images/system/arrow.gif"));
}
/**
* Creating the component in the GridBagLayout.
*
* @param tree tree
* @param value cell value
* @param selected closed tree icon
* @param expanded opened tree icon
* @param leaf end point icon
* @param row number of row
* @param hasFocus the focus
*/
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);
// setting icons
setLeafIcon(leafIcon);
setOpenIcon(openIcon);
setClosedIcon(closedIcon);
setBackgroundSelectionColor(new Color(0, 0, 128));
putClientProperty("JTree.lineStyle", "None");
// setFont(getFont().deriveFont(Font.BOLD));
setBackground(new Color(212, 217, 231));
return this;
}
}
|
|
| | Nach oben #2 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.160
|
Könnte funktionieren: Code: import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
/**
* The type TreeIconRenderer is responsible for changing the tree icons.
*
* @author smokie */
public class TreeIconRenderer extends DefaultTreeCellRenderer {
// declaring the Icons
Icon leafIcon;
ImageIcon openIcon;
ImageIcon closedIcon;
public TreeIconRenderer() {
super();
// getting image box
leafIcon = new ImageIcon(getClass().getResource("/images/system/box.gif"));
// getting image arrow down
openIcon = new ImageIcon(getClass().getResource("/images/system/arrow_down.gif"));
// getting image arrow right
closedIcon = new ImageIcon(getClass().getResource("/images/system/arrow.gif"));
}
/**
* Creating the component in the GridBagLayout.
*
* @param tree tree
* @param value cell value
* @param selected closed tree icon
* @param expanded opened tree icon
* @param leaf end point icon
* @param row number of row
* @param hasFocus the focus
*/
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);
// setting icons
setLeafIcon(leafIcon);
setOpenIcon(openIcon);
setClosedIcon(closedIcon);
setBackgroundSelectionColor(new Color(0, 0, 128));
putClientProperty("JTree.lineStyle", "None");
TreePath selectionPath = tree.getSelectionPath();
TreePath nodePath = ((DefaultTreeModel)tree.getModel()).getPathToRoot((TreeNode)value);
if(selectionPath == nodePath || nodePath.isDescentant(selectionPath)) {
setFont(getFont().deriveFont(Font.BOLD));
}
setBackground(new Color(212, 217, 231));
return this;
}
}
__________________ Patrick Gotthardts Weblog. |
| | |
| | Nach oben #5 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.160
|
Ändere die Zeile mal zu: Code: TreePath nodePath = new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot((TreeNode)value)); Code: if(selectionPath == nodePath || nodePath.isDescentant(selectionPath)) {
Code: if(selectionPath.equals(nodePath) || nodePath.isDescentant(selectionPath)) {
__________________ Patrick Gotthardts Weblog. |
| | |
| | Nach oben #6 |
| Gast
Beiträge: n/a
| java.lang.NullPointerException at de.weidmueller.view.TreeIconRenderer.getTreeCellRe ndererComponent(TreeIconRenderer.java:7 at javax.swing.plaf.basic.BasicTreeUI$NodeDimensionsH andler.getNodeDimensions(Unknown Source) at javax.swing.tree.AbstractLayoutCache.getNodeDimens ions(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache$TreeSta teNode.updatePreferredSize(Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.rebuild (Unknown Source) at javax.swing.tree.VariableHeightLayoutCache.setMode l(Unknown Source) at javax.swing.plaf.basic.BasicTreeUI.setModel(Unknow n Source) at javax.swing.plaf.basic.BasicTreeUI$PropertyChangeH andler.propertyChange(Unknown Source) at javax.swing.event.SwingPropertyChangeSupport.fireP ropertyChange(Unknown Source) at javax.swing.event.SwingPropertyChangeSupport.fireP ropertyChange(Unknown Source) at javax.swing.JComponent.firePropertyChange(Unknown Source) at javax.swing.JTree.setModel(Unknown Source) |
|
| | Nach oben #9 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.160
|
Passiert, wenn es keine Selection gibt. Code: if(selectionPath != null && (selectionPath.equals(nodePath) || nodePath.isDescendant(selectionPath))) {
__________________ Patrick Gotthardts Weblog. |
| | |
| | Nach oben #12 |
| Projektleiter Registriert seit: 30.11.2005 Ort: Bottrop
Beiträge: 1.160
|
Pack doch einfach ein "else" hinzu. Ich würde die Schrift sowieso irgendwie lokal speichern. Ist sonst zu zeitraubend.
__________________ Patrick Gotthardts Weblog. |
| | |
| | Nach oben #13 |
| Gast
Beiträge: n/a
|
Hey, du bist gut. Jetzt klappts. Code: // setting icons
setLeafIcon(leafIcon);
setOpenIcon(openIcon);
setClosedIcon(closedIcon);
setBackgroundSelectionColor(new Color(0, 0, 128));
putClientProperty("JTree.lineStyle", "None");
TreePath selectionPath = tree.getSelectionPath();
TreePath nodePath = new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot((TreeNode)value));
if(selectionPath != null && (selectionPath.equals(nodePath) || nodePath.isDescendant(selectionPath))) {
setFont(getFont().deriveFont(Font.BOLD));
}
else setFont(getFont().deriveFont(Font.PLAIN));
setBackground(new Color(212, 217, 231));
![]() 1000 Dank!!! |
|
![]() |
| Lesezeichen |
| Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1) | |
| Themen-Optionen | Thema durchsuchen |
| |
Ähnliche Themen | ||||
| Thema | Autor | Forum | Antworten | Letzter Beitrag |
| Pfad automatisch rauskriegen | Creativ | PHP-Programmierung | 2 | 02.09.2006 14:31 |
| JScrollPane und JTree | javra | Desktop-Applikationen und Grafik | 10 | 19.02.2006 10:39 |
| Absoluten Pfad zu einer Datei ermitteln... | cancer | PHP-Programmierung | 5 | 17.12.2005 16:40 |
| JTree wird nicht aktualisiert | Waldi5001 | Desktop-Applikationen und Grafik | 7 | 08.12.2005 15:31 |
| JTree Farbe | smokie | Desktop-Applikationen und Grafik | 6 | 06.07.2005 15:14 |