-2

the problem is i have multiple number of panels in that mainpane stays in the centre.using a for loop i added 1000 buttons to the main panel.i added a scroll pane for the mainpane.but i'm unable to scroll.i tried solutions from lot of posts from stackoverflow but i couldn't resolve please help me

  public class Products extends Applet {

private static final long serialVersionUID = -5897268039244126279L;
JPanel mainpane=new JPanel();
JPanel toppane=new JPanel();
JPanel leftpane=new JPanel()
{
    public void paintComponent(Graphics g)
    {
        g.drawImage(img.getI![enter image description here][1]mage(),0,0,200,500,null);
    }
}
;
JPanel total=new JPanel();
JPanel rightpane=new JPanel();
ImageIcon img=new ImageIcon(getClass().getResource("biskate.jpg"));
private void initialise() {

    total.setPreferredSize(new Dimension(1366,650));
         leftpane.setPreferredSize(new Dimension(200,500));
         rightpane.setPreferredSize(new Dimension(266,500));
         mainpane.setPreferredSize(new Dimension(900,500));
         mainpane.setLayout(new FlowLayout());
         toppane.setPreferredSize(new Dimension(1366,150));
         total.setLayout(new BorderLayout());
         leftpane.setBackground(Color.WHITE);   
         mainpane.setBackground(Color.BLUE);

         toppane.setBackground(Color.WHITE);
         rightpane.setBackground(Color.magenta);

         total.add(leftpane,BorderLayout.WEST);
            total.add(rightpane,BorderLayout.EAST);

JScrollPane scroll=new JScrollPane(mainpane,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        //  scroll.setPreferredSize(new Dimension(600,600));
            //scroll.getViewport().add(mainpane);
            total.add(scroll,BorderLayout.CENTER);

            total.add(toppane,BorderLayout.NORTH);
            add(total);

}


             JCheckBox arr[];
            BufferedImage image;
            JLabel look=new JLabel(img);
             public void init()
             {
               this.setSize(1366,650);

        initialise();
                arr=new JCheckBox[6];

    String []s={"Rs. 2000 and Below","Rs. 2001 - Rs. 5000","Rs. 5001 - Rs. 10000","Rs. 10001 - Rs. 18000",
            "Rs. 18001 - Rs. 25000","Rs. 25001 - Rs. 35000"};
    leftpane.add(new JLabel("Choose price range"));

    for(int i=0;i<6;i++)
    {
     arr[i]=new JCheckBox(s[i]);
     arr[i].setBackground(Color.YELLOW);
     leftpane.add(arr[i]);
    }
    addgrid();
        }
        private void addgrid()
        {
          for(int i=0;i<1000;i++)
            {
           mainpane.add(new JButton("holy"+" "+i));
            } 

         }
user22002
  • 29
  • 5
  • 1
    Don't mix Swing & AWT. Use a Swing based `JApplet`. More generally: 1) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 2) Why code an applet? If it is due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Sep 22 '13 at 18:09
  • can you help me with working of code i changed to JApplet but no use – user22002 Sep 23 '13 at 14:35
  • what is SSCCE ? i'm just a beginner .can you please be a bit clear. – user22002 Sep 23 '13 at 15:10
  • My apologies, I'm just a dope. I thought I had mentioned earlier: For better help sooner, post an [SSCCE](http://sscce.org/). :P – Andrew Thompson Sep 23 '13 at 16:46

1 Answers1

2
//leftpane.setPreferredSize(new Dimension(200,500));
//rightpane.setPreferredSize(new Dimension(266,500));
//mainpane.setPreferredSize(new Dimension(900,500));
//mainpane.setLayout(new FlowLayout());
//toppane.setPreferredSize(new Dimension(1366,150));

Don't use setPreferredSize() on any component. The layout manager of the panel with determine the preferred size for the panel. Then the scrollbars will appear automatically when required.

Also, FlowLayout is the default layout for a panel so you don't need to manually set the layout again.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • *"Don't use setPreferredSize() on any component."* There is a custom panel (which does suggest 'set a preferred size') used to draw an image, but it should really be a `JLabel` with a border. – Andrew Thompson Sep 22 '13 at 18:22
  • 1
    Agree, the `leftPane`, which paints an Image should really be a JLabel with an Icon. If custom painting is used for some reason then the panel should override the getPreferredSize() method to return the appropriate size. – camickr Sep 22 '13 at 19:05
  • can anyone help me i need to use setpreferredsize() as the panels size is fixed using this method.can anyone please post the working version of the code. – user22002 Sep 23 '13 at 14:27
  • @user22002, I already gave you the proper solution. No you don't need to use the setPreferredSize() method!!! That is NOT the way Swing was designed to be used. – camickr Sep 23 '13 at 15:09
  • so if i want fixed size of panel what am i supposed to do – user22002 Sep 23 '13 at 15:21
  • 1
    @user22002, I told you in my comment that you need to override the getPreferredSize() method if you are doing custom painting. But this is only if you do custom painting. If you add components to a panel you let the layout manager determine the size. – camickr Sep 23 '13 at 15:59
  • i'm just a beginner can you send me some links where i can learn – user22002 Sep 23 '13 at 16:20
  • 1
    @user22002, This section from the Swing tutorial on [Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html) has a simple example. I would suggest you browse the whole tutorial for Swing basics. – camickr Sep 23 '13 at 16:28