2

I am creating a JFrame object with some JPanels next to each other side by side.

I want the JPanels to have a 15px margin, etched border, and 15px padding. At first I thought that this would be something really intuitive just like the HTML box model, so I tried to create CompoundBorder inside a CompoundBorder but that wouldn't work.

Here's my code:

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

public class StackOverFlowExample extends JFrame {

    public static void main() {
        stackOverFlowExample window = new stackOverFlowExample();
        window.setVisible(true);
    }

    public StackOverFlowExample() {
        // create buttons
        JButton foo = new JButton("foo");
        JButton bar = new JButton("bar");
        JButton foo2 = new JButton("foo2");
        JButton bar2 = new JButton("bar2");

        // create panels and add buttons to them
        JPanel left = new JPanel();
        left.setBorder(BorderFactory.createEtchedBorder());
        left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
        left.add(foo);
        left.add(bar);
        JPanel right = new JPanel();
        right.setBorder(BorderFactory.createEtchedBorder());
        right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
        right.add(foo2);
        right.add(bar2);

        // add panels to frame
        this.getContentPane().setLayout(new BoxLayout(
            getContentPane(), BoxLayout.LINE_AXIS));
        this.getContentPane().add(left);
        this.getContentPane().add(right);

        // finalize layout
        this.setPreferredSize(new Dimension(150,150));
        this.pack();
        this.setVisible(true);
    }
}

I'm aware that I could have just used GridBagConstraints or JButton.setMargin() to create the padding, and then use CompoundBorder to create the etched border with an empty border. What if I don't want to make my code look messy with those techniques though?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    +1 for MCVE, but where is this CompoundBorder you are talking about? I don't see it being used once – Paul Samsotha Aug 10 '14 at 10:46
  • 1
    *"so I tried to create CompoundBorder inside a CompoundBorder but that wouldn't work."* It should work. Why did it not work here? – Andrew Thompson Aug 10 '14 at 10:47
  • 1this.setPreferredSize(new Dimension(150,150));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Aug 10 '14 at 10:49

3 Answers3

3

I'm not sure what problems you might be having, as you've not supplied an example of what you've tried, but the basic process would be to...

  • Create the inner border requirements (EtchedBorder wrapping a EmptyBorder), for example, new CompoundBorder(emptyBorder, etchedBorder)
  • Create the outer border requirements (EmptyBorder wrapping the inner compound border), for example, new CompoundBorder(inner, emptyBorder);
  • Apply this outer border to the component...

As an example...

Border

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

public class Test1 {

    public static void main(String[] args) {
        new Test1();
    }

    public Test1() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {

            setLayout(new GridBagLayout());

            EmptyBorder emptyBorder = new EmptyBorder(15, 15, 15, 15);
            EtchedBorder etchedBorder = new EtchedBorder();

            CompoundBorder inner = new CompoundBorder(emptyBorder, etchedBorder);
            CompoundBorder outter = new CompoundBorder(inner, emptyBorder);

            JPanel panel = new JPanel(new GridBagLayout());
            panel.setBorder(outter);

            panel.add(new JButton("Hello"));

            add(panel);

        }

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
3

enter image description here

import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

public class ThreePartBorder {

    public static void main(String[] args) {
        final BufferedImage bi = new BufferedImage(
                400, 100, BufferedImage.TYPE_INT_RGB);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JLabel l = new JLabel(new ImageIcon(bi));
                Border twoPartBorder = new CompoundBorder(
                        new EmptyBorder(15, 15, 15, 15),
                        new EtchedBorder());
                Border threePartBorder = new CompoundBorder(
                        twoPartBorder,
                        new EmptyBorder(15, 15, 15, 15));
                l.setBorder(threePartBorder);

                JFrame f = new JFrame("Three Part Border");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setContentPane(l);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

I came back and just realized I asked a dumb question haha. Both answers above are very helpful and helped me solve the problem so I accepted one of them. Here's my solution after reading the two answers...

enter image description here

    left.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createCompoundBorder(
                    BorderFactory.createEmptyBorder(10,10,10,10),   // margin
                    BorderFactory.createEtchedBorder()              // border
            ),          
            BorderFactory.createEmptyBorder(50,50,50,50)            // padding
    ));

    right.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createEmptyBorder(10,10,10,10),               // margin
        BorderFactory.createCompoundBorder(
                BorderFactory.createEtchedBorder(),                 // border
                BorderFactory.createEmptyBorder(50,50,50,50)        // padding
        )                   
    ));
mKorbel
  • 109,525
  • 20
  • 134
  • 319