sher17
sher17

Reputation: 617

JPanel and JScrollPane is not visible in Swing App

I am working on a Swing Application after a long time. I might have forgotten the basics but trying to figure out why the panel and the scroll pane are not visible when I run the app. Help me out to figure out the mistakes I have done.I have a JFrame and a JMenuBar on top and then need to have a panel. Note that the JFrame also needs a JScrollPane below is my code.

public class FrontEndView {
private JFrame mainFrame;
private JMenuBar menubar;
private JMenu menuFile, menuEdit,menuView,menuHelp;
private JMenuItem menuItemA,menuItemB;
private JPanel mainPanel;
private JScrollPane scrollPane;
private JPanel panel1;

public FrontEndView(){
    this.prepareGUI();
}

public void prepareGUI(){
    mainFrame=new JFrame("WALL-E");
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    int ySize = ((int) tk.getScreenSize().getHeight());
    mainFrame.setSize(xSize,ySize);
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainFrame.setResizable(true);
    mainFrame.setLayout(new BorderLayout());

    menubar=new JMenuBar();
    menuFile=new JMenu("File");
    menuEdit=new JMenu("Edit");
    menuView=new JMenu("View");
    menuHelp=new JMenu("Help");
    menuItemA=new JMenuItem("SubMenu-A");
    menuItemB=new JMenuItem("SubMenu-B");
    menuFile.add(menuItemA);
    menuFile.add(menuItemB);

    menubar.add(menuFile);
    menubar.add(menuEdit);
    menubar.add(menuView);
    menubar.add(menuHelp);

    mainFrame.add(menubar,BorderLayout.NORTH);

    mainPanel=new JPanel();
    mainFrame.add(mainPanel, BorderLayout.CENTER);
    mainPanel.setLayout(new GridLayout(1, 2));
    scrollPane=new JScrollPane(mainPanel);


    mainFrame.add(scrollPane);
    mainFrame.setVisible(true);
}

public static void main(String[] args){
    FrontEndView  swingControlDemo = new FrontEndView();
}
}

Upvotes: 1

Views: 232

Answers (2)

STaefi
STaefi

Reputation: 4377

After running your code, noticed that the main reason it does not showing any thing is because you didn't add any JComponent to your mainPanel. So try adding some component will show them:

mainPanel.add(new JLabel("I'm Visible!"));

Some Hints:

You should not add your mainPanel both to your mainFrame and also to your scrollPane. Only add your panel to the ViewPort region (You've already done this by passing your panel to the constructor of JScrollPane). Then add your scrollPane to your the CENTER of mainFrame.

So remove this line and it should work:

 mainFrame.add(mainPanel, BorderLayout.CENTER);

Also not related to the concrete problem, it is not good to add MenuBar to the NORTH of the frame. Use JFrame#setJMenuBar method instead.

Change this:

mainFrame.add(menubar,BorderLayout.NORTH);

To this:

mainFrame.setJMenuBar(menubar);

Good Luck

Upvotes: 2

Manikant Gautam
Manikant Gautam

Reputation: 3591

Scroll Pane is added by this way , with a little modification on your code.Now you can see a label on panel which is again over your mainPanel.

mainFrame.add(menubar,BorderLayout.NORTH);
JLabel l=new JLabel("Label on panel which is again inside scrollPane");
mainPanel=new JPanel();
//  mainFrame.add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new GridLayout(1, 2));
mainPanel.add(l);
scrollPane=new JScrollPane(mainPanel);
//scrollPane.setVisible(true);
//scrollPane.setSize(10, 20);
mainFrame.add(scrollPane);

If you wants scroll do one more thing.

        scrollPane=new JScrollPane(mainPanel,
        ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

Upvotes: 1

Related Questions