user3313462
user3313462

Reputation: 11

java swing how to restrict JScrollPane to only vertical

Check out if you can help on this.

I need to restrict the scrolling to only vertical when using JScrollPane.

REMEMBER: not disabling the hortizontal scroll bar by using HORIZONTAL_SCROLLBAR_NEVER, which just disable horizontal. And i need is the components should not go beyond the window horizontally.

Upvotes: 0

Views: 814

Answers (2)

ruba
ruba

Reputation: 120

Add this to the container within the JScrollPane:

@Override
public java.awt.Dimension getPreferredSize() {

    int h = super.getPreferredSize().height;
    int w = getParent().getSize().width;
    return new java.awt.Dimension(w, h);
}

Upvotes: 1

TacoTuesday
TacoTuesday

Reputation: 67

At first I suggest you to check this out.

http://www.java-tips.org/java-se-tips/javax.swing/how-to-use-a-scrollbar-in-both-vertical-and-horizontal-dire.html

and you can actually try these lines of codes as well:

public class AddScroll
  {

  public static void main(String[] args)
  {
    JPanel panel = new JPanel();
    JScrollPane scrollBar = new JScrollPane(panel,
        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    JFrame frame = new JFrame("AddScrollBarToJFrame");
    frame.add(scrollBar);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400, 400);
    frame.setVisible(true);
    }
  }

Upvotes: 0

Related Questions