Reputation:
Yes, i googled for about 30min. Yes there are 2 different Posts about that topic in stackoverflow, but those do not give me any solution to my problem.
I am using quite a few Panels with BoxLayout to position some stuff. When i try to add the final thing to my main Panel i get "BoxLayout can't be shared".
Code:
private void open(int i) {
JLabel titelLabel = new JLabel("Aufgabenblatttitel: ");
JTextField titelTextField = new JTextField();
JLabel dozentLabel = new JLabel("Dozent: ");
JTextField dozentTextField = new JTextField();
JLabel beschreibungLabel = new JLabel("Aufgabenblattbeschreibung: ");
JTextField beschreibungTextField = new JTextField();
JLabel studiengangLabel = new JLabel("Studiengang: ");
JTextField studiengangTextField = new JTextField();
JLabel dateLabel = new JLabel("Erstellt am: ");
for(Aufgabe aufgabe : data.get(i).getAufgaben()) {
JPanel aufgabenPanel = new JPanel();
JLabel aufgabeTitelLabel = new JLabel("Titel: ");
JTextField aufgabeTitelTextField = new JTextField();
aufgabeTitelTextField.setText(aufgabe.getTitel());
JPanel aufgabeTitelPanel = new JPanel();
aufgabeTitelPanel.add(aufgabeTitelLabel);
aufgabeTitelPanel.add(aufgabeTitelTextField);
aufgabeTitelPanel.setLayout(new BoxLayout(aufgabeTitelPanel, BoxLayout.LINE_AXIS));
JLabel aufgabeBeschreibungLabel = new JLabel("Beschreibung: ");
JTextField aufgabeBeschreibungTextField = new JTextField();
aufgabeBeschreibungTextField.setText(aufgabe.getBeschreibung());
JPanel aufgabeBeschreibungPanel = new JPanel();
aufgabeBeschreibungPanel.add(aufgabeBeschreibungLabel);
aufgabeBeschreibungPanel.add(aufgabeBeschreibungTextField);
aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS));
JLabel aufgabeLoesungLabel = new JLabel("Lösung: ");
JTextField aufgabeLoesungTextField = new JTextField();
aufgabeLoesungTextField.setText(aufgabe.getLoesung());
JPanel aufgabeLoesungPanel = new JPanel();
aufgabeLoesungPanel.add(aufgabeLoesungLabel);
aufgabeLoesungPanel.add(aufgabeLoesungTextField);
aufgabeLoesungPanel.setLayout(new BoxLayout(aufgabeLoesungPanel, BoxLayout.LINE_AXIS));
aufgabenPanel.add(aufgabeTitelPanel);
aufgabenPanel.add(aufgabeBeschreibungPanel);
aufgabenPanel.add(aufgabeLoesungPanel);
aufgabenPanel.setLayout(new BoxLayout(aufgabenPanel, BoxLayout.PAGE_AXIS));
this.add(aufgabenPanel);
}
}
Which is Part of the class "AufgabeEditieren", which is defined as:
public class AufgabeEditieren extends JPanel { ... }
So: The AufgabeEditieren constructor calls open() after the class has been initialized. The it tries to create some panels and objects and wants to add them to the class itself via "this.add(aufgabenPanel);". this is refererring to the class AufgabeEditieren (it's object). So why does it not work? Its a panel and should be able to get those items? Thanks...
Upvotes: 0
Views: 417
Reputation: 47607
Ok, it took me a while because I am really not familiar with your mother tongue (it would be much simpler for everyone if you posted your code with english names for variables), but the problem comes from here:
aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungLabel, BoxLayout.LINE_AXIS));
Your setting a BoxLayout on aufgabeBeschreibungPanel
but you provide aufgabeBeschreibungLabel
as a parameter of BoxLayout
. You should instead write:
aufgabeBeschreibungPanel.setLayout(new BoxLayout(aufgabeBeschreibungPanel, BoxLayout.LINE_AXIS));
When seeing this issue, the most common cause of this is that you wrote:
y.setLayout(new BoxLayout(x, BoxLayout.XXX));
where y
and x
are different.
Upvotes: 2