Question

I have a horizontal split pane that contains a vertical split pane. When the window shows up i want the vertical split pane in the top of the horizontal split pane to be split in the middle. I want the horizontal divider to be in the middle.

I have that working, however, i also want to the horizontal split pane to change its size when the window is maximized. (It currently does not.)

I also have a button box below the horizontal pane and would like it to always be visible when the window is resized. Currently when the window launches i can see everything in the horizontal split. I am unable to see the buttons, because they do not fit in the preferred size of the window (800, 600). But i would like everything to show up in the window automatically and stay Glue'd to the border of the window when it is resized...

How can i do this?

Thanks!

Below is the code i am currently using. I call the create methods in a controller. createView is called first then the rest in sequential order.


public void createView() {
        dialog = new JFrame();
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        dialog.setAlwaysOnTop(true);
        dialog.setBounds(0, 0, 800, 600);
        dialog.setMinimumSize(new Dimension(800, 600));
        dialog.setPreferredSize(new Dimension(800, 600));
        dialog.setResizable(true);
        dialog.setTitle("MJLA Class Control Panel");

        contentPanel = new JPanel();
//      contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        contentPanel.setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        dialog.getContentPane().add(contentPanel, BorderLayout.CENTER);

        classQuizSRTSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
        contentPanel.add(classQuizSRTSplit, BorderLayout.NORTH);

        classQuizSplit = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        classQuizSRTSplit.setTopComponent(classQuizSplit);




//      classQuizHBox = Box.createHorizontalBox();
//      contentPanel.add(classQuizHBox);

        sRTHBox = Box.createHorizontalBox();
        contentPanel.add(sRTHBox);

        buttonBox = Box.createHorizontalBox();
        contentPanel.add(buttonBox, BorderLayout.SOUTH);

        refreshButton = new JButton("Refresh");
        buttonBox.add(refreshButton);

        doneButton = new JButton("Done");
        buttonBox.add(doneButton);

        this.validateView();
    }

    public void createClassTablePanel() {
        this.classTablePanel = new JPanel();
        this.classTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.classTablePanel.setPreferredSize(new Dimension(300, 300));
        this.classTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(classTablePanel);

        this.classQuizSplit.setLeftComponent(classTablePanel);
        classTableModel = cPModel.getClassTableModel();

        classTable = new JTable(this.classTableModel);

        classTable.getSelectionModel().addListSelectionListener(this);
        JScrollPane scrollPane = new JScrollPane(classTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        classTablePanel.add(scrollPane, BorderLayout.CENTER);

        this.validateView();
    }

    public void createQuizTablePanel() {
        this.quizTablePanel = new JPanel();
        this.quizTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.quizTablePanel.setPreferredSize(new Dimension(300, 300));
        this.quizTablePanel.setLayout(new BorderLayout());

//      this.classQuizHBox.add(quizTablePanel);

        this.classQuizSplit.setRightComponent(quizTablePanel);

        quizTableModel = cPModel.getQuizTableModel();
        this.quizSorter = new TableRowSorter<DefaultTableModel>(quizTableModel);

        quizTable = new JTable(this.quizTableModel);
        quizTable.getSelectionModel().addListSelectionListener(this);
        quizTable.setRowSorter(quizSorter);
        JScrollPane scrollPane = new JScrollPane(quizTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        quizTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        quizTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        addQuizButton = new JButton("Add Quiz");
        buttonHBox.add(addQuizButton);

        removeQuizButton = new JButton("Remove Quiz");
        buttonHBox.add(removeQuizButton);

        editQuizButton = new JButton("Edit Quiz");
        buttonHBox.add(editQuizButton);

        this.validateView();
    }

    public void createStudentRecordTablePanel() {
        this.studentRecordTablePanel = new JPanel();
        this.studentRecordTablePanel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
        this.studentRecordTablePanel.setLayout(new BorderLayout());

//      this.sRTHBox.add(studentRecordTablePanel);

        this.classQuizSRTSplit.setBottomComponent(studentRecordTablePanel);

        this.studentRecordTableModel = cPModel.getStudentRecordTableModel();
        this.sRTSorter = new TableRowSorter<DefaultTableModel>(studentRecordTableModel);

        sRTTable = new JTable(this.studentRecordTableModel);
        sRTTable.setRowSorter(sRTSorter);
        JScrollPane scrollPane = new JScrollPane(sRTTable);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        studentRecordTablePanel.add(scrollPane, BorderLayout.CENTER);

        Box buttonHBox = Box.createHorizontalBox();
        studentRecordTablePanel.add(buttonHBox, BorderLayout.SOUTH);

        editGradeButton = new JButton("Edit Grade");
        buttonHBox.add(editGradeButton);

        generateReportButton = new JButton("Generate Report");
        buttonHBox.add(generateReportButton);

        this.validateView();
    }

Another issue.

That fixed one of the problems @TrashGod. However, how can i make the horizontal split pane resize its component to fit the new size of the window, instead of their being that big gap between the done and refresh button and the bottom of the horizontal split pane?

I was thinking that i would have to listen for an event for when the window size changes and then call the pack() method when that happens, is that the only way or would that even work? (Just tested this, it did not work... just puts everything back to preferred sizes. duh)


Initial look. enter image description here


After window maximized. enter image description here

Was it helpful?

Solution

You might look at setResizeWeight(); a value of 0.5 should distribute the space evenly.

The pack() method "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." BorderLayout.NORTH and BorderLayout.SOUTH seem like suitable layouts for staying with the divider.

For additional help, please provide an sscce that exhibits the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top