Search code examples
javaswingpaintcomponent

How to draw a white rectangle at the bottom of a JComponent?


In my code i have:

A Gui with a JScrolledPane containing a Box which contains some JPanel (they could be refactor as JComponent if needed).

See Image 1 :

example

I am trying to:

Find the best way to add a white space between them.

I tried with the Box.createVerticalStruct, but it complicates the managment. I don't want to add a new white panel to my object representing the coloredPanel too, so i googled it and it seems to me that there are other 2 ways: using inset or overriding the paint method.

What do you think is the best way in my case? in case you'd suggest to use the paintComponent method, how do i retrieve the bottom corner position to draw a white rectangle of a certain height?

The final results should be something like this:

enter image description here

Here is some code you can use to make some try (which has been written on purpose, so don't take too much care on good writing techniques):

public class TheMotherPuckerGlue2 {

    public static void main(String [] a) {
        final JFrame frame = new JFrame();
        frame.setSize(500, 500);
        frame.setTitle("myCode");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        JScrollPane myPane = new JScrollPane();
        myPane.setPreferredSize(new Dimension(350,200));
        myPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        JPanel viewPort = new JPanel(new BorderLayout());
        viewPort.setBackground(Color.WHITE);

        Box myBox = Box.createVerticalBox(); 
        myBox.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));

        viewPort.add(myBox,BorderLayout.PAGE_START);

        class ColoredPanel extends JPanel{
            public ColoredPanel(Color aColor){
                super.setBackground(aColor);
                super.setPreferredSize(new Dimension(100,60));
            }

        }

        ColoredPanel panel1 = new ColoredPanel(Color.green);
        ColoredPanel panel2 = new ColoredPanel(Color.yellow);
        ColoredPanel panel3 = new ColoredPanel(Color.red);

        myBox.add(panel1);
        myBox.add(panel2);
        myBox.add(panel3);

        viewPort.add(myBox,BorderLayout.PAGE_START);


        myPane.setViewportView(viewPort);

        frame.add(myPane);

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                frame.setVisible(true);
            }
        });
   }

}

Thanks


Solution

  • I found a great way to do that by rewriting the paintComponent that way:

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawWhiteSpace(g);
        drawBorder(g);
    }
    

    drawWhiteSpace method will draw a white rectangle which start at the bottom-left corner -6 pixel, and go to the bottom rightcorner

    private void drawWhiteSpace(Graphics g) {
        int x1 = 0;
        int y1 = this.getSize().height-6;
        int x2 = this.getSize().width;
        int y2 = this.getSize().height;
        g.setColor(Color.WHITE);
        g.fillRect(x1, y1, x2, y2);     
    }   
    

    however the border was not fine, so i had to remove it and to draw it myself by drawing it from the top-left corner to the top-right corner of the white space

    private void drawBorder(Graphics g) {
        int x1 = 0;
        int y1 = 0;
        int x2 = this.getSize().width-1;
        int y2 = this.getSize().height-6;
        g.setColor(Color.GRAY);
        g.drawRect(x1, y1, x2, y2); 
    }