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 6 of 6

Thread: Scroll Positioning Problem

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Scroll Positioning Problem

    Heyo, guys! I've got a problem that really has me scratching my head here. I have a JTable inside a JScrollPane. My code (below) adds an entry to the table, then tells the scroll pane to scroll to the bottom entry. The problem is that it never scrolls to the bottom entry, only to the *second* to bottom one. I suspect that some sort of odd timing error is going on, but can't figure it out. Any assistance would be really appreciated!

    private void addProcGuideWithValues(String base, String proc) {
            procTableModel.addRow(new Object[]{base, proc}); // adding object to the table's table model
     
            JScrollBar bar = sclProcGuide.getVerticalScrollBar(); // grabbing scroller object
            bar.setValue(bar.getMaximum()); // setting scroll value to its theoretical maximum
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scroll Positioning Problem

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    I don't have much experience here, but here are some ideas: Behavior that ALWAYS occurs is a good thing, because that gives us consistency we can work with. If your current code always moves to the next to last (same as second to bottom?) then increase the maximum so that it moves down one more. Or, the behavior suggests that the maximum needs to be updated to recognize that a change has occurred before calling getMaximum(), so see if there's a way to update the values before calling getMaximum().

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Scroll Positioning Problem

    Good thought, but I've already attempted that. There's even a getBlockIncrement() function on the scrollbar (example of that non-working code below), which represents the int value of one entry's height. When I add that, I get nothing at all. I am thinking that the actual addition of the height to the scroll bar is somehow delayed. If that is the case, then the code is *correctly* dropping the scroll bar to the bottom, but then *incorrectly* adding the additional line to the table afterward.

    Nonworking Code:
    private void addProcGuideWithValues(String base, String proc) {
            procTableModel.addRow(new Object[]{base, proc}); // adding object to the table's table model
     
            JScrollBar bar = sclProcGuide.getVerticalScrollBar(); // grabbing scroller object
            bar.setValue(bar.getMaximum() [B]bar.getBlockIncrement()[/B]); // setting scroll value to its theoretical maximum
    }

    Also, thanks for the warm welcome! ^^

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scroll Positioning Problem

    Sorry, I just don't spend much time with tables. In fact, I avoid them and Regex at all costs. Someone smarter than I will come along.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Scroll Positioning Problem

    It probably happens because the update to the model and the update to the scrollbars maximum value do not happen simultanously. I might be wrong, because I do not know for sure, but this explanation makes a lot of sense to me since Swing is not thread safe and usually events are enqueued on the EDT.

    One possible solution would be to add a listener to your model and, when an insertion event is fired, scroll the scrollbar. Possibly within an InvokeLater on the EDT just to make sure it will happen after the JTable got the event.

  6. The Following 2 Users Say Thank You to Cornix For This Useful Post:

    GregBrannon (August 2nd, 2014), Sedu (August 1st, 2014)

  7. #6
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Scroll Positioning Problem

    Cornix, thank you for the suggestion! Invokelater was exactly what I needed here. Below is my corrected code, now working correctly. This is a really good trick to remember...

    private void addProcGuideWithValues(String base, String proc) {
            procTableModel.addRow(new Object[]{base, proc}); // adding object to the table's table model
     
            // perform this action later, once the scroll object is properly updated
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JScrollBar bar = sclProcGuide.getVerticalScrollBar();
                    bar.setValue (bar.getMaximum() + bar.getBlockIncrement());
                }
            });
    }

Similar Threads

  1. JButton Positioning:
    By jocdrew21 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 21st, 2014, 10:35 AM
  2. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  3. Positioning problem HELP!?
    By R4DiCaL in forum What's Wrong With My Code?
    Replies: 8
    Last Post: May 6th, 2012, 10:42 AM
  4. button positioning
    By pds8475 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 29th, 2011, 09:35 AM
  5. Positioning elements. Is it possible without layouts?
    By goodguy in forum AWT / Java Swing
    Replies: 6
    Last Post: January 21st, 2011, 02:24 PM