Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbars not appearing in JScrollPane

I want to make a window with two buttons with great heights and a scrollbar on a side. The problem is that no scrollbar appears. Here is my code

public class Window {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //namestanje teme
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedLookAndFeelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JFrame frame = new JFrame("frame");
//  frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel(null);

    JButton but1 = new JButton();
    JButton but2 = new JButton();

    panel.add(but1);
    panel.add(but2);

    but1.setSize(50, 505);
    but2.setSize(50, 505);

    but1.setLocation(0, 0);
    but2.setLocation(400, 400);

    but1.setText("1");
    but2.setText("2");


    JScrollPane scroll = new JScrollPane(panel);

    frame.add(scroll);
    frame.setVisible(true);         
}
}

Note: At first, the buttons had large widths (did that by naming them with something like "11111111111111111111111111111") and a scrollbar would appear. Then I wanted large heights and had to put null in panel. Now no scrollbar appears.

like image 895
Damjan Avatar asked Jul 10 '11 15:07

Damjan


People also ask

Is JScrollPane horizontal scrollbar child field?

It is scrollpane's horizontal scrollbar child. It displays policy for the horizontal scrollbar. This displays the lower left corner. This displays in the lower right corner.

How do I add JScrollPane?

Create the panel and scrollpane like: JPanel panel = new JPanel(); JScrollPane scrollPane = new JScrollPane( panel );


1 Answers

Scrollbars appear when the preferred size of the component added to the scollpane is greater than the size of the scrollpane.

It is the job of the layout manager to determine the preferred size of the panel. It is also the job of the layout manager to determine the size and location of the components added to the panel.

Get rid of the null layout and use a layout manager and scrollbars will appear when required automatically.

If you want components to be displayed differently from a vertical point of view, then you need to use a different layout manager. Maybe you can use a BoxLayout with a verticxal layout. You can use:

panel.add( Box.createVerticalStrut(400) );

to add vertical space between the two components.

like image 95
camickr Avatar answered Nov 04 '22 16:11

camickr