Results 1 to 1 of 1
  1. #1
    hemanthjava is offline Member
    Join Date
    Dec 2007
    Rep Power

    Default JSplitPane Arranging Components(JTree) Doubt

    I was working on a JTree Example.

    The program is shown below for convenience sake

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSplitPane;
    import javax.swing.JTree;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    import javax.swing.tree.TreeSelectionModel;
    public class DynamicJTreeDemo extends JFrame{
    	private static int newNodeCounter;
    	private static String ADD_ACTION = "add";
    	private static String REMOVE_ACTION = "remove";
    	private static String CLEAR_ACTION = "clear";
    	private DefaultMutableTreeNode rootNode;
    	private DefaultTreeModel defaultTreeModel;
    	private JTree jtree;
    	public DynamicJTreeDemo() {
    		//    	Creates the JTree
    		rootNode = new DefaultMutableTreeNode("Root Node");
    		defaultTreeModel = new DefaultTreeModel(rootNode);
    		jtree = new JTree(defaultTreeModel);
    		JScrollPane jscrollPane = new JScrollPane(jtree);
    		JPanel treePanel = new JPanel();
    		JPanel buttonPanel = new JPanel();
    		JButton jbnAdd = new JButton("Add");
    //		jbnAdd.addActionListener(this);
    		buttonPanel.add(jbnAdd, BorderLayout.WEST);
    		JButton jbnRemove = new JButton("Remove");
    //		jbnRemove.addActionListener(this);
    		buttonPanel.add(jbnRemove, BorderLayout.SOUTH);
    		JButton jbnClear = new JButton("Clear");
    //		jbnClear.addActionListener(this);
    		buttonPanel.add(jbnClear, BorderLayout.EAST);
    		JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
    				treePanel, buttonPanel);
    		//Lay everything out.
    		//        setPreferredSize(new Dimension(300, 150));
    	private static void createAndShowGUI() {
    		DynamicJTreeDemo dynamicJTreeDemo = new DynamicJTreeDemo();
    		dynamicJTreeDemo.setTitle("Dynamic JTree Demo");
    	public void initializeTree() {
    		String p1 = new String("Parent 1"); //p1 = Parent 1
    		String p2 = new String("Parent 2");
    		String c1 = new String("Child 1");
    		String c2 = new String("Child 2");
    		DefaultMutableTreeNode p1Node = null, p2Node = null;
    		p1Node = addObject(null, p1, false);
    		p2Node = addObject(null, p2, false);
    		addObject(p1Node, c1, false);
    		addObject(p1Node, c2, false);
    		addObject(p2Node, c1, false);
    		addObject(p2Node, c2, false);
    	public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
    			Object child, boolean isVisible) {
    		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
    		if (parent == null) {
    			parent = rootNode;
    		defaultTreeModel.insertNodeInto(childNode, parent, parent
    		if (isVisible) {
    			jtree.scrollPathToVisible(new TreePath(childNode.getPath()));
    		return childNode;
    	public static void main(String[] args) {
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    The output shows a JSplitPane with JTree on the top and button Panel in the bottem. The JTree is not well aligned and I am not able to align it entirely in the top panel. I don't even know how by default the output jtree is so small when there is no size setting. How do I correct this program.

    Source of the Program is from iwebie
    Last edited by hemanthjava; 08-03-2008 at 08:31 AM. Reason: removed the unwanted parts of the program

Similar Threads

  1. A Simple JTree Example
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:41 PM
  2. JSplitPane nightmare
    By SwinGirl in forum SWT / JFace
    Replies: 1
    Last Post: 05-01-2008, 08:24 PM
  3. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 PM
  4. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:28 AM
  5. I need create a JSplitPane
    By Daniel in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2007, 06:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts