I cannot get the JFrame to display my JPanel. Either that, or my JPanel isn't displaying my JScrollPane.

Here's my code:

public class PokeBase extends JFrame
{
    private static final long   serialVersionUID    = 1L;

    private static final int WIDTH = 1280;
    private static final int HEIGHT = 720;

    public static void main(String[] args)
    {
        new PokeBase().setVisible(true);
    }

    public PokeBase()
    {
        super("PokeBase - Complete DataBase of Pokemon");
        Pokemon.init();
        initGUI();
    }

    private void initGUI()
    {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setLocation(getX() - WIDTH / 2, getY() - HEIGHT / 2);
        this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        this.setResizable(false);

        addGUIComponents();

        this.pack();
    }

    private void addGUIComponents()
    {
        JPanel menuPanel = new JPanel();
        menuPanel.setLocation(0, 0);
        menuPanel.setPreferredSize(new Dimension(200, HEIGHT));
        menuPanel.setVisible(true);

        JScrollPane treeView = new JScrollPane();

        DefaultMutableTreeNode pokemonList = new DefaultMutableTreeNode("Pokemon");
        DefaultMutableTreeNode bulb = new DefaultMutableTreeNode(Pokemon.bulbasaur.getName());

        pokemonList.add(bulb);

        JTree tree = new JTree(pokemonList);

        menuPanel.add(treeView);

        this.add(menuPanel);
    }

}

I haven't worked with JScrollPane's before, or JTree's either.

Also, I had to do Some trickery because when I centered the form using setLocationRelativeTo(null);, it centered using the top left corner. I don't remember this being a problem...

Recommended Answers

All 3 Replies

  1. read Initial Thread

  2. create JFrame as local variable

  3. where going code line menuPanel.setLocation(0, 0);, because JFrame has BorderLayout in API

  4. mode code lines from private void initGUI() and private void addGUIComponents() to constructor public PokeBase()

  5. put LineBorder to JPanel for testing purposes

  6. JTree should be inside JScrollPane

  7. nobody knows (reason for debugging) what is/are wrong in Pokemon class

You don't seem to put anything in your scroll pane?

setLocationRelativeTo(null); works OK. If it's centering the TL corner then it sounds like swing thinks your JFrame is empty when you execute that.

Why do you want to set sizes explicitly in pixels? YOur app is going to look pretty silly when someoine runs it on a mchine with a >200 dpi display!

Solving this by Switching to JavaFX... I think that's what I'm gunna use for GUI's from now on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.