2

I want to implement a Scrollbar onto my Tab. However nothing is showing and there are no exceptions.

I think I need a:

scrollPane.setViewportView(scrollPanel);

But it didn't work as well.

I am wondering when adding a Jscrollpane onto a JTab how do you set it visible without using an explicit frame. If I use a frame and add it on the frame it creates a new window. However how I got this program the Frame looks built I assume and this complicates everything.

import java.awt.*;
import javax.swing.*;

public class Test extends JFrame {

    private     JTabbedPane tabbedPane;
    private     JPanel      panel; // Page where I want JScrollPane intisialized

    public Test()
    {
        setTitle( "Program" );
        setSize( 400, 200 ); // I want the JScrollPane to extend to 400 vertically


        JPanel topPanel = new JPanel();
        topPanel.setLayout( new BorderLayout() );
        getContentPane().add( topPanel );

        // Create the tab pages
        createPage1();

        tabbedPane = new JTabbedPane();
        tabbedPane.addTab( "Welcome", panel );
        topPanel.add( tabbedPane, BorderLayout.CENTER );    
    }

    public void createPage1()
    {
        panel = new JPanel();
        panel.setLayout( null ); // sets layout to null

////////////////////////
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(null);
scrollPanel.setPreferredSize(new Dimension(400,400));
///////////////////////

        panel.add(scrollPanel);
        scrollPanel.setVisible (true);

    }

    public static void main( String args[] )
    {
        // Create an instance of the test application
        Test mainFrame  = new Test();
        mainFrame.setVisible( true );
    }
}

If you have any questions don't hesitate to ask.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jebathon
  • 4,310
  • 14
  • 57
  • 108
  • 2
    Yes, one question, why don't you use a layout manager? – Reimeus Apr 10 '13 at 15:52
  • 1
    Also, there's nothing in your `scrollpanel` panel, so it's probably hard for you to tell when it's visible. And you're not actually creating a `JScrollPane`, so there won't be a scrollbar. – Rob I Apr 10 '13 at 15:58
  • I feel null layout is more flexible for what I intend to do. TBH your right- I need to incorperate one. Il most probably use BorderLayout in the future for everything.. – Jebathon Apr 10 '13 at 16:09
  • simple: never-ever code any ui without a LayoutManager. – kleopatra Apr 10 '13 at 16:15

1 Answers1

5

What you want is to use a JScrollPane. Change the createPage1() method to something like this:

public void createPage1()
{
    panel = new JPanel();
    panel.setLayout( new BorderLayout() );

    ////////////////////////
    JScrollPane scrollPanel = new JScrollPane();
    scrollPanel.setViewportView(new JLabel("hellossssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"));
    scrollPanel.setPreferredSize(new Dimension(400,400));
    ///////////////////////

    panel.add(scrollPanel,BorderLayout.CENTER);
}

And you will see a scrollbar. Note this change encompasses four things:

  1. replace the null layout call with a BorderLayout
  2. make a JScrollPane instead of a JPanel
  3. add something to the pane for demo purposes
  4. remove the unnecessary setVisible(true) call.
Rob I
  • 5,627
  • 2
  • 21
  • 28
  • a LayoutManager definitely is the way to go, but you'll never want to [fiddle with the sizing hints](http://stackoverflow.com/a/7229519/203657) (aka: setXXSize with XX = min/pref/max) – kleopatra Apr 10 '13 at 16:18
  • Sorry, I had only pasted part of the code needed - replace the whole method with my edited answer's code. And @kleopatra - definitely a true point! – Rob I Apr 10 '13 at 16:19