How can I make the Jpanel to fill the remaining space remaining inside another panel?

Advertisements

I started out with Swing a few days ago and I was trying to make a UI for a text editor like the one I’ve attached below
Sample UI

This might be a stupid question but I’ve made the toolbar and added a border but when I am trying to add the drawing pad it’s not filing up the rest of the space?
My UI

Here is the code, can someone tell me what I am doing wrong?

import javax.swing.*;
import java.awt.*;

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Text Editor");
        GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        GraphicsConfiguration config = graphicsDevice.getDefaultConfiguration();
        frame.setSize(config.getBounds().getSize());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        menuBar(frame);
        sketchPad(frame);

        frame.setVisible(true);

    }

    public static void menuBar(JFrame frame){
        JMenuBar menuBar = new JMenuBar();
        JMenu file= new JMenu("File");
        String[] fileItems = {"New Tab","New Window","Open","Save","Save As","Save All", "Page Setup","Print","Close Tab","Close Window","Exit"};
        for(String item:fileItems){
            JMenuItem menuItem = new JMenuItem(item);
            file.add(menuItem);
        }
        menuBar.add(file);

        JMenu edit = new JMenu("Edit");
        String[] editItems = {"Undo","Cut","Copy","Paste","Delete","Go To","Select All","Time/Date"};
        for(String item:editItems){
            JMenuItem menuItem = new JMenuItem(item);
            edit.add(menuItem);
        }
        menuBar.add(edit);

        JMenu review = new JMenu("Review");
        String[] reviewItems = {"Zoom","Status Bar","Word Wrap"};
        for(String item:reviewItems){
            if(item.equals("Zoom")){
                JMenu subMenu = new JMenu("Zoom");
                JMenuItem in,out,setDefault;
                in = new JMenuItem("Zoom In");
                subMenu.add(in);
                out = new JMenuItem("Zoom Out");
                subMenu.add(out);
                setDefault = new JMenuItem("Default");
                subMenu.add(setDefault);
                review.add(subMenu);
            }else {
                JMenuItem menuItem = new JMenuItem(item);
                review.add(menuItem);
            }
        }
        menuBar.add(review);
        
        JMenu help = new JMenu("Help");
        String[] helpItems = {"Help","About"};
        for(String item:helpItems){
            JMenuItem menuItem = new JMenuItem(item);
            help.add(menuItem);
        }
        menuBar.add(help);
        
        frame.setJMenuBar(menuBar);
    }

    public static void sketchPad(JFrame frame){
        JPanel sketchPad = new JPanel();
        sketchPad.setLayout(new BorderLayout());
        sketchPad.add(new JLabel("Sketch Pad"),BorderLayout.NORTH);
        JPanel toolBar = new JPanel();
        toolBar.setLayout(new FlowLayout());
        toolBar.add(new JButton("Rectangle"));
        toolBar.add(new JButton("Circle"));
        toolBar.add(new JButton("Line"));
        toolBar.add(new JButton("Triangle"));
        toolBar.add(new JButton("Pentagon"));
        toolBar.add(new JButton("Clear"));
        sketchPad.add(toolBar,BorderLayout.CENTER);
        JPanel drawingPad = new JPanel();
        drawingPad.setBackground(Color.GRAY);
        sketchPad.add(drawingPad,BorderLayout.SOUTH);
        sketchPad.setBorder(BorderFactory.createLineBorder(Color.black));
        frame.add(sketchPad,BorderLayout.EAST);
    }
}

I have tried using the setMinimumSize, setSize, getPreferedSize methods, but nothing seems to work. If I change the layout, it messes up everything

>Solution :

Prefer adding drawingPad at the center.
As you need both a label and a toolbar at north add both in a dedicated panel.

public static void sketchPad(JFrame frame){
    JPanel toolBar = new JPanel();
    toolBar.setLayout(new FlowLayout());
    toolBar.add(new JButton("Rectangle"));
    toolBar.add(new JButton("Circle"));
    toolBar.add(new JButton("Line"));
    toolBar.add(new JButton("Triangle"));
    toolBar.add(new JButton("Pentagon"));
    toolBar.add(new JButton("Clear"));

    JPanel control = new JPanel();
    control.setLayout(new BorderLayout());
    control.add(new JLabel("Sketch Pad"),BorderLayout.NORTH);
    control.add(toolBar,BorderLayout.SOUTH);
  
    JPanel drawingPad = new JPanel();
    drawingPad.setBackground(Color.GRAY);

    JPanel sketchPad = new JPanel();
    sketchPad.setLayout(new BorderLayout());
    sketchPad.add(control,BorderLayout.NORTH);
    sketchPad.add(drawingPad,BorderLayout.CENTER);
    sketchPad.setBorder(BorderFactory.createLineBorder(Color.black));
    frame.add(sketchPad,BorderLayout.EAST);
}

Leave a ReplyCancel reply