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

Thread: Use jbutton to scroll through a jscrollpane

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Use jbutton to scroll through a jscrollpane

    Howdy, I'm currently writing a java program which replicates a GPS, on the points of interest screen for Airports, Restaurants etc I have each entry set as a separate jbutton, organized using a gridlayout (1 column listing all entries) and finally the user is able to scroll through the entries (jscrollpanel). I have 2 arrow buttons for up/down and I want to use them to scroll through the list of entries. Is this possible?

    Cheers
    Josh


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Use jbutton to scroll through a jscrollpane

    So you have a row of JButtons you want to scroll through? What happens when you add them all to a JPanel, then add the JPanel to a JScrollPane?

    Recommended reading: How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Use jbutton to scroll through a jscrollpane

    Quote Originally Posted by KevinWorkman View Post
    So you have a row of JButtons you want to scroll through? What happens when you add them all to a JPanel, then add the JPanel to a JScrollPane?

    Recommended reading: How to Use Scroll Panes (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    Sorry, I didn't word my question very well. The jButtons have already been added to a jPanel, and the jPanel to the jScrollpane. I have two separate jButtons to the right of this jScrollpane which I want to use to scroll the jScrollpane up/down, is this possible?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Use jbutton to scroll through a jscrollpane

    Get the appropriate JScrollBar of the JScrollPane, and set its value based upon its current value.
    In pseudo-code that can be implemented in a JButton action listener:
    myScrollPane.getVerticalScrollBar().setValue( myScrollPane.getVerticalScrollBar().getValue() + myScrollPane.getVerticalScrollBar().getUnitIncrement());

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Use jbutton to scroll through a jscrollpane

    Thanks, that helped heaps. I got it to work by doing this:

    JButton upButton = new JButton("Up");
    upButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0){
    scrollPane.getVerticalScrollBar().setBlockIncrement(50);
     
    scrollPane.transferFocusDownCycle();
    scrollPane.getVerticalScrollBar().setValue( scrollPane.getVerticalScrollBar().getValue() - scrollPane.getVerticalScrollBar().getBlockIncrement());
     
    }
    });
     
    JButton downButton = new JButton("Down");
     
    downButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent arg0){
    scrollPane.getVerticalScrollBar().setBlockIncrement(50);
     
    scrollPane.transferFocusDownCycle();
    scrollPane.getVerticalScrollBar().setValue( scrollPane.getVerticalScrollBar().getValue() + scrollPane.getVerticalScrollBar().getBlockIncrement());
     
    }
    });

Similar Threads

  1. [SOLVED] JScrollPane cant paint correctly when scroll
    By fuweng in forum AWT / Java Swing
    Replies: 4
    Last Post: December 6th, 2011, 01:27 PM
  2. [SOLVED] JComboBox with scroll bars.
    By Melawe in forum AWT / Java Swing
    Replies: 6
    Last Post: October 12th, 2011, 12:22 AM
  3. 1 vertical scroll bar fro 2 jtextareas
    By fortune2k in forum AWT / Java Swing
    Replies: 8
    Last Post: March 4th, 2011, 02:04 PM
  4. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM
  5. Java Program to add scroll bars to JTextArea using JScrollPane
    By Flash in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 21st, 2009, 03:41 AM