Results 1 to 2 of 2

Thread: Split the JTree

  1. #1
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default Split the JTree

    Hi All

    I wonder if someone can help. I'm trying to split the JTree into two JPanels so the Root and "Parents" can be in one JPanel and the "Child" can be in another. Folders(root & parents) are on left and Files(child) are in centre. I've managed to do it so it's error free but the result is a "dot" displays where root/parents should be and an area of textfield displays where Files are meant to be. why ??? why can't I see the Folders(root & parent) and files(child). Many thanks in advance
    Zed:confused:
    Java Code:
    //Import packages
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.event.TreeSelectionEvent;
    import javax.swing.event.TreeSelectionListener;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.TreeSelectionModel;
    import java.net.URL;
    import java.io.IOException;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import javax.swing.tree.*;
     
    public class Repository {
      
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("Soft Office - Repository");
        frame.setContentPane(new ContentPanel().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        //frame.setSize(1025, 740);
        frame.setVisible(true);
      }
       public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }
    
    class ContentPanel {
      private JPanel mainPanel = new JPanel();
      private CenterPanel cPanel = new CenterPanel(); 
      
      public ContentPanel() {
        mainPanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        mainPanel.setLayout(new BorderLayout());
    
        JComponent cntPanel = cPanel.getComponent();
        cntPanel.setBorder(BorderFactory.createTitledBorder("You Outcomes"));
        mainPanel.add(cntPanel, BorderLayout.CENTER);    
    }
     
      public JComponent getComponent() {
        return mainPanel;
      }
    }
    /***************************************** Center Panel ************************************************/
    
    class CenterPanel implements TreeSelectionListener {
    	
        JPanel mainPanel = new JPanel();
        private JEditorPane htmlPane;
        private JTree tree;
        private URL helpURL;
        private static boolean DEBUG = false;
        private static boolean playWithLineStyle = false;
        private static String lineStyle = "Horizontal";
        private static boolean useSystemLookAndFeel = true;
    	public CenterPanel(){
    		JPanel folderPanel = createFolderPanel();
    		JPanel filePanel = createFilePanel();
    		mainPanel.add(folderPanel, BorderLayout.WEST);
    		mainPanel.setLayout(new GridLayout(1,0));
    		mainPanel.add(filePanel, BorderLayout.CENTER);
            //Create the nodes.
            DefaultMutableTreeNode fldPanel =
                new DefaultMutableTreeNode("Repository");
            createNodes(fldPanel);
            //Create a tree that allows one selection at a time.
            tree = new JTree(fldPanel);
            tree.getSelectionModel().setSelectionMode
                    (TreeSelectionModel.SINGLE_TREE_SELECTION);
            //Listen for when the selection changes.
            tree.addTreeSelectionListener(this);
            if (playWithLineStyle) {
                System.out.println("line style = " + lineStyle);
                tree.putClientProperty("JTree.lineStyle", lineStyle);
    	}
    }
    	private JPanel createFolderPanel(){
    		JPanel folderPanel = new JPanel();
         	folderPanel.setBorder(BorderFactory.createTitledBorder("Folders"));
    		//Create the scroll pane and add the tree to it. 
            JScrollPane treeView = new JScrollPane(tree);
    	        //Create the HTML viewing pane.
            htmlPane = new JEditorPane();
            htmlPane.setEditable(false);
            initHelp();
            folderPanel.add(treeView);
    	return folderPanel;
    	}
    	private JPanel createFilePanel(){
    		JPanel filePanel = new JPanel();
         	filePanel.setBorder(BorderFactory.createTitledBorder("Files"));
    		JScrollPane htmlView = new JScrollPane(htmlPane);
    		filePanel.add(htmlView);
           	return filePanel;
    	}
    	/** Required by TreeSelectionListener interface. */
        public void valueChanged(TreeSelectionEvent e) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                               tree.getLastSelectedPathComponent();
            if (node == null) return;
            Object nodeInfo = node.getUserObject();
            if (node.isLeaf()) {
                RepositoryInfo repository = (RepositoryInfo)nodeInfo;
                displayFiles(repository.fileSubject);
                if (DEBUG) {
                    System.out.print(repository.fileSubject + ":  \n    ");
                }
            } else {
                displayFiles(helpURL); 
            }
            if (DEBUG) {
                System.out.println(nodeInfo.toString());
            }
        }
            private class RepositoryInfo {
            public String fileName;
            public URL fileSubject;
            public RepositoryInfo(String file, String filename) {
                fileName = file;
                fileSubject = getClass().getResource(filename);
                if (fileSubject == null) {
                    System.err.println("Couldn't find file: "
                                       + filename);
                }
            }
            public String toString() {
                return fileName;
            }
        }
        private void initHelp() {
            String s = "Repository.html";
            helpURL = getClass().getResource(s);
            if (helpURL == null) {
                System.err.println("Couldn't open help file: " + s);
            } else if (DEBUG) {
                System.out.println("Help URL is " + helpURL);
            }
    
            displayFiles(helpURL);
        }
        private void displayFiles(URL url) {
            try {
                if (url != null) {
                    htmlPane.setPage(url);
                } else { //null url
    		htmlPane.setText("File Not Found");
                    if (DEBUG) {
                        System.out.println("Attempted to display a null helpURL.");
                    }
                }
            } catch (IOException e) {
                System.err.println("Attempted to read a bad helpURL: " + url);
            }
        }
        private void createNodes(DefaultMutableTreeNode fldPanel) {
            DefaultMutableTreeNode category = null;
            DefaultMutableTreeNode repository = null;
            category = new DefaultMutableTreeNode("Inbox");
            fldPanel.add(category);
            repository = new DefaultMutableTreeNode(new RepositoryInfo
                ("test",
                "test"));
            category.add(repository);
            category = new DefaultMutableTreeNode("Outbox");
            fldPanel.add(category);
                repository = new DefaultMutableTreeNode(new RepositoryInfo
                ("test 2",
                 "test 2"));
            category.add(repository);
    
           }
        
      public JComponent getComponent() {
        return mainPanel;
      }
    }
    Also posted in sun java forum.
    Last edited by zed420; 01-02-2010 at 08:11 PM.

  2. #2
    zed420 is offline Member
    Join Date
    Dec 2009
    Location
    Leeds, UK
    Posts
    12
    Rep Power
    0

    Default

    Can I just ask, Is this even possible to split JTree like I'm trying to do ???
    Thanks
    Zed

Similar Threads

  1. string split
    By gisler in forum New To Java
    Replies: 6
    Last Post: 12-17-2009, 03:23 PM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  3. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 09:27 PM
  4. How To Use Split In Java
    By glenwood in forum New To Java
    Replies: 1
    Last Post: 09-03-2008, 02:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:32 PM

Posting Permissions

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