The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JScrollPane  [3 examples]

e821. Setting the Scrollbar Policy of a JScrollPane Container

A scroll bar in a scroll pane can be set to appear only as needed, always appear, or never appear. By default, both the vertical and horizontal scrollbars in a scroll pane appear only when needed.
    // Create a scrollable text area
    JTextArea textArea = new JTextArea();
    JScrollPane pane = new JScrollPane(textArea);
    
    // Get the default scrollbar policy
    int hpolicy = pane.getHorizontalScrollBarPolicy();
    // JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED;
    
    int vpolicy = pane.getVerticalScrollBarPolicy();
    // JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED;
    
    
    // Make the scrollbars always appear
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
    // Make the scrollbars never appear
    pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);

 Related Examples
e820. Creating a JScrollPane Container
e822. Listening for Scrollbar Value Changes in a JScrollPane Container

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.