Home > Net >  SWING : calling a method with the setBackground Container causes StackOverFlow error due to recursiv
SWING : calling a method with the setBackground Container causes StackOverFlow error due to recursiv

Time:11-15

when i call a method that creates a Jpanel object and a GridBagLayout object, and sets the background of my JFrame to cyan. i get a stackOverFlow error, i believe this is due to the setBackground function attempting to set the background of containers other than the panel background, such as my buttons, and this is what has caused the error, i have attempted to put the function in another JPanel but to no avial. my question is , what can i do in my code to fix StackOverFlow error from happening, and will i need to create a new panel for the background.

code :

Swing Methods:

public JFrame frame(){
        JFrame frame = new JFrame();
        frame.setTitle("a new "   MethodHandles.lookup().lookupClass().getName()   " Appears! ");
        return frame;
    }
    public JPanel mainPanel(){
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        //mainPanel().setBackground(Color.cyan);
        return mainPanel;
    }
    public JPanel panel(){
        JPanel panel = new JPanel(new GridBagLayout());
        panel().setBackground(Color.cyan);
        return panel;
    }
    public GridBagConstraints constr(){
        GridBagConstraints constr = new GridBagConstraints();
        return constr;
    }

class the methods are called to :

static void AddWindow(){
       Options OPT = new Options();
       Tutors TRS = new Tutors();


       /*JFrame frame = new JFrame();
       frame.setTitle("a new "   MethodHandles.lookup().lookupClass().getName()   " Appears! ");
       JPanel mainPanel = new JPanel();
       mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));*/

      // JPanel panel = new JPanel(new GridBagLayout());
       //panel.setBackground(Color.cyan);

       OPT.frame();
       OPT.mainPanel();
       OPT.panel();
       OPT.constr();

// Constraints for the layout
       //GridBagConstraints constr = new GridBagConstraints();
// Setting initial grid values to 0,0

/////////// first columns ///////////////////////
       OPT.constr().weightx = 0.5;
       OPT.constr().weighty = 0.5;

       OPT.constr().gridx = 0;
       OPT.constr().gridy = 0;
       OPT.panel().add(OPT.NamesText(), OPT.constr());

       OPT.constr().gridx = 0;
       OPT.constr().gridy = 1;
       OPT.panel().add(OPT.ModuleNameText(), OPT.constr());

       OPT.constr().gridx = 0;
       OPT.constr().gridy = 2;
       OPT.panel().add(OPT.PostitionText(), OPT.constr());

       OPT.constr().gridx = 0;
       OPT.constr().gridy = 3;
       OPT.panel().add(OPT.AreaOfExpertiseText(), OPT.constr());

       OPT.constr().gridx = 0;
       OPT.constr().gridy = 4;
       OPT.panel().add(OPT.StudentsSupervisedText(), OPT.constr());


/////////// second column ////////////////////
       OPT.constr().gridx = 1;
       OPT.constr().gridy = 0;
       OPT.panel().add(OPT.Names(), OPT.constr());

       OPT.constr().gridx = 1;
       OPT.constr().gridy = 1;
       OPT.panel().add(OPT.ModuleName(), OPT.constr());

       OPT.constr().gridx = 1;
       OPT.constr().gridy = 2;
       OPT.panel().add(OPT.Position(), OPT.constr());

       OPT.constr().gridx = 1;
       OPT.constr().gridy = 3;
       OPT.panel().add(OPT.AreaOfExperise(), OPT.constr());

       OPT.constr().gridx = 1;
       OPT.constr().gridy = 4;
       OPT.panel().add(OPT.StudentsSupervised(), OPT.constr());

       OPT.constr().gridx = 1;
       OPT.constr().gridy = 5;
       OPT.panel().add(OPT.SubmitButton(), OPT.constr());



       OPT.mainPanel().add(OPT.panel());
       OPT.frame().add(OPT.mainPanel());

       OPT.frame().pack();
       OPT.frame().setSize(800,800);
       OPT.frame().setLocationRelativeTo(null);
       OPT.frame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       OPT.frame().setVisible(true);

   }

CodePudding user response:

Every time you call this method, it's going to be calling itself until it crashes...

public JPanel panel(){
    JPanel panel = new JPanel(new GridBagLayout());
    panel().setBackground(Color.cyan);
    return panel;
}

This is probably a good time to consider renaming these methods to something like getXxx

Thanks , thats worked :) , now that the frame runs it is completely empty, however when i created all the objects inside the class it worked fine, is there a chance that calling the methods from outside the class is breaking the code due to the objects not being created in the same class ?

Every time you call one of the methods, it's creating a new instance of the object.

This...

OPT.getFrame().pack();
OPT.getFrame().setSize(800, 800);
OPT.getFrame().setLocationRelativeTo(null);
OPT.getFrame().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
OPT.getFrame().setVisible(true);

alone creates 5 seperate instance of a JFrame, non of which have any relationship to the previous

Instead, you should either consider redesigning the solution to isolate the functionality or use a "lazy" property, for example...

public class Options {

    private JFrame frame;
    private JPanel mainPanel;
    private JPanel panel;
    private GridBagConstraints gbc;

    public JFrame getFrame() {
        if (frame == null) {
            frame = new JFrame();
            frame.setTitle("a new "   MethodHandles.lookup().lookupClass().getName()   " Appears! ");
        }
        return frame;
    }

    public JPanel getMainPanel() {
        if (mainPanel == null) {
            mainPanel = new JPanel();
            mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
            //mainPanel().setBackground(Color.cyan);
        }
        return mainPanel;
    }

    public JPanel getPanel() {
        if (panel == null) {
            panel = new JPanel(new GridBagLayout());
            panel.setBackground(Color.cyan);
        }
        return panel;
    }

    public GridBagConstraints getGridBagConstraints() {
        if (gbc == null) {
            gbc = new GridBagConstraints();
        }            
        return gbc;
    }
}
  • Related