17

I want a JPanel with a size and defined position. Inside the JPanel, I've certain number of elements (buttons) inserted horizontally. Because my JPanel has a defined width, if I add much buttons, I couldn't see that. In this case, I need a scrollbar for this JPanel. But this JPanel must be a CERTAIN SIZE IN A CERTAIN POSITION inside a JFrame. The scrollbar of the JPanel has positioned under it horizontally. Someone can help me? I've tried it without success!

Chu
  • 468
  • 1
  • 5
  • 21
  • AFAIK, the point is since your `JPanel` has one `Defined Width/Height/Size`, so once you add anything to it, which goes beyond it's size, you cannot see that thing, even with `JScrollPane` added to it, since even `JScrollPane` cannot go beyond `JPanel`'s size, so those components are hidden always :( . Since if you wont' define it's size, the components are first added to the `JPanel` and then it's `preferredSize()` is determined implicitly by `doLayout()` sort of thingies, hence your `JScrollPane` works in that situation, but not here. – nIcE cOw Apr 27 '12 at 07:53
  • 1
    Also consider [`JToolBar`](http://docs.oracle.com/javase/tutorial/uiswing/components/toolbar.html) or a [suitable layout](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Without an [sscce](http://sscce.org/) that clarifies the meaning of _certain size_ and _certain position_, this question cannot be answered in it's present form. – trashgod Apr 27 '12 at 09:09

3 Answers3

22

Use a JScrollPane and force its preferredSize to your given size (or set the scrollPane container LayoutManager to null and call setBounds() on the scrollpane). Also set the scrollbar policies. Here is a small sample of that:

import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Test {

    public static void main(String... args) {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        for (int i = 0; i < 10; i++) {
            panel.add(new JButton("Hello-" + i));
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.setBounds(50, 30, 300, 50);
        JPanel contentPane = new JPanel(null);
        contentPane.setPreferredSize(new Dimension(500, 400));
        contentPane.add(scrollPane);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Emm, the problem is that I need to locate the JPanel in a specific position of the JFrame. Additionally, it must has a definite size. – Chu Apr 27 '12 at 07:46
  • @BrallanAguilar either you use a LayoutManager to position and size your components, either set the LayoutManager to null and call setBounds() to position and size your component. In this case, you must do that on the JScrollPane – Guillaume Polet Apr 27 '12 at 07:48
  • Can you help me with an example, please? – Chu Apr 27 '12 at 07:52
  • @BrallanAguilar I edited my post to show how it is possible to force location and size of the JScrollPane – Guillaume Polet Apr 27 '12 at 07:57
  • Hey, I've a question: why you put the layout of the contentPane null? – Chu Apr 27 '12 at 20:00
  • @BrallanAguilar As I said, either you use a LayoutManager and let him position and size the components (based on min/max/pref size and constraints), either your set the LayoutManager to null, like I did, and you force the size and position (the bounds) yourself. – Guillaume Polet Apr 27 '12 at 20:56
6

Add all the buttons to your panel. Then add this panel to scrollpane.

panel.add(button1);
panel.add(button2);
panel.add(button3);
.
.
.
panel.add(buttonn);

Once you add buttons in panel, add panel to scrollpane.

JScrollPane panelPane = new JScrollPane(panel);

And then add this panel to your frame.

Sachin Mhetre
  • 4,465
  • 10
  • 43
  • 68
4

Here are some possible solutions:

or

  • for scrolling to the decision Point or Rectangle there exists the method scrollRectToVisible e.g. myPanel.scrollRectToVisible(myButton.getBounds());
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319