SwingBorder.java Source code

Java tutorial

Introduction

Here is the source code for SwingBorder.java

Source

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.border.AbstractBorder;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;

public class SwingBorder extends JFrame {
    Container frameContainer;

    JPanel panel = new JPanel();

    JMenuBar menuBar = new JMenuBar();

    JMenu fileMenu = new JMenu("File");

    JMenuItem fileExit = new JMenuItem("Exit");

    JMenu borderMenu = new JMenu("Border");

    String[] borderTypes = { "Bevel", "Compound", "Empty", "Etched", "Line", "Matte", "SoftBevel", "Titled" };

    JRadioButtonMenuItem[] borders = new JRadioButtonMenuItem[borderTypes.length];

    AbstractBorder[] border = { new BevelBorder(BevelBorder.LOWERED),
            new CompoundBorder(new LineBorder(Color.blue, 10), new LineBorder(Color.red, 5)),
            new EmptyBorder(10, 10, 10, 10), new EtchedBorder(), new LineBorder(Color.blue, 10),
            new MatteBorder(new ImageIcon("phone.gif")), new SoftBevelBorder(BevelBorder.RAISED),
            new TitledBorder("TitledBorder") };

    ButtonGroup buttonGroup = new ButtonGroup();

    public SwingBorder() {
        super("");
        fileMenu.add(fileExit);
        for (int i = 0; i < borderTypes.length; ++i) {
            borders[i] = new JRadioButtonMenuItem(borderTypes[i]);
            buttonGroup.add(borders[i]);
            borderMenu.add(borders[i]);
        }
        menuBar.add(fileMenu);
        menuBar.add(borderMenu);
        setJMenuBar(menuBar);

        frameContainer = getContentPane();
        frameContainer.setLayout(new BorderLayout());
        frameContainer.add("Center", panel);

        setupEventHandlers();
        setSize(450, 450);
        setVisible(true);
    }

    void setupEventHandlers() {
        addWindowListener(new WindowHandler());
        fileExit.addActionListener(new MenuItemHandler());
        for (int i = 0; i < borders.length; ++i)
            borders[i].addItemListener(new ItemHandler());
    }

    public static void main(String[] args) {
        SwingBorder app = new SwingBorder();
    }

    public class WindowHandler extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }

    public class MenuItemHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            if (cmd.equals("Exit"))
                System.exit(0);
        }
    }

    public class ItemHandler implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            JRadioButtonMenuItem button = (JRadioButtonMenuItem) e.getItem();
            String label = button.getText();
            for (int i = 0; i < borderTypes.length; ++i) {
                if (label.equals(borderTypes[i])) {
                    panel.setBorder(border[i]);
                    repaint();
                }
            }
        }
    }
}