0

I am trying to make a boxlayout filled with a Jlabel and 3 radio buttons in descending order. The program compiles fine but then errors out with the error BoxLayout cant be shared. I have seen people saying this error is because they are trying to attach it to a jframe, but in this case it is the jpanel being given the layout not a frame. This is the segment of code that compiles the window.

JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BorderLayout());
    JPanel effortButtons = new JPanel();
    JPanel skillButtons = new JPanel();
    effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    skillButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    effortButtons.add(effortHeader);//this is what gives the error
    effortButtons.add(oneEffort);
    effortButtons.add(twoEffort);
    effortButtons.add(threeEffort);
    skillButtons.add(skillHeader);
    skillButtons.add(oneSkill);
    skillButtons.add(twoSkill);
    skillButtons.add(threeSkill);
    mainPanel.add(effortButtons, BorderLayout.WEST);
    mainPanel.add(skillButtons, BorderLayout.EAST);
    mainPanel.add(studentName, BorderLayout.NORTH);
    mainPanel.add(next, BorderLayout.SOUTH);
    add(mainPanel);
    pack();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Possible duplicate of [BoxLayout can't be shared error](http://stackoverflow.com/questions/761341/boxlayout-cant-be-shared-error) – TheMirrox Mar 25 '16 at 02:20

1 Answers1

3
// xxxxxxxxxxxxx                         xxxxxxxxx
   effortButtons.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

You have to pass into the BoxLayout constructor the component that is getting the layout. So this should be:

effortButtons.setLayout(new BoxLayout(efforButtons, BoxLayout.Y_AXIS));

Likewise for our other JPanel -- change it to:

skillButtons.setLayout(new BoxLayout(skillButtons, BoxLayout.Y_AXIS));

Per the BoxLayout API:

public BoxLayout(Container target, int axis)
target - the container that needs to be laid out

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Oh really, I was under the impression that the first option was the panel that I wanted to link it to. That boxlayout needs to be able to be placed on the mainpanel that is using the borderlayout. Would that still stand? – TheDoctorHax Mar 25 '16 at 02:20
  • @TheDoctorHax: define "that I wanted to link to" as it has no meaning. The BoxLayout needs a reference to the component that is receiving the layout. Try it out. – Hovercraft Full Of Eels Mar 25 '16 at 02:21
  • @TheDoctorHax: see edit to the answer. The API specifically tells you that the parameter should be the container that needs to be laid out. – Hovercraft Full Of Eels Mar 25 '16 at 02:23
  • Thank you, that worked, but now there is nothing displaying in the window. I made sure to do add(mainPanel) and still nothing. – TheDoctorHax Mar 25 '16 at 02:24
  • @TheDoctorHax: no idea given the current code posted. Create and post your [mcve] and let's see where your problem is. – Hovercraft Full Of Eels Mar 25 '16 at 02:25
  • @TheDoctorHax: this means that you've got another problem unrelated to your main problem that generated this question. – Hovercraft Full Of Eels Mar 25 '16 at 02:25
  • Edited the code segments to show the other areas of code pertaining to window creation. – TheDoctorHax Mar 25 '16 at 02:29
  • @TheDoctorHax: please read or re-read the [mcve] link that I've provided since your posted code still doesn't help us at all. We don't want to see your whole program, and please don't post code in a link, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem. You might very well solve the problem yourself by simply trying to isolate and expose the bug. – Hovercraft Full Of Eels Mar 25 '16 at 02:31
  • The problem is that there is nothing showing up in the window when I run the program. I will remove that second area of code as that only creates the buttons. The layout of the window is handled in the remaining bit of code, so that is where the problem would be. – TheDoctorHax Mar 25 '16 at 02:36
  • @TheDoctorHax: how can I state this so you will believe me? I will try again, **the current bug is not found in the code you've posted**. Period. Full stop. End of story. Sorry if I am sounding a bit frustrated, but please, please, please believe me. If you still need a solution to this new problem, ask a new question on this site, **with your runnable [mcve]**, and you'll get a decent answer. – Hovercraft Full Of Eels Mar 25 '16 at 02:40
  • @TheDoctorHax: Question: are you adding your JButton variables to the JPanels **before** assigning viable JButton objects to these variables? In other words, you should be creating your JButton objects **before** adding the variables to the JPanel -- are you doing this? If not, you're adding `null` references to the JPanels not JButtons. Again, a valid [mcve] would answer this straight away. – Hovercraft Full Of Eels Mar 25 '16 at 02:48
  • ... and please address this last question of mine as well. – Hovercraft Full Of Eels Mar 25 '16 at 04:33