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

Thread: JScrollPane not showing scroll bars

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JScrollPane not showing scroll bars

    Hi everyone,

    I'm developing a new Java aplication and my JScrollPane doesn't show the scroll bars.

    I'm able to see the JScrollPane border and i can add elements to it, but when those elements pass beyond the given size i have no way to see them because the scroll bars aren't showing up.

    Here is the source code i'm using for the scrollPane:

    public void Display(Rectangle location) {       
            Color menu_color = null;  
            display = new JScrollPane();
            display.setLayout(null);
            display.setBackground(menu_color);       
            display.setBounds(location);
            display.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            display.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            display.setVisible(true);
            display.setViewportBorder(new LineBorder(Color.BLACK));
            content.add(display); // Container
         }

    Can anyone give me a hand?

    Thanks


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: JScrollPane not showing scroll bars

    For the love of all things good, *never* set the layout of a JScrollPane to null. Just don't do it. JScrollPane uses a special layout, ScrollPaneLayout, that allows it to handle its viewport properly, and by setting the layout to null, you've in effect nullified this, and have prevented your JScrollPane from working.

    In fact you should avoid using null layouts if at all possible for all of your Swing coding.

    Next of all, to see the JScrollPane's scrollbars, you will need to add a component into its viewportView, and this component will need to be larger than the JScrollPane itself. Note that you don't add components directly to the JScrollPane itself. It may seem like you do this, since you can pass the component into the JScrollPane via its constructor parameter, but that's just syntactic sugar that actual adds the component to the viewportView.
    Last edited by Fubarable; July 4th, 2012 at 11:13 AM.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane not showing scroll bars

    Thanks for the info Furable.

    I changed the layout to:

     display.setLayout(new ScrollPaneLayout());

    However, I'm not very experienced in working with viewports .

    I will try to find an example.

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: JScrollPane not showing scroll bars

    Quote Originally Posted by Stx View Post
    Thanks for the info Furable.

    I changed the layout to:

     display.setLayout(new ScrollPaneLayout());
    No, just leave JScrollPane's layout alone. It is already set to the correct layout and so messing with it only invites trouble.

    However, I'm not very experienced in working with viewports .

    I will try to find an example.
    Your best bet is to go to the scrollpane tutorial.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane not showing scroll bars

    Thanks Fubarable.

    I got the scroll bars working.

    However, with the solution i have i can't add a JLabel to my text area, I can only append text.

           Color menu_color = null;  
            menu = new JTextArea();
            menu.setEditable(false);
            menu.setBackground(menu_color);
            JScrollPane display = new JScrollPane(menu);
            display.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            display.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);     
            display.setBounds(location);
            content.add(display);

    If i do area.add(JLabel), this doesn't show up.

    Got any ideas on how i can successfully add JLabels to the textarea?

    Thanks

  6. #6
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: JScrollPane not showing scroll bars

    Quote Originally Posted by Stx View Post
    Thanks Fubarable.

    I got the scroll bars working.
    Great, and you're welcome!

    However, with the solution i have i can't add a JLabel to my text area, I can only append text.
    That's what JTextAreas are for -- they are for displaying text and text alone. You can't and shouldn't try to add JLabels to JTextAreas (not without a lot of unnecessary gymnastics). There are other text components that allow this, but first before exploring this, why do you even want to do this? What is your ultimate goal?
    Last edited by Fubarable; July 4th, 2012 at 01:25 PM.

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane not showing scroll bars

    I created some JLabels that contain formatted text (with position, font color).

    For example:

    JLABEL header = (TeamA TeamB Score)
    header.setBounds(screen_center);
    header.setFont(black);

    JLAbel data = (PlayerX PlayerY 1-1)
    data.setBounds(screen_center+height); // add some space between header and data
    data.setFont(black);

    This JLabel is constructed based on a database.

    Since I already have the data in a JLAbel, my goal was to put the already constructed JLAbel in the textarea.

    I didnt find any way to put the direct text that exists in the database in a determined position of the textarea and to give it some color.

  8. #8
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: JScrollPane not showing scroll bars

    I'm thinking that you're almost trying in a round about way to create your own version of JTable. Why not simply use a JTable directly to display your data? You can use headers and such with this creature.

  9. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JScrollPane not showing scroll bars

    Ok, i think I understand your advice: instead of having a jtextarea, i create a JTable put my data in it and then create a scroll pane with the jtable right?

  10. #10
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: JScrollPane not showing scroll bars

    Yep, that's correct.

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. Use jbutton to scroll through a jscrollpane
    By joshacurtis in forum AWT / Java Swing
    Replies: 4
    Last Post: November 2nd, 2011, 01:38 AM
  3. [SOLVED] JComboBox with scroll bars.
    By Melawe in forum AWT / Java Swing
    Replies: 6
    Last Post: October 12th, 2011, 12:22 AM
  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