Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I scroll my JFrame using the JScrollbar?

I have a problem.

I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar?

like image 217
Alexey Avatar asked May 08 '11 15:05

Alexey


People also ask

How do I make my JPanel scrollable?

The simplest way is to create your JPanel and specify it when creating the JScrollPane: JPanel myPanel = ...; JScrollPane scroller = new JScrollPane( myPanel ); Then just add the scroller to your GUI (instead of adding myPanel ). Save this answer.

How do you create a scrollable text area in a Java Swing frame?

JFrame frame = new JFrame ("Test"); JTextArea textArea = new JTextArea ("Test"); JScrollPane scrollV = new JScrollPane (textArea); JScrollPane scrollH = new JScrollPane (textArea); scrollV. setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS); scrollH. setHorizontalScrollBarPolicy(JScrollPane.

How do you add a scroll in Java?

Java Prime PackJScrollPane(Component view) − To create a scrollPane on a component. JScrollPane. setVerticalScrollBarPolicy(JScrollPane. VERTICAL_SCROLLBAR_ALWAYS) − To show a vertical bar always.


1 Answers

  1. Put all the components in one panel (instead of in the JFrame)
  2. Add this panel to a JScrollPane
  3. Add the JScrollPane to your frame.

I should be something like:

JPanel container = new JPanel();
container.add(panel1);
container.add(Panel2);
JScrollPane jsp = new JScrollPane(container);
frame.add(jsp);
like image 191
MByD Avatar answered Sep 19 '22 14:09

MByD