Search code examples
javastack-overflow

StackOverflowError when running a JFrame in java


I coded a JFrame (framePanel) in java but I'm getting a StackOverflowError when running it or running it from another class by instantiating an object of this class and calling the method 'create_ShowGUI()'.

Basically I want that when main() method is called, initComponents() method is called to initialize my components (sets radio buttons, groups them,adds Actionlistener to all radio buttons, creates a JPanel (menuFrame) & adds the buttons to it). After initComponents() finishes its job a JFrame is initialized, the JPanel is added to it & finally the frame is displayed.

Can you help me solve the error ?

EDIT: I created various apps but with NetBeans GUI Builder Tool. This is one of my first times that I'm trying to do it from scratch. Therefore excuse me if something isn't clear.

EDIT: Code updated with comments

EDIT: btw I coded with netbeans

Code starts here:

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 *
 * @author jtech
 * @version 1.0.0
 * 
 * 'c' loop variable is initialized to 1
 * 'c1' loop variable is initialized to 0
 */
public class BankMainMenuFrame 
{      

//    public void BankMainMenuFrame()
//    {                
//        initComponents(); 
//        create_ShowGUI();
//    }

    private void initComponents()
    {
//        JRadioButton defaultOpt = new JRadioButton();
//        defaultOpt.setText(bankMainMenuOpts[0]);
//        defaultOpt.setMnemonic(0x30);
//        defaultOpt.setActionCommand("Default");
//        defaultOpt.setSelected(true);

        //initializes the first button aside the loop that initializes others (default)
        aJB[0] = new JRadioButton();
        aJB[0].setText(bankMainMenuOpts[0]);
        aJB[0].setMnemonic(KeyEvent.VK_0);
        aJB[0].setSelected(true);
        aJB[0].setActionCommand("Default");
        aJB[0].addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent evt)
            {
                aJBActionPerformed(evt, c);
            }
        });

        //the other buttons are initialized here
        while (c < bankMainMenuOpts.length)
        {
            aJB[c] = new JRadioButton();
            aJB[c].setText(bankMainMenuOpts[c]);
            aJB[c].setMnemonic((vK_nums + c));
            aJB[c].setActionCommand(aJB[c].getText());   
            groupRadioButtons.add(aJB[c]);

            aJB[c].addActionListener(new ActionListener() 
            {
                @Override
                public void actionPerformed(ActionEvent evt)
                {
                    aJBActionPerformed(evt, c);
                }
            });

            c++;
        }

        framePanel = new JPanel(new GridLayout(0, 1));

        //buttons will be added to the panel one by one
        while (c1 < bankMainMenuOpts.length)
        {
            framePanel.add(aJB[c1]);

            c1++;
        }

    }

//other methods go here; like 'private void defaultOptActionPerformed(java.awt.event.ActionEvent evt) {}

    public void create_ShowGUI()
    {        
        initComponents();

        menuFrame = new JFrame("Bank Main Manu Program");
        menuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        menuFrame.add(framePanel); //framePanel is added to menuFrame
        menuFrame.setContentPane(new Container());
        menuFrame.pack();
        menuFrame.setVisible(true);
    }

    public void aJBActionPerformed(ActionEvent e, int optionNum)
    {
      //  aCA_MainMenuFrame.executeSelectedMenuOption(); 
        //calls a method which is in another class & that class isn't posted here due to its large 
       //amount of code         
    }

    public static void main(String[] args)
    {

                //instance of this class is created to call create_ShowGUI() method
                new BankMainMenuFrame().create_ShowGUI();

    }

    //<editor-fold defaultstate="collapsed" desc="variables decrlarations">
    private static char vK_nums = 0x30;
    private static int c = 1, c1 = 0;
    private static String[] bankMainMenuOpts = {"Select an option..",     //[0] (pre-selected)
        "Deposit Money",                                                  //[1]
        "Withdraw Money",                                                 //[2]
        "Display Balance",                                                //[3]
        "Exit Bank Account Program"};                                     //[4]

    private static JFrame menuFrame = null;
    private static JPanel framePanel = null;
    private static JRadioButton[] aJB = new JRadioButton[5];
    private static ButtonGroup groupRadioButtons = new ButtonGroup();

    //</editor-fold>   
}

Solution

  • The stack overflow seems to be corrected. Remain two errors;

        while (c < bankMainMenuOpts.length) { //ERROR was <=
    
       //ERROR menuFrame.setContentPane(new Container());