hi leute!!
wie kann man einen Tree automatisch öffnen. Damit bei der Ausgabe gleich der JTree geöffnet is und man nicht erst einmal drauf klicken muss?
wie kann man einen Tree automatisch öffnen. Damit bei der Ausgabe gleich der JTree geöffnet is und man nicht erst einmal drauf klicken muss?
Code:
package com.cbm.vis.util;
import java.awt.*;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.*;
import javax.swing.*;
import javax.swing.JTable.PrintMode;
import javax.swing.tree.*;
import com.cbm.util.CBMIConstant;
import com.cbm.vis.cashbook.CBM_KBDelete;
import com.cbm.vis.cashbook.CBM_KBEntry;
import com.cbm.vis.cashbook.CBM_KBList;
import com.cbm.vis.cashbook.CBM_KBSearch;
import com.cbm.vis.cmanagment.CBM_CustomerList;
import com.cbm.vis.cmanagment.CBM_Customerentry;
import com.cbm.vis.cmanagment.CBM_Customerdelete;
import com.cbm.vis.main.CBMMain;
/****************************************************
* Name: JTree mit Buttons *
* Author: Thomas Rockenschaub *
* Date: 30.12.2006 *
* Modified Date: 5.1.2007 *
* Version: 2.0 [Beta] *
* ------------------------------------------------*
* Description: Tree um auf die diversen Funktion *
* des Programms zugreifen zu können *
***************************************************/
public class CBMJTree
implements CBMIConstant
{
/* Konstante für die Größe des JTrees im Hauptfenster */
private static final int m_sizex = 200;
private CBM Kassabuch[] = {
new CBM(" KB Eintrag "),
new CBM(" KB Editieren "),
new CBM(" KB Löschen "),
new CBM(" KB Auflisten "),
new CBM(" KB Drucken ") };
private CBM Kundenverwaltung[] = {
new CBM(" Kundeneintrag "),
new CBM(" Kunden Editieren "),
new CBM(" Kunden Löschen "),
new CBM(" Kunden Auflisten "),
new CBM(" Kunden Drucken ") };
private CBM Administrator[] = {
new CBM(" AD Einstellungen "),
new CBM(" AD Benutzer "),
new CBM(" AD Login/Logout "),
new CBM(" AD Informationen ")};
private Vector KBVector;
private Vector KVVector;
private Vector ADVector;
private Vector rootVector;
private JTree tree;
private JScrollPane scroll;
public CBMJTree()
{
KBVector = new NameVector("Kassabuch ", Kassabuch);
KVVector = new NameVector("Kundenverwaltung", Kundenverwaltung);
ADVector = new NameVector("Administration ", Administrator);
Object rootNodes[] = { KBVector, KVVector,ADVector };
rootVector = new NameVector("Root", rootNodes);
tree = new JTree(rootVector);
TreeCellRenderer renderer = new CBMCellRenderer();
tree.setCellRenderer(renderer);
scroll = new JScrollPane(tree);
scroll.setMinimumSize(new Dimension(m_sizex,m_fgry));
}
public Component getJScrollPane()
{
return scroll;
}
}
class CBM
{
String title;
public CBM(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
}
class CBMCellRenderer
implements TreeCellRenderer
{
private JLabel titleLabel;
private boolean choose=true;
private int value1, value2;
private JPanel renderer;
private DefaultTreeCellRenderer defaultRenderer = new DefaultTreeCellRenderer();
private Color backgroundSelectionColor;
private Color backgroundNonSelectionColor;
private JButton button;
public CBMCellRenderer()
{
renderer = new JPanel(new FlowLayout(FlowLayout.RIGHT,6,6));
titleLabel = new JLabel(" ");
titleLabel.setForeground(Color.blue);
button=new JButton();
renderer.add(button);
backgroundSelectionColor = defaultRenderer.getBackgroundSelectionColor();
backgroundNonSelectionColor = defaultRenderer.getBackgroundNonSelectionColor();
}
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 NameVector )
{
String name = ((NameVector)userObject).toString();
button.setText( name );
returnValue = renderer;
if(selected)
{
renderer.setBackground(backgroundSelectionColor);
}
else
{
renderer.setBackground(backgroundNonSelectionColor);
}
renderer.setEnabled(tree.isEnabled());
}
if (userObject instanceof CBM)
{
CBM book = (CBM) userObject;
button.setText(book.getTitle());
if (selected)
{
renderer.setBackground(backgroundSelectionColor);
String react = book.getTitle();
if(choose==false)
{
if(react.equals(" KB Eintrag "))
value2=1;
else if(react.equals(" KB Editieren "))
value2=2;
else if(react.equals(" KB Löschen "))
value2=3;
else if(react.equals(" KB Drucken "))
value2=4;
else if(react.equals(" KB Auflisten "))
value2=5;
else if(react.equals(" Kundeneintrag "))
value2=6;
else if(react.equals(" Kunden Editieren "))
value2=7;
else if(react.equals(" Kunden Löschen "))
value2=8;
else if(react.equals(" Kunden Drucken "))
value2=9;
else if(react.equals(" Kunden Auflisten "))
value2=10;
else if(react.equals(" AD Einstellungen "))
value2=11;
else if(react.equals(" AD Benutzer "))
value2=12;
else if(react.equals(" AD Login/Logout "))
value2=13;
else if(react.equals(" AD Informationen "))
value2=14;
if(value1!=value2)
choose=true;
}
//Hier kann jetzt auf die Buttons reagiert werden
if(choose==true)
{
if(react.equals(" Kundeneintrag "))
{
System.out.println(""+react);
CBM_Customerentry Custentry=new CBM_Customerentry();
CBMMain.setrightPanelComponent2(Custentry.getMainJPanel());
value1=6;
choose=false;
}else if(react.equals(" Kunden Editieren "))
{
System.out.println(""+react);
value1=7;
choose=false;
}else if(react.equals(" Kunden Löschen "))
{
System.out.println(""+react);
CBM_Customerdelete Custdelete=new CBM_Customerdelete();
CBMMain.setrightPanelComponent2(Custdelete);
value1=8;
choose=false;
}else if(react.equals(" Kunden Auflisten "))
{
System.out.println(""+react);
CBM_CustomerList ShowCust=new CBM_CustomerList();
CBMMain.setrightPanelComponent2(ShowCust.getMainJPanel());
value1=10;
choose=false;
}else if(react.equals(" Kunden Drucken "))
{
System.out.println(""+react);
value1=9;
choose=false;
}else if(react.equals(" KB Eintrag "))
{
System.out.println(""+react);
CBM_KBEntry entry=new CBM_KBEntry();
CBMMain.setrightPanelComponent2(entry);
value1=1;
choose=false;
}else if(react.equals(" KB Editieren "))
{
System.out.println(""+react);
CBM_KBSearch entry=new CBM_KBSearch();
CBMMain.setrightPanelComponent2(entry);
value1=2;
choose=false;
}else if(react.equals(" KB Löschen "))
{
CBM_KBDelete entry=new CBM_KBDelete();
CBMMain.setrightPanelComponent2(entry);
System.out.println(""+react);
value1=3;
choose=false;
}else if(react.equals(" KB Auflisten "))
{
CBM_KBList entry=new CBM_KBList();
CBMMain.setrightPanelComponent2(entry);
System.out.println(""+react);
value1=5;
choose=false;
}else if(react.equals(" KB Drucken "))
{
new CBMPrintPreview();
System.out.println(""+react);
value1=4;
choose=false;
}else if(react.equals(" AD Einstellungen "))
{
System.out.println(""+react);
value1=11;
choose=false;
}else if(react.equals(" AD Benutzer "))
{
System.out.println(""+react);
value1=12;
choose=false;
}else if(react.equals(" AD Login/Logout "))
{
System.out.println(""+react);
value1=13;
choose=false;
}else if(react.equals(" AD Informationen "))
{
System.out.println(""+react);
value1=14;
choose=false;
}
}
}
else
{
renderer.setBackground(backgroundNonSelectionColor);
}
renderer.setEnabled(tree.isEnabled());
returnValue = renderer;
}
}
if (returnValue == null)
{
returnValue = defaultRenderer.getTreeCellRendererComponent(tree,value, selected, expanded, leaf, row, hasFocus);
}
return returnValue;
}
}
class NameVector
extends Vector
{
private static final long serialVersionUID = 1L;
String name;
public NameVector(String name)
{
this.name = name;
}
public NameVector(String name, Object elements[])
{
this.name = name;
for (int i = 0, n = elements.length; i < n; i++)
{
add(elements[i]);
}
}
public String toString()
{
return name;
}
}