0

I am trying to write some code for a java swing program, but my program throws errors. I looked at posts with similar problems but none of them apply in this situation. what am I doing wrong?

I've rearranged my panels into a more logical format but it didn't help.

Here is my code, the problem is pointed out in the comments. each indentation is a another panel level.

BaseballStats()
{
JFrame myFrame = new JFrame();          
FlowLayout myLayout = new FlowLayout(); 
myFrame.setLayout(myLayout);      

    // configure JFrame with basic settings
myFrame.setTitle("JFrame Window");      
myFrame.setSize(400,200);
myFrame.setLocation(200, 300);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel myPanel = (JPanel)myFrame.getContentPane();
    myPanel.setSize(200,200);
    myPanel.setLayout(new BorderLayout());      

        JPanel topmiddlePanel = new JPanel();
        topmiddlePanel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.Y_AXIS));

            JPanel game1panel = new JPanel();
            game1panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
            game1panel.setAlignmentX(Component.TOP_ALIGNMENT);

                JLabel G1Label = new JLabel("Game 1 hits:");
                G1Label.setAlignmentX(Component.LEFT_ALIGNMENT);
     //problem here ---->// game1panel.add(G1Label);
                JSpinner G1Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
                G1Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
                game1panel.add(G1Spinner);
                topmiddlePanel.add(game1panel);

            JPanel game2panel = new JPanel();
            game2panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
            game2panel.setAlignmentX(Component.TOP_ALIGNMENT);

                JLabel G2Label = new JLabel("Game 2 hits:");
                G2Label.setAlignmentX(Component.LEFT_ALIGNMENT);
                game2panel.add(G2Label);
                JSpinner G2Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
                G2Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
                game2panel.add(G2Spinner);
                topmiddlePanel.add(game2panel);

            JPanel game3panel = new JPanel();
            game3panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
            game3panel.setAlignmentX(Component.CENTER_ALIGNMENT);

                JLabel G3Label = new JLabel("Game 3 hits:");
                G3Label.setAlignmentX(Component.LEFT_ALIGNMENT);
                game3panel.add(G3Label);
                JSpinner G3Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
                G3Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
                game3panel.add(G3Spinner);
                topmiddlePanel.add(game3panel);

            JPanel game4panel = new JPanel();
            game4panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
            game4panel.setAlignmentX(Component.BOTTOM_ALIGNMENT);

                JLabel G4Label = new JLabel("Game 4 hits:");
                G4Label.setAlignmentX(Component.LEFT_ALIGNMENT);
                game4panel.add(G4Label);
                JSpinner G4Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
                G4Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
                game4panel.add(G4Spinner);
                topmiddlePanel.add(game4panel);

            JPanel game5panel = new JPanel();
            game5panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));
            game5panel.setAlignmentX(Component.BOTTOM_ALIGNMENT);

                JLabel G5Label = new JLabel("Game 5 hits:");
                G5Label.setAlignmentX(Component.LEFT_ALIGNMENT);
                game5panel.add(G5Label);
                JSpinner G5Spinner = new JSpinner(new SpinnerNumberModel(1,1,5,1));
                G5Spinner.setAlignmentX(Component.RIGHT_ALIGNMENT);
                game3panel.add(G5Spinner);
                topmiddlePanel.add(game5panel);

        myPanel.add(topmiddlePanel, BorderLayout.CENTER);

It's supposed to produce an overall panel, with 3 subsidiary panels, (2 of which are not shown). The panel shown is supposed to have 5 subsidiary panels in a box layout. Each of THOSE panels have a label and a spinner in a flow.

Thanks for your help!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Srlojohn
  • 1
  • 1
  • Welcome to StackOverflow! It would be easier for contributors to help you if you tried to tried to reduce your code as much as possible. For example, it seems to me that everything starting at `game2panel` isn't useful to demonstrate your issue. Also, have you tried to search for similar issues before posting? I think you may find a proper answer on this post: https://stackoverflow.com/questions/761341/boxlayout-cant-be-shared-error?rq=1. If the solutions proposed there doesn't help, try to explain how your issue is different. Good luck. – James May 08 '19 at 13:18
  • It is very similar question i admit, but the other problem stemmed more from the use of `this` in the code. – Srlojohn May 09 '19 at 17:01

1 Answers1

1

BoxLayout is a bit special, in that the layout need to be given the component instance which it is layouting. The 'BoxLayout can't be shared' exception always indicates that a BoxLayout instance has been created with a certain component, but is then being added as a layout to a different component.

In your cases, the pertinent lines are:

    JPanel topmiddlePanel = new JPanel();
    JPanel game1panel = new JPanel();
    game1panel.setLayout(new BoxLayout(topmiddlePanel, BoxLayout.X_AXIS));

See that BoxLayout was instantiated with topmiddlePanel, but is then set as layout in game1panel?

I won't provide corrected code, because I'm uncertain what you want to do exactly in this case, but the proper solution must be of the form:

x.setLayout(new BoxLayout(x, ...))
James
  • 4,211
  • 1
  • 18
  • 34
  • well, that worked, but now my panels are really wonky. my spinners are huge, and some of them are on the same line with others. Is there anyway to use a box layout for more than 3 objects? – Srlojohn May 09 '19 at 17:00
  • Yes, I had some feeling that you would have more problems with that code... First, have you noticed that you are adding spinner 5 on panel 3: `game3panel.add(G5Spinner);`. This is obviously incorrect, and might explain your comment regarding that "some of them are on the same line". Can BoxLayout be used for more than 3 objects? Sure, and there is really nothing special in doing so. – James May 09 '19 at 20:19
  • Regarding the fact that your spinners are bigger than expected, I think that might be a case of setting calling `setPreferredDimensions()` on these objects. I also encourage you to review Java's tutorial on BoxLayout, before anything else: https://docs.oracle.com/javase/tutorial/uiswing/layout/box.html. – James May 09 '19 at 20:31
  • If you still can't resolve your issue, however, then it would be more appropriate (that is, more in line with StackOverflow practices) for you to consider opening a new question. Make sure you first reduce your example as much as possible (read https://stackoverflow.com/help/mcve), and review any other SO questions on this subject before posting. A screenshot of your current result could also help. Good luck. – James May 09 '19 at 20:33
  • One last thing… Once a question has been resolved (that is, the question that you _initially_ asked), please consider closing it. The preferred way to do so is to accept the answer that best helped you progress (see https://stackoverflow.com/help/accepted-answer). If you found the solution by yourself, then you are encouraged to answer your own question, in the interest of other SO users. As a last resort, you may also click "close", right under your question if the question has not been resolved but is no longer pertinent. – James May 09 '19 at 20:48