简体   繁体   中英

Adding a JPanel to JScrollPane within JSplitPane

I'm working on some nesting of GUI objects. I've gotten most of it working so far but the latest part I haven't been able to figure out. It may be a simple problem and I don't think the depth of nesting is causing the issue but I'll list what I have in case it is.

So far I have a JFrame containing a JPanel , which has a JTabbedPane , this JTabbedPane has 2 JPanels in it. I am currently working on one of these JPanels - it contains a JSplitPane containing a JScrollPane and another JSplitPane . The second JSplitPane has two JScrollPane s. One of the JScrollPane s has a JPanel .

This is all working correctly up until the last JPanel , I've added a background just as a means of testing it to see if it's working right. My problem is that the background shows up basically as a border within the JScrollPane , the inside of the pane is gray, like there is no background.

Here is some of my code - I can provide more if anybody thinks it would be useful.

This is the JPanel that is within the JTabbedPane .

public class MapEditor extends JPanel{

    int height, width;
    final int DIVIDER_SIZE = 7;
    final int HORIZONTAL_DIVIDER_PLACE = 300;
    final int VERTICAL_DIVIDER_PLACE = 200;

    //side bar divider separates main page and left (vertical divider)
    //side bar division separates top and bottom of sidebar (horizontal divider)
    JSplitPane sideBarDivider;
    JSplitPane sideBar;

    //scroll pane to hold tiles panel, map panel
    JScrollPane toolContainer;
    JScrollPane tileContainer;
    JScrollPane mapContainer;

    //panels held inside JScrollPanes above
    JPanel toolPanel;
    JPanel tilePanel;
    JPanel mapPanel;

    public MapEditor(int w, int h){
        this.width = w;
        this.height = h;
        setLayout(new BorderLayout());
        //we can just pass the final variables in here because it's in the top left
        toolPanel = new ToolPanel(HORIZONTAL_DIVIDER_PLACE, 
                VERTICAL_DIVIDER_PLACE);
        sideBar = splitScreen();
        add(sideBar);
    }

    //main split between map and tools/tiles
    public JSplitPane splitScreen(){
        mapContainer = new JScrollPane();
        sideBar = buildSideBar();
        //add map container and sidebar
        sideBarDivider = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                sideBar, mapContainer);
        sideBarDivider.setSize(width, height);
        //set divider size and position
        sideBarDivider.setDividerLocation(HORIZONTAL_DIVIDER_PLACE);
        sideBarDivider.setDividerSize(DIVIDER_SIZE);
        return sideBarDivider;
    }

    //small split between tools and tiles
    public JSplitPane buildSideBar(){
        toolContainer = new JScrollPane();
        toolContainer.add(toolPanel);
        tileContainer = new JScrollPane();
        //add tile & tool containers
        sideBar = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                toolContainer, tileContainer);
        //set divider size and position
        sideBar.setDividerSize(DIVIDER_SIZE);
        sideBar.setDividerLocation(VERTICAL_DIVIDER_PLACE);
        return sideBar;
    }   
}

This is the panel that is giving me problems.

public class ToolPanel extends JPanel{

    int width, height;

    public ToolPanel(int w, int h){     
        this.width = w;
        this.height = h;
        setLayout(new BorderLayout());
        setSize(w, h);
        setBackground(Color.BLACK);     
    }
}

Anybody see anything I'm doing wrong? I'm not too experienced with Java GUI components and would appreciate any help anybody has to offer.

    toolContainer = new JScrollPane();
    toolContainer.add(toolPanel);

You don't add components to a JScrollPane. The component is added to the "viewport" of the scroll pane.

You can use:

    toolContainer = new JScrollPane( toolPanel );

or

    toolContainer = new JScrollPane();
    toolContainer.setViewportView(toolPanel);

Also, you should not be using:

setSize(w, h);

The layout manager of the component will determine the preferred size of the component based on the comopnents added to the panel.

If you are doing some kind of custom painting then you should override the getPreferredSize() method of the ToolPanel to return the appropriate size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM