Thursday, January 16, 2014

Java Swing example using Border

Java Swing example using Border

package javaswingborder;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
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;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaSwingBorder extends JFrame 
    implements ListSelectionListener{

    JList jList;
    JLabel jLabelInfo;
    
    static final String borderTypeArray[] = {
        "Bevel", 
        "Compound", 
        "Empty", 
        "Etched", 
        "Line", 
        "Matte", 
        "SoftBevel",
        "Titled" };
    
    AbstractBorder[] borderArray = {
        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(5, 10, 5, 10, Color.GREEN), 
        new SoftBevelBorder(BevelBorder.RAISED),
        new TitledBorder("TitledBorder") };

    private void prepareUI() {
        
        JPanel hPanel = new JPanel();
        hPanel.setLayout(new BoxLayout(hPanel, BoxLayout.X_AXIS));
        
        jList = new JList(borderTypeArray);
        jList.addListSelectionListener(this);
        hPanel.add(jList);
        jLabelInfo = new JLabel("java-buddy.blogspot.com");
        hPanel.add(jLabelInfo);

        getContentPane().add(hPanel, BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JavaSwingBorder myFrame = new JavaSwingBorder();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.prepareUI();
        myFrame.pack();
        myFrame.setVisible(true);
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        int selectedIndex = jList.getSelectedIndex();
        String selectedType = (String)jList.getSelectedValue();
        jLabelInfo.setText(selectedType);
        jLabelInfo.setBorder(borderArray[selectedIndex]);
    }

}

No comments:

Post a Comment