Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Adding a JScrollPane to a JTree

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding a JScrollPane to a JTree

    I can't get the scrollpane to appear here:

    package oldschool.runescape.com;
     
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.regex.Pattern;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.border.EmptyBorder;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeModel;
     
    public class Worlds extends JFrame {
     
    	private static final long serialVersionUID = 445871530648558835L;
     
    	private JPanel contentPane;
    	private Map<String, Integer> worlds = new HashMap<String, Integer>(); 
    	private String site = "http://oldschool.runescape.com/slu.ws?order=WpMLA";
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					Worlds frame = new Worlds();
    					frame.setVisible(true);
    					frame.prepareWorlds();
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	private void prepareWorlds() {
    		final Pattern pattern = Pattern.compile("d.write");
    		try {
    			URL url = new URL(site);
    			URLConnection curl = url.openConnection();
    			curl.setDoInput(true);
    			InputStream instr = curl.getInputStream();
    			String s;
    			if (instr != null) {
    				BufferedReader in = new BufferedReader(new InputStreamReader(
    						instr));
    				while ((s = in.readLine()) != null) {
    					if (pattern.matcher(s).find()) {
    						while ((s = in.readLine()) != null) {
    							if (s == null || s.equalsIgnoreCase("") || s.equalsIgnoreCase(" ")) continue;
    							if (s.equals("</script>")) break;
    							String data = s;
     
    							final Pattern school = Pattern.compile("oldschool.*?");
    							if (school.matcher(data).find()) {
    								String[] newStrings = data.split("\"");
    								String name = newStrings[1];
    								int count = Integer.parseInt(data.split(",")[4]);
    								worlds.put(name, count);
    							}
    						}
    					}
    				}
    			}
    			for (Entry<String, Integer> v : worlds.entrySet()) {
    				dropsNode.add(new DefaultMutableTreeNode(v.getKey()));
    			}
    		} catch (Throwable t) {
    			t.printStackTrace();
    		}
    	}
     
    	private DefaultMutableTreeNode dropsNode = new DefaultMutableTreeNode("Worlds");
    	private DefaultTreeModel dropsTreeModel = new DefaultTreeModel(dropsNode);
    	private JTree tree = new JTree();
    	private JScrollPane treeScroll = new JScrollPane();
     
    	/**
    	 * Create the frame.
    	 */
    	public Worlds() {
    		prepareWorlds();	
     
     
    		setTitle("07 World Picker");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 414, 295);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		JButton btnGo = new JButton("GO!");
    		btnGo.setBounds(125, 222, 129, 23);
    		contentPane.add(btnGo);
     
    		JLabel lblPickAWorld = new JLabel("Pick a world:");
    		lblPickAWorld.setBounds(10, 11, 129, 23);
    		contentPane.add(lblPickAWorld);
     
    		tree.setBounds(125, 14, 129, 114);
    		tree.setModel(dropsTreeModel);
     
    		treeScroll.add(tree);
    		contentPane.add(tree);
     
    		btnGo.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				try {
    					//RS2Loader.startUp(tree.getSelectionModel().getSelectionPath().getLastPathComponent());
    				} catch (Exception e1) {
    					e1.printStackTrace();
    				}
    				setVisible(false);
    			}
    		});
    	}
    }

    It appears like this:



  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding a JScrollPane to a JTree

    What is wrong with the image you posted? What do you want different?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a JScrollPane to a JTree

    There is no scroll bar, theres more than 4 values in the tree but they arent visible.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding a JScrollPane to a JTree

    You should look at the API doc on how to use the JScrollPane. There is a link to the tutorial there.

    The first thing I see is that the JScrollPane object is not added to a container in the GUI.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a JScrollPane to a JTree

    		contentPane.add(treeScroll);

    Makes no difference. Am I doing something wrong in my code?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding a JScrollPane to a JTree

    Did you read the API doc to see how to use the JScrollPane?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding a JScrollPane to a JTree

    Quote Originally Posted by Norm View Post
    Did you read the API doc to see how to use the JScrollPane?
    Yes I have I'm still hopelessly lost as to what I'm doing wrong. I've been searching for hours on end. Can you just tell me what to do

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Adding a JScrollPane to a JTree

    Look at how the code in the API doc uses a JScrollPane to hold a component to be displayed and use that same technique.
    http://docs.oracle.com/javase/tutori...crollpane.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. problems on implementing a JTree
    By shenhuang in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 23rd, 2012, 10:51 AM
  2. [SOLVED] Need help with my JTree add JScrollPane
    By justyStepi in forum AWT / Java Swing
    Replies: 13
    Last Post: April 26th, 2012, 05:04 PM
  3. JTree DnD Problem
    By hafunui in forum AWT / Java Swing
    Replies: 3
    Last Post: August 12th, 2011, 10:33 PM
  4. JTree not expanding?
    By captain alge in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 03:32 AM
  5. [SOLVED] Jtree help
    By sman36 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 6th, 2010, 09:39 AM