Custom Border Sample : Border « Swing JFC « Java






Custom Border Sample

Custom Border Sample
   

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class CustomBorderSample {
  public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };
    JFrame frame = new JFrame("Custom Border");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    JList jlist = new JList(labels);
    ListCellRenderer renderer = new FocusedTitleListCellRenderer();
    jlist.setCellRenderer(renderer);
    JScrollPane sp = new JScrollPane(jlist);
    contentPane.add(sp, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

class FocusedTitleListCellRenderer implements ListCellRenderer {
  protected static Border noFocusBorder = new EmptyBorder(15, 1, 1, 1);

  protected static TitledBorder focusBorder = new TitledBorder(LineBorder
      .createGrayLineBorder(), "Focused");

  protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();

  public String getTitle() {
    return focusBorder.getTitle();
  }

  public void setTitle(String newValue) {
    focusBorder.setTitle(newValue);
  }

  public Component getListCellRendererComponent(JList list, Object value,
      int index, boolean isSelected, boolean cellHasFocus) {
    JLabel renderer = (JLabel) defaultRenderer
        .getListCellRendererComponent(list, value, index, isSelected,
            cellHasFocus);
    renderer.setBorder(cellHasFocus ? focusBorder : noFocusBorder);
    return renderer;
  }
}

           
         
    
    
  








Related examples in the same category

1.Border Demo Border Demo
2.Different Swing bordersDifferent Swing borders
3.demonstrate the custom CurvedBorder classdemonstrate the custom CurvedBorder class
4.An example of the MatteBorder classAn example of the MatteBorder class
5.A simple demonstration of the LineBorder class built with rounded cornersA simple demonstration of the LineBorder class built with rounded corners
6.An example of using a titled border on a labelAn example of using a titled border on a label
7.Borders with a BevelBorder used on JLabels as a highlightBorders with a BevelBorder used on JLabels as a highlight
8.MatteBorder DemoMatteBorder Demo
9.TitledBorder DemoTitledBorder Demo
10.How to create the borderHow to create the border
11.Solid border button border 3D borderSolid border button border 3D border
12.Oval borderOval border
13.Border of all kindsBorder of all kinds
14.EtchedBorderEtchedBorder
15.Red Green BorderRed Green Border
16.TitledBorderTitledBorder
17.TitledBorder 3TitledBorder 3
18.BevelBorder 2BevelBorder 2
19.Icon MatteBorderIcon MatteBorder
20.Soft BevelBorderSoft BevelBorder
21.Another TitledBorder 1Another TitledBorder 1
22.BevelBorderBevelBorder
23.EmptyBorderEmptyBorder
24.LineBorder DemoLineBorder Demo
25.CompoundBorder DemoCompoundBorder Demo
26.Adding a Title to a Border
27.Create a title border from another border
28.Change border text Justification alignment style
29.Bottom Border
30.Right border
31.Rectangle border
32.Set title position
33.Flex Border