-2

When I tried to run my program, it is always an error. The 5 JButtons are not appearing at the west (left) of the MainFrame. I used the BoxLayout for the JButtons so that it can be displayed from top to bottom and called the FirstPanel class to the MainFrame so that I can position it on the west side. It is supposed to be like this, but my application is not running. Please help me with how can I achieve this enter image description here

MainFrame.java

public class MainFrame extends JFrame {
    TitlePanel title;
    FirstPanel first;
    
 
    
    public MainFrame() {
        
        title = new TitlePanel();
        add(title, BorderLayout.NORTH);
        
        first = new FirstPanel();
        add(first, BorderLayout.WEST);
 
        
    
        setSize(5000,5000);
    setVisible(true);
    this.pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    
    
    public static void main(String[] args) {
        new MainFrame();
    }
}

FirstPanel.java

   public class FirstPanel extends JPanel {
   JButton b1;
   JButton b2;
   JButton b3;
   JButton b4;
   JButton b5;
   FirstPanel fp;
    
    public FirstPanel() {

       fp = new FirstPanel();
       BoxLayout layout = new BoxLayout(fp, BoxLayout.Y_AXIS);
       fp.setLayout(layout);
       
       b1 = new JButton();
       b2 = new JButton();
       b3 = new JButton();
       b4 = new JButton();
       b5 = new JButton();
       
       fp.add(b1);
       fp.add(b2);
       fp.add(b3);
       fp.add(b4);
       fp.add(b5);
    }
    
    
}

This is the ERROR that I got

Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Component.setFont(Component.java:1907)
    at java.awt.Container.setFont(Container.java:1753)
    at javax.swing.JComponent.setFont(JComponent.java:2748)
    at javax.swing.LookAndFeel.installColorsAndFont(LookAndFeel.java:208)
    at javax.swing.plaf.basic.BasicPanelUI.installDefaults(BasicPanelUI.java:66)
    at javax.swing.plaf.basic.BasicPanelUI.installUI(BasicPanelUI.java:56)
    at javax.swing.JComponent.setUI(JComponent.java:660)
    at javax.swing.JPanel.setUI(JPanel.java:153)
    at javax.swing.JPanel.updateUI(JPanel.java:126)
    at javax.swing.JPanel.<init>(JPanel.java:86)
    at javax.swing.JPanel.<init>(JPanel.java:109)
    at javax.swing.JPanel.<init>(JPanel.java:117)
    at FirstPanel.<init>(FirstPanel.java:26)
    at FirstPanel.<init>(FirstPanel.java:28)
  • 1
    have you read the official swing tutorial about layout ? https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html – jhamon Jul 27 '22 at 12:00
  • 1
    *but my application is not running* means? You get an error or what? – Jens Jul 27 '22 at 12:01
  • [mcve] please .. make sure that the example is minimal, compileable and runnable as-is. The code shown doesn't compile - replacing the unrelated (so correctly left-out!) TitlePane with a plain JLabel. – kleopatra Jul 27 '22 at 12:08
  • @kleopatra yea that's my problem, it does not run since it has error. I already edited my post and included the error that I got – Carrington Jul 27 '22 at 12:11
  • @Jens I already included the error – Carrington Jul 27 '22 at 12:15
  • looks like you might consider taking a step back in your learning plan: calling the constructor (of itself) in the constructor is .. utterly wrong. You want to understand the keywork _this_. Remove it and you should be right for now. – kleopatra Jul 27 '22 at 12:16
  • 1
    You are calling the constructor of `FirstPanel` in the constructor which gives you an endless recursion. `fp = new FirstPanel();` --> `fp = this;` – Jens Jul 27 '22 at 12:17
  • is "this" pertains to the current class I'm working in? – Carrington Jul 27 '22 at 12:21

1 Answers1

0

It would be better to first call pack and then make the JFrame visible.

pack();
setVisible(true);

Also in the main use:

SwingUtilities.invokeLater(() -> new MainFrame());

or what I like:

SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));

This ensures the correct thread.

The error is having a FirstPanel fp inside the FirstPanel. This is an endlessly nested object. Hence never completely created.

   BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
   setLayout(layout);
   ...
   add(b1);

One also often sees making just an anonymous JPanel to a variable, and using that. In the case of a no-name like "FirstPanel" that would make sense. However "ButtonPanel" or such is okay.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138