May 20, 2009

Published May 20, 2009 by

Using Borders for Components

Class BorderFactory has various methods for rendering a border around the edges of a swing component.

Some are:

Empty Border: It takes up space but does no drawing, specifying the width of the top, left, bottom, and right sides.



Etched Border: It’s a border with an "etched" look using the component's current background color for highlighting and shading. There are two types : EtchedBorder.RAISED, or EtchedBorder.LOWERED





Line Border: A line border with the specified color.



Bevel Border: It’s a beveled border of the specified type (either BevelBorder.LOWERED or BevelBorder.RAISED), using the specified highlighting and shadowing. The outer edge of the highlighted area uses a brighter shade of the highlight color. The inner edge of the shadow area uses a brighter shade of the shadow color.





Titled Border: It adds a title to an existing border, with positioning (example, sitting on the top line), justification (example, leading) and the default font and text color (determined by the current look and feel).



Justification can be one of the following:
• TitledBorder.LEFT
• TitledBorder.CENTER
• TitledBorder.RIGHT
• TitledBorder.LEADING
• TitledBorder.TRAILING
• TitledBorder.DEFAULT_JUSTIFICATION (leading)
Vertical position of the text in relation to the border can be one of the following:
• TitledBorder.ABOVE_TOP
• TitledBorder.TOP (sitting on the top line)
• TitledBorder.BELOW_TOP
• TitledBorder.ABOVE_BOTTOM
• TitledBorder.BOTTOM (sitting on the bottom line)
• TitledBorder.BELOW_BOTTOM
• TitledBorder.DEFAULT_POSITION (top)


Source Code for the programs:

public class BordersViewer extends javax.swing.JFrame {
       
    public BordersViewer() {
        initComponents();
    }

    
    private void initComponents() {

        borderTypesButtonGroup = new javax.swing.ButtonGroup();
        borderTypesPanel = new javax.swing.JPanel();
        rbtnEmpty = new javax.swing.JRadioButton();
        rbtnLoweredEtched = new javax.swing.JRadioButton();
        rbtnRaisedEtched = new javax.swing.JRadioButton();
        rbtnLine = new javax.swing.JRadioButton();
        rbtnLoweredBevel = new javax.swing.JRadioButton();
        rbtnRaisedBevel = new javax.swing.JRadioButton();
        rbtnTitled = new javax.swing.JRadioButton();
        borderPanel = new javax.swing.JPanel();
        lblBorder = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        borderTypesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Border Types"));

        borderTypesButtonGroup.add(rbtnEmpty);
        rbtnEmpty.setSelected(true);
        rbtnEmpty.setText("Empty");
        rbtnEmpty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnEmptyActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnEmpty);

        borderTypesButtonGroup.add(rbtnLoweredEtched);
        rbtnLoweredEtched.setText("Lowered Etched");
        rbtnLoweredEtched.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLoweredEtchedActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLoweredEtched);

        borderTypesButtonGroup.add(rbtnRaisedEtched);
        rbtnRaisedEtched.setText("Raised Etched");
        rbtnRaisedEtched.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnRaisedEtchedActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnRaisedEtched);

        borderTypesButtonGroup.add(rbtnLine);
        rbtnLine.setText("Line");
        rbtnLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLineActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLine);

        borderTypesButtonGroup.add(rbtnLoweredBevel);
        rbtnLoweredBevel.setText("Lowered Bevel");
        rbtnLoweredBevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnLoweredBevelActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnLoweredBevel);

        borderTypesButtonGroup.add(rbtnRaisedBevel);
        rbtnRaisedBevel.setText("Raised Bevel");
        rbtnRaisedBevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnRaisedBevelActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnRaisedBevel);

        borderTypesButtonGroup.add(rbtnTitled);
        rbtnTitled.setText("Titled");
        rbtnTitled.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnTitledActionPerformed(evt);
            }
        });
        borderTypesPanel.add(rbtnTitled);

        borderPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        lblBorder.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lblBorder.setText("This is an Empty Border");

        javax.swing.GroupLayout borderPanelLayout = new javax.swing.GroupLayout(borderPanel);
        borderPanel.setLayout(borderPanelLayout);
        borderPanelLayout
                .setHorizontalGroup(borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, borderPanelLayout.createSequentialGroup()
                                .addContainerGap(175, Short.MAX_VALUE).addComponent(lblBorder).addGap(194, 194, 194)));
        borderPanelLayout
                .setVerticalGroup(borderPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(borderPanelLayout.createSequentialGroup().addGap(71, 71, 71).addComponent(lblBorder)
                                .addContainerGap(92, Short.MAX_VALUE)));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(borderPanel, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                .addComponent(borderTypesPanel, javax.swing.GroupLayout.Alignment.LEADING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(borderTypesPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(borderPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(39, Short.MAX_VALUE)));

        pack();
    }//

    private void rbtnEmptyActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        lblBorder.setText("This is an Empty Border");
    }

    private void rbtnLoweredEtchedActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        lblBorder.setText("This is a Lowered Etched Border");
    }

    private void rbtnRaisedEtchedActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
        lblBorder.setText("This is a Raised Etched Border");
    }

    private void rbtnLineActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        lblBorder.setText("This is a Line Border");
    }

    private void rbtnLoweredBevelActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        lblBorder.setText("This is a Lowered Bevel Border");
    }

    private void rbtnRaisedBevelActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        lblBorder.setText("This is a Raised Bevel Border");

    }

    private void rbtnTitledActionPerformed(java.awt.event.ActionEvent evt) {
        borderPanel.setBorder(javax.swing.BorderFactory
                .createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Titled Border"));
        lblBorder.setText("This is a Titled Border");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new BordersViewer().setVisible(true);
            }
        });
    }

    private javax.swing.JPanel borderPanel;
    private javax.swing.ButtonGroup borderTypesButtonGroup;
    private javax.swing.JPanel borderTypesPanel;
    private javax.swing.JLabel lblBorder;
    private javax.swing.JRadioButton rbtnEmpty;
    private javax.swing.JRadioButton rbtnLine;
    private javax.swing.JRadioButton rbtnLoweredBevel;
    private javax.swing.JRadioButton rbtnLoweredEtched;
    private javax.swing.JRadioButton rbtnRaisedBevel;
    private javax.swing.JRadioButton rbtnRaisedEtched;
    private javax.swing.JRadioButton rbtnTitled;


}
Read More