How to Create a Title Border in Java

This tutorial will teach you how to create a border with title in Java. A titled border has a combination of a String/text for title and any borders that put inside its text. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of titleBorder.java. 2. Import the following package library.
  1. import javax.swing.*; // used to access BorderFactory, JButton, and JFrame class
  2. import javax.swing.border.*; //used to access the TitledBorder subclass of the border class
  3. import java.awt.*; // used to access the Font and Color class
3. We will initialize variables in our Main, variable frame as JFrame and button as JButton.
  1. JFrame frame = new JFrame("Title Border");
  2. JButton button = new JButton();
4. Now, we will create first the titled border in the top. We will use the TitledBorder class here and named its variable as topBorder and we will set its position to the top using setTitlePosition method.
  1. TitledBorder topBorder = BorderFactory.createTitledBorder("This is a header");
  2. topBorder.setTitlePosition(TitledBorder.TOP);
Next we will create a font for our title in the bottom border. Tahoma as the font, Italic as the font style, and 12 as the font size.
  1. Font font = new Font("Tahoma", Font.ITALIC, 12);
We will create again the TitledBorder class for the bottom position with right alignment, have its font, and will have its foreground color as red.
  1. TitledBorder bottomBorder = new TitledBorder(topBorder, "This is a Footer", TitledBorder.RIGHT, TitledBorder.BOTTOM, font, Color.red);
To have the bottomBorder visible, we will add it on the button that we have declared. We will use the setBorder method here.
  1. button.setBorder(bottomBorder);
5. Lastly, add the button to the frame with a default BorderLayout of Center position, set its size and visibility, and its close operation. Have this code below:
  1. frame.getContentPane().add(button, "Center");
  2. frame.setSize(300, 100);
  3. frame.setVisible(true);
  4. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*; // used to access BorderFactory, JButton, and JFrame class
  2. import javax.swing.border.*; //used to access the TitledBorder subclass of the border class
  3. import java.awt.*; // used to access the Font and Color class
  4. public class titleBorder {
  5. public static void main(String args[]) {
  6. JFrame frame = new JFrame("Title Border");
  7. JButton button = new JButton();
  8.  
  9.  
  10.  
  11. TitledBorder topBorder = BorderFactory.createTitledBorder("This is a header");
  12. topBorder.setTitlePosition(TitledBorder.TOP);
  13.  
  14.  
  15. Font font = new Font("Tahoma", Font.ITALIC, 12);
  16.  
  17. TitledBorder bottomBorder = new TitledBorder(topBorder, "This is a Footer", TitledBorder.RIGHT, TitledBorder.BOTTOM, font, Color.red);
  18.  
  19. button.setBorder(bottomBorder);
  20.  
  21. frame.getContentPane().add(button, "Center");
  22. frame.setSize(300, 100);
  23. frame.setVisible(true);
  24. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. }
  26. }
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

Comments

Add new comment