2014-07-25 22:50:21

Jtree

隐藏root:jtree.setRootVisible(false);

2:

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
public class Admin extends JFrame{
	private static final long serialVersionUID = 1L;
	private JTree createJTree() {
		JTree jTree = new JTree();
		jTree.setRootVisible( false );
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("root node, should be invisible");
		DefaultTreeModel defaultTreeModel = new DefaultTreeModel( root );
		jTree.setModel( defaultTreeModel );
		DefaultMutableTreeNode parentNode; 
		DefaultMutableTreeNode node;
		parentNode = (DefaultMutableTreeNode) defaultTreeModel.getRoot(); 
		node = new DefaultMutableTreeNode( "node 1" );
		addNodeToDefaultTreeModel( defaultTreeModel, parentNode, node );
		parentNode = node; 
		node = new DefaultMutableTreeNode( "node 2" );
		addNodeToDefaultTreeModel( defaultTreeModel, parentNode, node );
		parentNode = (DefaultMutableTreeNode) defaultTreeModel.getRoot(); 
		node = new DefaultMutableTreeNode( "node 3" );
		addNodeToDefaultTreeModel( defaultTreeModel, parentNode, node );
		parentNode = node; 
		node = new DefaultMutableTreeNode( "node 4" );
		addNodeToDefaultTreeModel( defaultTreeModel, parentNode, node );
		return jTree;
	}
	private static void addNodeToDefaultTreeModel( DefaultTreeModel treeModel, DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node ) {
		treeModel.insertNodeInto(node, parentNode, parentNode.getChildCount());
		if (parentNode == treeModel.getRoot()) {
			treeModel.nodeStructureChanged((TreeNode) treeModel.getRoot());
		}
	}
	public static void main(String[] args) {
		JFrame j = new JFrame("Test");
		j.setSize(500,300);
		j.setLocation(500,300);
		j.setLayout(new BorderLayout());
		Admin a = new Admin();
		JTree t = a.createJTree();
		j.add(t);
		j.setVisible(true);
	}
}

2:

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class Admin extends JFrame{
	private static final long serialVersionUID = 1L;
	public static void main(String[] args) {
		JFrame j = new JFrame("Test");
		j.setSize(500,300);
		j.setLocation(500,300);
		JTree t = new JTree();
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
		root.add(new DefaultMutableTreeNode("123"));
		root.add(new DefaultMutableTreeNode("456"));
		DefaultTreeModel model = new DefaultTreeModel (root);
		t.setModel(model);
		t.setRootVisible(false);
		j.add(t);
		j.setVisible(true);
	}
}

3:

jtree.expandRow(0);
jtree.setRootVisible(false);

4:

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
public class Admin extends JFrame {
	private static final long serialVersionUID = 1L;
	public void test(){
		JFrame j = new JFrame("Test");
		j.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		j.setSize(500, 300);
		j.setLocation(500, 300);
		JTree t = new JTree(){
			private static final long serialVersionUID = 1L;
			@Override
			public void paintComponent(Graphics g) {
			   super.paintComponent(g);
			   g.drawLine(0, 20, 10, 8);
			}
		};
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
		DefaultMutableTreeNode d1 =  new DefaultMutableTreeNode("11111");
		d1.add(new DefaultMutableTreeNode("222"));
		root.add(d1);
		root.add(new DefaultMutableTreeNode("4562"));
		DefaultTreeModel model = new DefaultTreeModel(root);
		t.putClientProperty("JTree.lineStyle", "Horizontal");
		t.setCellRenderer(new DefaultTreeCellRenderer() {
			private static final long serialVersionUID = 1L;
			public Component getTreeCellRendererComponent(JTree tree, Object value,boolean isSelected,boolean expanded,boolean leaf, int row,boolean hasFocus) {
				Component renderer = super.getTreeCellRendererComponent(tree, value, isSelected, expanded, leaf, row, hasFocus);
                Color foreground, background = getBackground();
                foreground = Color.BLACK;
                setBackgroundNonSelectionColor(Color.RED);
                if (isSelected) {
                    setBackgroundNonSelectionColor(background);
                } else {
                	setBackgroundSelectionColor(background);
                    background = new Color(217,236,254);
                }
                if (value instanceof JLabel) {
                    this.setText("1345676");
                    this.setIcon(((JLabel) value).getIcon());
                }
                super.setBackground(Color.yellow);
                renderer.setForeground(foreground);
                renderer.setBackground(background);
                return renderer;
			}
			ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("images/toolbar_new.png"));
			public Icon getClosedIcon() {
				return icon;
			}
			public Icon getLeafIcon() {
				return icon;
			}
			public Icon getOpenIcon() {
				return icon;
			}
		});
		t.setOpaque(true);
//		t.setBackground(Color.RED);
		t.setModel(model);
		t.setRootVisible(false);
		j.add(t);
		j.setVisible(true);
	}
	public static void main(String[] args) {
		new Admin().test();
	}
}

jtree展开和获取节点问题:

import java.awt.Dimension;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
/**
 *
 * @author yz
 */
public class MainFrame extends javax.swing.JFrame {
	public MainFrame() {
        initComponents();
    }
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
        jTree1 = new javax.swing.JTree(rootNode);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        jScrollPane1.setViewportView(jTree1);
        /** add some dummy data */
        DefaultMutableTreeNode prNode = new DefaultMutableTreeNode("a"); //project
        DefaultMutableTreeNode pNode = new DefaultMutableTreeNode("a1"); //package
        pNode.add(new DefaultMutableTreeNode("AutoscrollTextArea.java"));
        DefaultMutableTreeNode p2Node = new DefaultMutableTreeNode("euphoria.utils");
        p2Node.add(new DefaultMutableTreeNode("Timer.java"));
        pNode.add(p2Node);
        prNode.add(pNode);
        rootNode.add(prNode);
        rootNode.add(new DefaultMutableTreeNode("Hello"));
        TreeModel tm = jTree1.getModel();
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tm.getRoot();
        DefaultMutableTreeNode tn = (DefaultMutableTreeNode) root.getFirstChild();
        jTree1.expandPath(new TreePath(tn.getPath()));
        jTree1.expandRow(0);
        jTree1.setRootVisible(false);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 431, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE)
        );
        int screenWidth = java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenHeight = java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
        Dimension d = getPreferredSize();
        this.setBounds(screenWidth / 2 - (d.width/2), screenHeight / 2 - (d.height/2), d.width, d.height);
        pack();
    }// </editor-fold>                        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration                   
}
发表回复