Search code examples
javaswingviewsplitpane

Java Swing Split pane view layout


Hello im implementing my firs split pane view and it doesn't seem to be working for me and i get the following output...

enter image description here

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);

Solution

  • 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.