Search code examples
javajscrollpaneborder-layout

Java: How to make a JScrollPane around a BorderLayout


I am trying to make my first GUI-program and sofar everything went well, but now I have a problem:

First my Window had a minimum size and everything worek well, but now I dont want a minimum size, therefore I want a ScrollBar (vertical and horizonal) to be able to to see everything. I am trying to make this with a JScrollPane. The Problem is, that I have my program structured with a BorderLayout and I am not able to connect my JScrollPane with my BorderLayout. "JScrollPane Constructor is undefinied for BorderLayout". So I initialized a JPanel and set the layout to my BorderLayout.

windowContainer = new JPanel(new BorderLayout(10, 10));

Then I can connect the "windowContainer"(JPanel) with my JscrollPane

windowScrollPane = new JScrollPane(windowContainer);

After changing the rest of the code (changed "getContentPane.add..." to "windowContainer.add...") I didnt become errors but the JScrollPane didnt work. In my BorderLayout (LINE_START) is a JPanel with a minimim width of "300", so at least if the window is thinner than 300px the ScrollBar should appaer. I spend a lot of research on the iinternet but everything what i found was "How to create a JScrollPane in a BorderLayout" and not "How to create a JScrollPane around the BorderLayout".

To clarify it i will uploat a picture (the red things are the JScrollBars). Sorry I wasnt allowed to upload pictures, so please look here: http://www.fotos-hochladen.net/view/jscrollpanepu20315v9x.png

And i dont know how much code i have to give you, because everything would be to much, so just say something if you need more.

Here is again the important code about it:

... 
windowContainer = new JPanel(new BorderLayout(10, 10));
windowScrollPane = new JScrollPane(windowContainer);
frame.add(windowContainer);
...

PS: This is my first post, so please correct me if I did something wrong (about the post). And sorry for my english.


Solution

  • Try this code. Method initComponent() use in contructor, or in place where you build view. Below I put example of JFrame with BorderLaylout as you want:

    public class TestWindow extends JFrame{
    
    int containerHeigh=300;
    int containerWitdh=400;
    private JPanel container;
    private JPanel westPanel;
    private JPanel eastPanel;
    private JPanel northPanel;
    private JPanel southPanel;
    private JPanel centerPanel;
    private JScrollPane scroll;
    
    public TestWindow(){
        super("test");
        initComponents();
    }
    
    private void initComponents(){
        container=new JPanel();
        westPanel=new JPanel();
        eastPanel=new JPanel();
        northPanel=new JPanel();
        southPanel=new JPanel();
        centerPanel=new JPanel();
    
        //...fill panels of container
        westPanel.setBackground(new Color(95,183,70));
        eastPanel.setBackground(new Color(0,164,232));
        northPanel.setBackground(new Color(255,255,185));
        southPanel.setBackground(new Color(34,177,76));
        centerPanel.setBackground(new Color(152,114,203));
    
        scroll=new JScrollPane();
        scroll.setViewportView(container);
    
        BorderLayout containerLayout=new BorderLayout(containerHeigh, containerWitdh);
        container.setLayout(containerLayout);
        container.add(westPanel, BorderLayout.WEST);
        container.add(eastPanel, BorderLayout.EAST);
        container.add(northPanel, BorderLayout.NORTH);
        container.add(southPanel, BorderLayout.SOUTH);
        container.add(centerPanel, BorderLayout.CENTER);
    
        add(scroll);
        setVisible(true);
    }
    
    public static void main(String...args){
        new TestWindow();
    }
    

    }

    If you want you can use NetBeans for create Panels and other element of desctop applications. I generally build in NetBeans simple Panels, Dialog and then combine together dynamically in application. This provide me get user interface exactly what I want and prepare really fast.