• Post Reply Bookmark Topic Watch Topic
  • New Topic

JScrollPane doesn't work?  RSS feed

 
Con Lu
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gidday,
I have done the following:-
1) Created a JFrame
2) Created a JScrollPane
3) Added a large panel to the JScrollPane
4) Added the JScrollPane to the JFrame
The scrollbars appear but don't scroll to see the
large panel. The code is below:-
import javax.swing.*;
import java.awt.*;
public class Trial1{
public static void main(String args[]){
JFrame f1 = new JFrame();
f1.setTitle("Trial1");
JScrollPane sP = new JScrollPane(new P1(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sP.setPreferredSize(new Dimension(300,200));
f1.getContentPane().add(sP);
f1.setBounds(25,25,400,300);
f1.setVisible(true);
}
}
class P1 extends JPanel{
P1(){
setSize(600,400);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.fillRect(25,25,500,350);
}
}
Any help would be much appreciated,
Thanks,
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try setPreferredSize( new Dimension (600,400 ) ); in the constructor of the P1 panel and it will work...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!