Create a CompoundBorder and combine two line borders in Java

Description

The following code shows how to create a CompoundBorder and combine two line borders.

Example


/*from   w ww .  j a v a  2  s. com*/
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class Main extends JFrame {

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel();
    JLabel label;

    label = new JLabel("Compound - red inside green lines");
    label.setBorder(BorderFactory.createCompoundBorder(new LineBorder(
        Color.green), new LineBorder(Color.red)));
    panel.add(label);
    getContentPane().add(panel);
    pack();
  }

  public static void main(String[] args) {
    Main s = new Main();
    s.setVisible(true);
  }
}

The code above generates the following result.

Create a CompoundBorder and combine two line borders in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer