How to Create a Compound Border in Java

This tutorial will teach you how to create a compound border in Java. A compound border is a border nested with other borders. Thus, it is composed of two border objects. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of compoundBorders.java. 2. Import the following package library:
  1. import java.awt.*; //used to access Color and GridLayout class
  2. import javax.swing.*; //used to access BorderFactory, JFrame, and JButton class
  3. import javax.swing.border.*; //used to access Border, LineBorder, and CompoundBorder class
3. We will initialize variables in our Main, variable frame as JFrame and button as JButton.
  1. JFrame frame = new JFrame("Compound Borders");
  2. JButton button = new JButton("A nested border");
4. Now, we will create first the line borders. Remember that the first parameter of the createLineBorder method from the BorderFactory class is the color of the line and next is the thickness of the line as a pixel.
  1. Border redBorder = BorderFactory.createLineBorder(Color.RED, 3);
  2. Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 3);
  3. Border orangeBorder = BorderFactory.createLineBorder(Color.ORANGE, 3);
  4. Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 3);
  5. Border blueBorder = BorderFactory.createLineBorder(Color.BLUE, 3);
  6. Border magentaBorder = BorderFactory.createLineBorder(Color.MAGENTA, 3);
Then, to declare the Compound Border we will use the Border class to instantiate the CompoundBorder class. As such, this is a parameter for creating a CompoundBorder:
  1. public static CompoundBorder createCompoundBorder(Border outside, Border inside)
  2. Border compoundBorder = BorderFactory.createCompoundBorder(lineBorder, matteBorder);
Now, we will nest all the line borders that we have created above using the CompoundBorder. Have this code below:
  1. Border border1 = new CompoundBorder(redBorder, yellowBorder);
  2. Border border2 = new CompoundBorder(border1, orangeBorder);
  3. Border border3= new CompoundBorder(border2, greenBorder);
  4. Border border4 = new CompoundBorder(border3, blueBorder);
  5. Border border5 = new CompoundBorder(border4, magentaBorder);
Add the last border that you have created in your CompoundBorder in the button.
  1. button.setBorder(border5);
5. Now, we will have its layout into Grid Layout using the setLayout method of the frame.
  1. frame.getContentPane().setLayout(new GridLayout(1,1));
Then add the buttons into the frame using the add method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane().add(button1);
  2. frame.getContentPane().add(button2);
  3. frame.setSize(300, 100);
  4. frame.setVisible(true);
  5. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: outputHere's the full code of this tutorial:
  1. import java.awt.*; //used to access Color and GridLayout class
  2. import javax.swing.*; //used to access BorderFactory, JFrame, and JButton class
  3. import javax.swing.border.*; //used to access Border, LineBorder, and CompoundBorder class
  4.  
  5.  
  6. public class compoundBorders {
  7. public static void main(String args[]) {
  8. JFrame frame = new JFrame("Compound Borders");
  9. JButton button = new JButton("A nested border");
  10.  
  11. Border redBorder = BorderFactory.createLineBorder(Color.RED, 3);
  12. Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 3);
  13. Border orangeBorder = BorderFactory.createLineBorder(Color.ORANGE, 3);
  14. Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 3);
  15. Border blueBorder = BorderFactory.createLineBorder(Color.BLUE, 3);
  16. Border magentaBorder = BorderFactory.createLineBorder(Color.MAGENTA, 3);
  17.  
  18. Border border1 = new CompoundBorder(redBorder, yellowBorder);
  19. Border border2 = new CompoundBorder(border1, orangeBorder);
  20. Border border3= new CompoundBorder(border2, greenBorder);
  21. Border border4 = new CompoundBorder(border3, blueBorder);
  22. Border border5 = new CompoundBorder(border4, magentaBorder);
  23.  
  24.  
  25. button.setBorder(border5);
  26.  
  27. frame.getContentPane().setLayout(new GridLayout(1, 1));
  28. frame.getContentPane().add(button);
  29. frame.setSize(300, 100);
  30. frame.setVisible(true);
  31. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. }
  33. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment