0

When I run this program it appears as an empty window until you fullscreen, then it can be resized as you like, why is it doing this/how do I stop it?

the program is very basic just a menubar and two panels split.

public class SplitPane {


    public static void main(String[] args) {
        window view = new window();
    }

    private static class window extends JFrame {



        public window() {
            this.setSize(1000, 750);
            this.setVisible(true);
            this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

       //menubar is here, must lower code quantity for stack

        //panels
           //graph half 
            JPanel graphRep = new JPanel();
            //Background colour - graphRep.setBackground(Color.RED);
            graphRep.setVisible(true);
            String graphTitle = "Textual Representation.";
            Border graphBorder = BorderFactory.createTitledBorder(graphTitle);
            graphRep.setBorder(graphBorder);
            //text half
            JPanel textRep = new JPanel();
            textRep.setVisible(true);
            String textTitle = "Graphical Representation.";
            Border textBorder = BorderFactory.createTitledBorder(textTitle);
            textRep.setBorder(textBorder);

            //splitpane
            JSplitPane splitPane = new JSplitPane();
            splitPane.setSize(600, 750);
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerSize(10);
            splitPane.setDividerLocation(250);
            splitPane.setLeftComponent(graphRep);
            splitPane.setRightComponent(textRep);

            this.add(splitPane);
        }
    }

1 Answer 1

2
this.setVisible(true);

You are making the frame visible BEFORE you add components to the frame. The layout manager is never invoked so the size of all the components remains (0, 0) so there is nothing to paint.

The frame should be made visible AFTER all the components have been added to the frame.

And the code should be:

frame.pack();
frame.setVisible();

So each component is displayed at its proper size. Don't hardcode the size() because you don't know what the size of a users screen might be.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.