by Dinesh Thakur Category: Swing

Every swing component that is a subclass of JComponent can have a border. A border is a decorative element that virtually group components by drawing a line around them. By default, a component does not have a border.

import javax.swing.*;

import javax.swing.border.*;
class JPanelExample extends JFrame
{
     JPanelExample()
     {
       JPanel p1 = new JPanel();
       JLabel lblRoll = new JLabel("Rollno");
       JLabel lblName = new JLabel("Name");
       JTextField txtRoll = new JTextField(15);
       JTextField txtName = new JTextField(15);
       Border b = BorderFactory.createTitledBorder("Student Information");    
       p1.setBorder(b);
       p1.add(lblRoll);
       p1.add(txtRoll);
       p1.add(lblName);
       p1.add(txtName);
       add(p1);
     }
}
   class JavaSwingBorderExample
  {
     public static void main(String args[])
     {
       JPanelExample frame = new JPanelExample();
       frame.setTitle("Java Swing Border Example");
       frame.setBounds(200,250,250,150);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setVisible(true);
     }
  }

Java Swing Border in Panel Example



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles