0

So, I have a JPanel, panelWButtons, which contains a list of buttons. panelWButtons has a size of (350, 130). I was wondering how would I add a vertical scrollbar to the right side of the JPanel? So that if the buttons are exceeding the length of the panel, a scrollbar will appear Much help would be appreciated. Here is a snippet of my code:

final JPanel panelWButtons = new JPanel();
panelWButtons.setPreferredSize(new Dimension(350, 130));

JScrollPane scroller = new JScrollPane(panelWButtons);  //is this right?
add(scroller, BorderLayout.CENTER);
D.Maximov
  • 53
  • 1
  • 8
  • 2
    I'm voting to close this question as off-topic because the answer can easily be found by reading one or more good tutorials, like [How to Use Scroll Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html) or any number of possible related questions on SO – MadProgrammer May 21 '16 at 22:40
  • I tried searching online, but it didn't work. And I thought that someone could help me. – D.Maximov May 21 '16 at 22:41
  • "*//is this right?"* what happens when you run it? Hint, this `panelWButtons.setPreferredSize(new Dimension(350, 130));` is going to screw with you – MadProgrammer May 21 '16 at 22:41
  • It shows the panel with the buttons inside, but then I can see half of them as they start to overlap the length of the jpanel. That is why I wanted to use a scrollbar. – D.Maximov May 21 '16 at 22:42
  • Hint: `panelWButtons.setPreferredSize(new Dimension(350, 130));` is going to screw with you. [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer May 21 '16 at 22:43
  • Then what should I do then? I don't want to extend my JFrame with my mouse as the buttons are added and overlapping. – D.Maximov May 21 '16 at 22:45
  • The size of the container should be determined by it's content. Make use of appropriate layout managers which allow you to fill out the container the way you want to, make use of multi layered containers to generate complex layouts, utilising `EmptyBorder`s and layout constraints to provide spacing – MadProgrammer May 21 '16 at 22:47
  • But do you know how to add a scrollbar to the right(east) side of a jpanel? I tried all day and I can't figure it out. – D.Maximov May 21 '16 at 22:48
  • `JScrollPane scroller = new JScrollPane(panelWButtons);`, but my hint is what's causing you problems – MadProgrammer May 21 '16 at 23:03
  • My buttons just go in a single row out of the panel when I take out the preffered size part. – D.Maximov May 21 '16 at 23:07
  • I put them in flowlayout because I like how they are seperated, not joined together. That is why I have a size to the panel, so the buttons don't go in a single row out of the panel. – D.Maximov May 21 '16 at 23:08
  • Try using something like ['WrapLayout`](https://tips4java.wordpress.com/2008/11/06/wrap-layout/) which fixes this issue – MadProgrammer May 21 '16 at 23:09
  • I heard about the wrap layout, I'll try using that. Now, if I use that layout, will the scrollbar appear? Because currently nothing is happening. – D.Maximov May 21 '16 at 23:12
  • So long as you're not using `setPreferredSize` on the container, yes, they should – MadProgrammer May 21 '16 at 23:12
  • I will try using that then. Thank you! – D.Maximov May 21 '16 at 23:13

0 Answers0