Results 1 to 12 of 12
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default JScrollPane scroll setValue failing bigtime (SSCCE included)

    I am having major issues with setting the value of the scrollbar of a JScrollPane and am looking for some major clarification. This is a complilable SSCCE which illustrates my issue. Observe the print statements. I'm having major fails with changing the value of the vertical scrollbar.


    Java Code:
        import javax.swing.*;
        import javax.swing.tree.*;
    
        public class ScrollPaneTester extends JFrame {
    
    	public static void main(String[] args){
    		ScrollPaneTester s = new ScrollPaneTester();
    	}
    	public ScrollPaneTester(){
    		setSize(200, 100);
    		setLocation(400,400);
    
    		JTree tree = new JTree();
    		TreePath treepath = new TreePath(tree.getModel().getRoot());
    		tree.expandPath(treepath);
    
    		JScrollPane scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		scrollpane.setViewportView(tree);
    		add(scrollpane);
    		
    		//setVisible(true);
    
    		System.out.println("ORIGNAL MAXIMUM " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setMaximum(1000);
    		System.out.println("MAXIMUM AFTER SETTING MAXIMUM TO 1000 " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setValue(500);
    		System.out.println("MAXIMUM AFTER SETTING VALUE TO 500 " + scrollpane.getVerticalScrollBar().getMaximum());
    		System.out.println("FINAL VALUE " + scrollpane.getVerticalScrollBar().getValue());
    
    		setVisible(true);
    
    	}
        }
    THE CONSOLE OUTPUT I GET IS THE FOLLOWING:


    Java Code:
    ORIGNAL MAXIMUM 100
    MAXIMUM AFTER SETTING MAXIMUM TO 1000 1000
    MAXIMUM AFTER SETTING VALUE TO 500 72
    FINAL VALUE 72
    As you can see... There is something clearly wrong here. The maximum value is changing from 100 to 1000 to 72(for no apparent reason). Then... if I change the setVisible to another location (try changing setVisible(true) to the location I commented out), it changes the maximum to 13 of all values!! What is this? Why is it doing this?


    PLEASE EXPLAIN WHAT IS GOING ON HERE AND HOW I CAN FIX IT.



    Also, I heard there is a solution invokeLater but my actual program is larger and invokeLater isn't working for my actual program. If there is another solution please let me know.


    Also... why hasn't java fixed this?


    If you want to see the example with the weird 13 value for the scrollbar, this is the alternative code:

    Java Code:
        import javax.swing.*;
        import javax.swing.tree.*;
    
        public class ScrollPaneTester extends JFrame {
    
    	public static void main(String[] args){
    		ScrollPaneTester s = new ScrollPaneTester();
    	}
    	public ScrollPaneTester(){
    		setSize(200, 100);
    		setLocation(400,400);
    
    		JTree tree = new JTree();
    		TreePath treepath = new TreePath(tree.getModel().getRoot());
    		tree.expandPath(treepath);
    
    		JScrollPane scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		scrollpane.setViewportView(tree);
    		add(scrollpane);
    		
    		setVisible(true);
    
    		System.out.println("ORIGNAL MAXIMUM " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setMaximum(1000);
    		System.out.println("MAXIMUM AFTER SETTING MAXIMUM TO 1000 " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setValue(500);
    		System.out.println("MAXIMUM AFTER SETTING VALUE TO 500 " + scrollpane.getVerticalScrollBar().getMaximum());
    		System.out.println("FINAL VALUE " + scrollpane.getVerticalScrollBar().getValue());
    
    		//setVisible(true);
    
    	}
        }

    THE CONSOLE OUTPUT I GET IS THE FOLLOWING:


    Java Code:
    ORIGNAL MAXIMUM 100
    MAXIMUM AFTER SETTING MAXIMUM TO 1000 1000
    MAXIMUM AFTER SETTING VALUE TO 500 72
    FINAL VALUE 13
    Last edited by PrinceSendai; 04-18-2013 at 12:57 AM. Reason: added console output

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    All updates (including the creation of the GUI) should be done on the EDT, which means yes you should use invokeLater(). This has nothing to do with the size of your program. Read the Swing tutorial and you will find example of how to properly create a GUI.

    Maybe start with: How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for the basics. Then you can look at a more complicated example that shows you you can crate a GUI with more components. Maybe "How to Use Labels".

  3. #3
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Quote Originally Posted by camickr View Post
    All updates (including the creation of the GUI) should be done on the EDT, which means yes you should use invokeLater(). This has nothing to do with the size of your program. Read the Swing tutorial and you will find example of how to properly create a GUI.

    Maybe start with: How to Make Frames (Main Windows) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components) for the basics. Then you can look at a more complicated example that shows you you can crate a GUI with more components. Maybe "How to Use Labels".
    Let me start with saying that i am not a JAVA expert, so please ignore my comments when an expert disagrees with me.

    It looks that Oracle has changed the guidelines for using Swing, it used to be:

    - the initial SwingComponent make it on the initial thread pack() and setVisuable(true) can also be on the mainthread.
    - later SwingComponents make them on any thread , but do pack() and setVisuable(true) on the EventDispatchThread
    (by making a runnable and using SwingUtilities invokeLater or SwingUtilities invokeAndWait() )
    - almost everything else related to Swing on the EventDispatchThread. (there are a few threadsafe methods, around a handfull)

    At some time , not sure when or why the guidelines changed

    - the Initial SwingComponent make it on the initial thread even pack() and setVisuable(true) can still be done on the main Thread.
    - later SwingComponents , do everything on the EventDispatchThread
    (not only the pack() and the setVisuable(true) method but everything)
    - almost everything else related to Swing on the EventDispatchThread.

    One of the problems I encountered recently was that even the last bit should be taken with a pinch of salt, what is the use of a safe method if the object itself isn't made yet.

    (I made an JTextArea on the EventDispatchThread, but then its first "threadsafe" setText() method failed, because the JTextArea wasn't made yet :( , nullpointerexception)

    the problem is a bit that all Java examples are only about initial swing Components and the guidelines are hard to follow.
    (do you really want to encapsulate a basic JOptionPane method in an InvokeAndWait Runnable?
    Yep even this static methods of this class are not marked as threadsafe :(

    curious the examples assume there is a (visuable) parent Component, but still don't do it properly by using invokeAndWait

  4. #4
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Thank you so much camickr and willemien for the valuable information you guys provided.


    I sort of get an idea of what you are trying to say about the EDT and why I should try to run it in the EDT.


    That, said, I tried using invokeLater() in the SSCCE below and it still didn't work. Please let me know if I am not using invokeLater() correctly.

    Java Code:
    package Graphics;
    
    import javax.swing.*;
    import javax.swing.tree.*;
    
    public class ScrollPaneTester extends JFrame {
    
    	public static void main(String[] args){
    		ScrollPaneTester s = new ScrollPaneTester();
    	}
    	public ScrollPaneTester(){
    		setSize(200, 100);
    		setLocation(400,400);
    
    		JTree tree = new JTree();
    		TreePath treepath = new TreePath(tree.getModel().getRoot());
    		tree.expandPath(treepath);
    
    		JScrollPane scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		scrollpane.setViewportView(tree);
    		add(scrollpane);
    		
    		//setVisible(true);
    		final JScrollPane sp = scrollpane;
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					System.out.println("ORIGNAL MAXIMUM " + sp.getVerticalScrollBar().getMaximum());
    					sp.getVerticalScrollBar().setMaximum(1000);
    					System.out.println("MAXIMUM AFTER SETTING MAXIMUM TO 1000 " + sp.getVerticalScrollBar().getMaximum());
    					sp.getVerticalScrollBar().setValue(500);
    					System.out.println("MAXIMUM AFTER SETTING VALUE TO 500 " + sp.getVerticalScrollBar().getMaximum());
    					System.out.println("FINAL VALUE " + sp.getVerticalScrollBar().getValue());
    
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    		
    		setVisible(true);
    
    	}
    }


    The console output I get is:


    Java Code:
    ORIGNAL MAXIMUM 100
    MAXIMUM AFTER SETTING MAXIMUM TO 1000 72
    MAXIMUM AFTER SETTING VALUE TO 500 72
    FINAL VALUE 13

    Thank you so much for your help!

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    The code doesn't look like the code from the tutorial links I gave you. Why is it so hard to follow the tutorial?

    Also, I don't even know what you are trying to accomplish.

  6. #6
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Haha. Let me go back and do this again then. :) Thanks.

  7. #7
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    How about this code? Does this make more sense? Am I doing anything wrong?

    Java Code:
    package Graphics;
    
    import javax.swing.*;
    import javax.swing.tree.*;
    
    
    public class ScrollPaneTester extends JFrame {
    
    	public static void main(String[] args){
    		createAndShowGUI();
    	}
    	public static void createAndShowGUI(){
    		ScrollPaneTester s = new ScrollPaneTester();
    		s.setSize(200, 100);
    		s.setLocation(400,400);
    		
    		JTree tree = new JTree();
    		TreePath treepath = new TreePath(tree.getModel().getRoot());
    		tree.expandPath(treepath);
    
    		JScrollPane scrollpane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    		scrollpane.setViewportView(tree);
    		System.out.println("ORIGNAL MAXIMUM " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setMaximum(1000);
    		System.out.println("MAXIMUM AFTER SETTING MAXIMUM TO 1000 " + scrollpane.getVerticalScrollBar().getMaximum());
    		scrollpane.getVerticalScrollBar().setValue(500);
    		System.out.println("MAXIMUM AFTER SETTING VALUE TO 500 " + scrollpane.getVerticalScrollBar().getMaximum());
    		System.out.println("FINAL VALUE " + scrollpane.getVerticalScrollBar().getValue());
    		
    		s.add(scrollpane);
    		
    		s.setVisible(true);
    
    	}
    
    }

    My console output is:


    Java Code:
    ORIGNAL MAXIMUM 100
    MAXIMUM AFTER SETTING MAXIMUM TO 1000 1000
    MAXIMUM AFTER SETTING VALUE TO 500 72
    FINAL VALUE 72

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    I recommend explaining a bit about what exactly you are trying to accomplish.

    Setting the value or maximum of a JScrollBar is a lot more complicated then just setting the values (if you truly wish to try and understand, take a look at the java source code). The behavior you see is most likely not a problem with the main thread/EDT (at least in my hands, although you should follow the advice given above regarding this issue). More than likely its the model, view, component sizes, and UI of the JScrollPane and its components. As a demonstration, immediately after you instantiate your JTree, add one line of code to set the preferred size of the JTree...

    Java Code:
    tree.setPreferredSize(new Dimension(200,1000));
    What I get:
    MAXIMUM AFTER SETTING VALUE TO 500 1000

    Again though, you should more thoroughly explain what you are trying to achieve rather than getting bogged down in the nuances of how many components interact and behave, because often there's a quicker solution than this.
    Last edited by doWhile; 04-18-2013 at 06:48 PM.

  9. #9
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Wow, that's pretty interesting. It seems to set the value correctly.

    So does this means I have to change the preferred size?


    What I actually WANT to do in my actual program, is I want to hide the first row of my JTree, which lies inside a JScrollPane. Because I haven't found a way to hide an arbitrary row, I decided to simply scroll the whole tree downwards a bit, so that the first row would be hidden.

    I will play around with this preferreSize thing and see if it fixes the issue. If you have any other ideas let me know. Thanks a ton!

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    What I actually WANT to do in my actual program, is I want to hide the first row of my JTree, which lies inside a JScrollPane.
    Still not clear about the exact behavior - by hide, how do you wish to toggle visibility? If you wish to truly 'hide' a node, then why add it to the JTree at all? You can toggle its visibility by adding it to the model. Alternatively you can play with the JTree renderer to affect how the Node is rendered. I don't think playing with the JScrollPane is the best way to go about hiding nodes, unless for instance you wish to force the component to scroll to the bottom of a list first, seemingly 'hiding' the first nodes which forces the user to scroll up to see the node.

  11. #11
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Quote Originally Posted by doWhile View Post
    Still not clear about the exact behavior - by hide, how do you wish to toggle visibility? If you wish to truly 'hide' a node, then why add it to the JTree at all? You can toggle its visibility by adding it to the model. Alternatively you can play with the JTree renderer to affect how the Node is rendered. I don't think playing with the JScrollPane is the best way to go about hiding nodes, unless for instance you wish to force the component to scroll to the bottom of a list first, seemingly 'hiding' the first nodes which forces the user to scroll up to see the node.

    Alright, I will try to explain it in detail:


    I have several JScrollPanes which contain JTrees, and they are all aligned and the same size. They are in a JPanel with a BoxLayout on the X_AXIS, and they are all adjacent to each other.


    These JScrollPanes do not have visible scrollbars. However, they can be vertically scrolled using another scrollbar (of another component) which actually synchronizes the scrolling. For example, when I move the vertical scrollbar I mentioned, all of the JScrollPanes that contain the JTrees scroll simultaneously and at the same ratio. SO FAR SO GOOD. The scrolling is smooth and stuff.


    This is where the important part is:


    1. Each JTree contains icons rather than text.
    2. Each JTree has rootVisible set to false.
    3. Out of all of the nodes of each JTree, each node is sort of like a BOX. Suppose that the box is green. When you click on the box (node) it turns to red.
    4. The very first child has a SPECIAL FUNCTION. when you click on the topmost box (the first child node), it marks ALL boxes as either green or red.
    5. Because the first child has a special function, I want this child to ALWAYS REMAIN VISIBLE.


    6. In other words, when I scroll the JTree, I want all of the nodes to scroll up and down, but I want the very first child node to remain fixed in the same position (sort of like the title header of a column).


    7. I assumed (without proof) that this function was not available in Java (making one row of the JTree fixed while the rest was scrollable)


    8. I concluded I needed a JLayeredPane, in which I could add these "header nodes" on top of the scrolling tree, and leave them on top of the tree so that they would always be visible.
    9. However, the JLayeredPane approach brought a lot of extra work, which I ultimately decided to avoid (I was having issues with the display, the sizes of components, and also the transparency)


    10. I decided to, instead, create TWO JScrollPane arrays, one that represents ONLY the first children of every JTree, and a second JScrollPane array which showed the scrollable children (from the second child, up to the last child)


    11. The question I am asking relates to the second array of JScrollPanes.
    12. Ideally, it would be nice if I simply DID NOT INCLUDE the first node... thus I wouldn't need to hide it.
    13. However, based on my program, I simply can't delete the first node because it messes with ALL OF MY INDEXES, and it is a large program and I don't want to mess with indexes. For example, index 1 represents the first real normal BOX (because the heading box is a special function box). If I deleted the first node, this index would be 0 and would screw up many parts of my program.



    14. Finally, I concluded that I needed a way to perpetually HIDE the first node of the tree... I concluded I could do this by scrolling the whole JTree down a bit... so as to conceal the node.
    15. BY THE WAY, and this is important, I HAVE IMPLEMENTED the AdjustmentListener so that it automatically hides the first node by scrolling it down a bit. AND IT WORKS, but only within the AdjustmentListener (only when the event is fired.)


    16. Since it works in the AdjustmentListener I concluded it should work when I try to scroll the trees at the beginning of my program.

    17. But when I try to scroll from the begining of my program, it doesn't do it. It doesn't work.


    Is this clear enough? Maybe it's a little complicated.


    I have basically split my JTrees into two: column header, and elements.

    I cannot remove the column header from the elements tree because it screwes up my indexes.

    So I decided I should constantly hide it by scrolling down the length of one node.


    THank you so much.

  12. #12
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default Re: JScrollPane scroll setValue failing bigtime (SSCCE included)

    Basically, the only reason I am including the first node, is because removing it would cause the indexes of my program to be messed up. And that would be a lot of delicate modifications I would need to do with the indexes.

    I basically want to hide the node without removing it or having it affect the indexes of the other nodes.


    OR... as I mentioned above, if I could possibly figure out a way to make the first row of the JTree FIXED while the rest is SCROLLABLE (sort of like a column header), then that would solve my issue as well and I wouldn't need to hide the node and create separate components for the "column header" as I am doing here.

Similar Threads

  1. JscrollPane and scroll Bug
    By jviure in forum AWT / Swing
    Replies: 0
    Last Post: 01-16-2013, 04:06 PM
  2. Replies: 5
    Last Post: 09-28-2011, 05:43 PM
  3. Replies: 1
    Last Post: 04-05-2011, 12:12 PM
  4. Moving Scroll in JScrollPane
    By ayesh85 in forum AWT / Swing
    Replies: 0
    Last Post: 03-02-2011, 08:11 AM
  5. jscrollpane scroll issues
    By kumar_gemi in forum AWT / Swing
    Replies: 11
    Last Post: 09-30-2009, 08:33 PM

Tags for this Thread

Posting Permissions

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