Java Swing Split pane view layout
Frage
Hello im implementing my firs split pane view and it doesn't seem to be working for me and i get the following output...
Here is the code.
//Create Album Panel
albumPanel.setLayout(new FlowLayout());
//Add List view
albumList.setMinimumSize (new Dimension(150,150));
albumPanel.add(new JScrollPane(albumList));
//Add Text Area
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setMinimumSize (new Dimension(150,150));
albumPanel.add(textArea);
//Split Pane
JSplitPane splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, albumList, textArea);
albumPanel.add(splitpane, BorderLayout.CENTER);
Lösung
You have set your albumPanel
layout to FlowLayout
but you try and use a BorderLayout
constants when adding to the JSplitPane
:
albumPanel.add(splitpane, BorderLayout.CENTER);
you should rather set the albumPanel
layout to BorderLayout
via new BorderLayout()
Also it is not a good idea for you to set the size of your components let the LayoutManager
's do it for you.
Andere Tipps
You need to add components, the JScrollPanes that hold your list and your text area, to your JSplitPane for it to show them. And yeah as David states (1+ to him), your container that holds the JSplitPane needs to be able to let it expand, and BorderLayout would work well for this.
Also, don't add components to a container more than once. Add your components to the JScrollPanes, then add the JScrollPanes to the JSplitPane. Don't also add the components to the albumPanel container. Your code is a bit schizophrenic in this regard.