Pergunta

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));
            } 

         }
Foi útil?

Solução

//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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top