Search code examples
javaswinglayoutjscrollpane

JScrollPane doesn't work.. Why?


I've a Frame which has two internal frames. I create a 'Board' object which is an instance of Board class. Board class extends JPanel.

class Layout extends JFrame{
   Dimension dimen=Toolkit.getDefaultToolkit().getScreenSize();
   public initializeWindows(){
       JInternalFrame dev=new JInternalFrame("Devices",true,true,false,false);
       JInternalFrame cir=new JInternalFrame("Circuit",true,true,false,false);
       Board b=new Board();
       cir.add(b);
       JScrollPane scroll=new JScrollPane(b);
       this.add(dev);
       this.add(cir);

       dev.setVisible(true);
       dev.setSize(150,650);
       dev.setLocation(0,100);
       dev.pack();

       inf.setVisible(true);
       inf.setPreferredSize(new Dimension((int)(dimen.width*0.88),(int)(dimen.height*0.75)));
       inf.setLocation(150,100);
       inf.setBackground(Color.WHITE);
       inf.pack();

   }

But the scrollpane does not appear. Why is tat??


Solution

  • Because you are not adding the JScrollPane to the internal frame.

    You are actually adding the Board to the JInternalFrame cir and to the JScrollPane while you should do something like

    JInternalFrame cir=new JInternalFrame("Circuit",true,true,false,false);
    Board b=new Board();
    JScrollPane scroll=new JScrollPane(b);
    cir.add(scroll)
    this.add(cir);