Garmaine Staff asked 1 year ago

My Java JFrame project has been buggy with the components, removing everything JComboBox has used in space

The video example of the bug in my program can be viewed visually here = https://i.imgur.com/0K002PL.gifv

Main Code is as below. You can replicate it by replacing the 3 Panel with random JCombobox(es) and using the main class to call it.

Any help would be appreciated

package com.company;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DisplayPage extends JFrame implements ActionListener {

    EnterDetailsPage newRecord;
    ViewMore viewMoreRecord;
    ViewPage viewRecord;
    JPanel contentPanel = new JPanel(new FlowLayout());

    public DisplayPage(){
        setLayout(new BorderLayout());
        setSize(900, 600);
        setTitle("Add Flight Details");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setPage(1);
        MenuSection menuSection = new MenuSection();

        //Add action listener to buttons
        menuSection.addNewButton.addActionListener(this::addActionPerformed);
        menuSection.updateButton.addActionListener(this::updateActionPerformed);
        menuSection.viewButton.addActionListener(this::viewActionPerformed);

        add("West", menuSection.getMainMenu());
        add("Center", contentPanel);
        setVisible(true);
    }

    public void setPage(int selection){
        contentPanel.removeAll();
        contentPanel.revalidate();
        //contentPanel.repaint();
        switch (selection) {
            case 1:
                newRecord = new EnterDetailsPage();
                contentPanel = newRecord.getTotalPanel();
                break;
            case 2:
                viewMoreRecord = new ViewMore();
                contentPanel = viewMoreRecord.getViewMorePanel();
                break;
            case 3:
                viewRecord = new ViewPage();
                contentPanel = viewRecord.getUpdatePagePanel();
                break;
        }
        add("Center", contentPanel);
    }

    public void actionPerformed(ActionEvent e){

    }

    public void addActionPerformed(ActionEvent e){
        setPage(1);
    }

    public void updateActionPerformed(ActionEvent e){
        setPage(2);
    }

    public void viewActionPerformed(ActionEvent e){
        setPage(3);
    }
}