How to set insets inside JScrollPane object?
Asked Answered
V

1

5

I have JScrollPane which contains only one instance of JTree. How can I set margin around JTree inside of JScrollPane?

I have such a code

tree.setPreferredSize(new Dimension(200, 200));
    JScrollPane scrollerTree = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollerTree.setPreferredSize(new Dimension(200, 199));
    scrollerTree.getVerticalScrollBar().setUnitIncrement(16);

I want to achieve, that margin would be scrollable with scrollpane content.

Vang answered 7/8, 2012 at 8:9 Comment(1)
@Andrew: thanks for advice. But I don't understand, I accepted answers almost in each question I have made and also in some question from another users.Vang
V
13

Update: From your updated question I reckon that you are actually after JComponent.setBorder, example screenshot:

screenshot border

Code:

public static void main(String[] args) {

    JTree tree = new JTree();
    tree.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JFrame frame = new JFrame("Test");
    frame.add(new JScrollPane(tree));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

Original answer, if you want a border around the view port, use JScrollPane.setViewportBorder:

screenshot viewport border

Code:

public static void main(String[] args) {

    JScrollPane scroll = new JScrollPane(new JTree());
    scroll.setViewportBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

    JFrame frame = new JFrame("Test");
    frame.add(scroll);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
Vinegarroon answered 7/8, 2012 at 8:15 Comment(3)
thank you. I forgot to explain that I want to achieve, that margin would be scrollable with scrollpane content.Vang
I have already found solution: I have set border to scrollable object: tree.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));Vang
I already added that example. Good that you got that aswell! :) (Takes a while to get the screenshots up :))Vinegarroon

© 2022 - 2024 — McMap. All rights reserved.