Home > Software engineering >  How to fix the problem with showing JPanel element in Java Swing?
How to fix the problem with showing JPanel element in Java Swing?

Time:10-11

I have problem with showing JPanel (Page) element that located in another JPanel element in JFrame.

enter image description here

Abstract Class Page is extending JPanel and giving it some new parameters.

public abstract class Page extends JPanel implements KeyListener {
    protected String pageName;
    protected Color backgroundColor;

    public Page(String name, Color backgroundColor) {
        this.pageName = name;
        this.backgroundColor = backgroundColor;

        addKeyListener(this);
        this.setFocusable(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setSize((int) PAGE_WIDTH, (int) PAGE_HEIGHT);
        this.setBackgroundColor(backgroundColor);
    }

    /* keyTyped method is not needed in usage, so it is empty */
    @Override
    public void keyTyped(KeyEvent e) {}

    /* keyTyped method is not needed in usage, so it is empty */
    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public abstract void keyPressed(KeyEvent e);

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String name) {
        this.pageName = name;
    }

    public Color getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor) {
        this.backgroundColor = backgroundColor;
    }
}

I initial Page object in Game Board constructor and then try to use it in printComponent() method of GameBoard class.

public class GameBoard extends JPanel {
    private static GameBoard instance = null;
    private Page currentPage = new MainMenuPage();
    private JPanel dialog = Dialog.getInstance();

    private GameBoard() {}

    public static GameBoard getInstance() {
        if (instance == null) {
            instance = new GameBoard();
        }
        return instance;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setBackground(Color.WHITE);

        JPanel container = new JPanel();
        container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
        container.setSize(WINDOW_WIDTH, (int) PAGE_HEIGHT);
        container.setBackground(Color.decode("#333333"));

        container.add(currentPage);
        container.add(dialog);
        this.add(container);
    }

    public Page getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Page currentPage) {
        this.currentPage = currentPage;
    }

    public JPanel getDialog() {
        return dialog;
    }

    public void setDialog(JPanel dialog) {
        this.dialog = dialog;
    }
}

When window appears, Page element is not showing, like in screenshot below. Main class code:

public class GameApplication {
    private static JFrame window;
    private static GameBoard gameBoard;

    public static void main( String[] args ) {
        setupWindow();
        createGameBoard();
    }

    private static void setupWindow() {
        System.out.println("[GameApplication]: Creating window");
        window = new JFrame("Dungeon Master");
        window.setVisible(true);
        window.setResizable(false);
        window.setBounds(200, 80, WINDOW_WIDTH, WINDOW_HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private static void createGameBoard() {
        System.out.println("[GameApplication]: Creating GameBoard");
        gameBoard = GameBoard.getInstance();
        window.add(gameBoard);
        gameBoard.requestFocusInWindow();
    }

    /* Game Init Method? */
}

enter image description here

CodePudding user response:

So I solved this problem, by deleting containers:

public class GameBoard extends JPanel {
    private static GameBoard instance = null;
    private Page currentPage = new MainMenuPage();
    private final JPanel dialog = Dialog.getInstance();

    private final JPanel keyInfo = new JPanel();
    private final JLabel keyInfoLabel = new JLabel("Q - quit the game | S - go to start menu | C - clear dialog");

    private GameBoard() {}

    public static GameBoard getInstance() {
        if (instance == null) {
            instance = new GameBoard();
        }
        return instance;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        this.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);

        currentPage.setBounds(0, 0, PAGE_WIDTH, PAGE_HEIGHT);
        currentPage.setBackground(Color.decode("#121212"));

        dialog.setBounds(PAGE_WIDTH, 0, DIALOG_WIDTH, PAGE_HEIGHT);
        dialog.setBackground(Color.decode("#333333"));

        keyInfo.setBounds(0, PAGE_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT);
        keyInfo.setBackground(Color.decode("#f6f9f9"));
        keyInfo.setLayout(new BoxLayout(keyInfo, BoxLayout.X_AXIS));
        
        keyInfoLabel.setForeground(Color.decode("#121212"));
        keyInfo.add(keyInfoLabel);

        this.add(currentPage);
        this.add(dialog);
        this.add(keyInfo);
    }

    public Page getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Page currentPage) {
        this.currentPage = currentPage;
    }

    public JPanel getDialog() {
        return dialog;
    }
}

It looks like this right now: enter image description here

  • Related