Search code examples
javaswingjframejsplitpaneborder-layout

JSplitPane overlaps JButtons in BorderLayout, but does not when layout is null


So I'm having another issue.....

I'm currently using a BorderLayout in my GUI so that when I resize my jframe, all of the internal components resize with it. I couldn't get any other layout to work with how I want the GUI to look while providing this resizing capability. At the top I have a JMenuBar, and below it I have a bunch of buttons. Below that I am supposed to have a JSplitPane, and it is there. However, the buttons seemed to be contained within the JSplitPane, which is not my intention. So when anything happens within the splitpane, the buttons disappear until I move my mouse over them again.

When I set my layout to null everything works great except I lose the resizing capability, which is not good.

Tried posting images but it wont let me since my rep isnt at 10 yet :(

Any suggestions? I've tried putting the buttons into a JPanel then adding the jpanel but the splitpane overlaps with that. Same with a JToolBar.

The order in which I add my items is:

1) the menu bar

setJMenuBar(menuBar)

2) the buttons

getContentPane().add(btnZoomIn)

etc.

3) the split pane

getContentPane().add(splitPane)

then the rest of the things you see after that


Solution

  • Seems to work fine for me...

    enter image description here

    public class BadLayout06 {
    
        public static void main(String[] args) {
            new BadLayout06();
        }
    
        public BadLayout06() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JMenuBar mb = new JMenuBar();
                    mb.add(new JMenu("File"));
    
                    JToolBar tb = new JToolBar();
                    tb.add(new JButton("Zoom In"));
                    tb.add(new JButton("Zoom Out"));
                    tb.add(new JButton("Invert Image"));
                    tb.add(new JButton("Toggle Highlights"));
                    tb.add(new JButton("Save"));
                    tb.add(new JButton("Cancel"));
                    tb.setFloatable(false);
    
                    JSplitPane spSub = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
                    JTabbedPane tpLeft = new JTabbedPane();
                    tpLeft.add("Table Entry", new JPanel());
                    tpLeft.add("Form Entry", new JPanel());
                    spSub.setLeftComponent(tpLeft);
                    JTabbedPane tpRight = new JTabbedPane();
                    tpRight.add("Field Help", new JPanel());
                    tpRight.add("Image Navigation", new JPanel());
                    spSub.setRightComponent(tpRight);
    
    
                    JSplitPane spMain = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                    spMain.setLeftComponent(new JPanel());
                    spMain.setRightComponent(spSub);
    
    
                    JFrame frame = new JFrame("Testing");
                    frame.setJMenuBar(mb);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(tb, BorderLayout.NORTH);
                    frame.add(spMain, BorderLayout.CENTER);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
    }