Java Reference
In-Depth Information
g.setColor(verticalColor);
// Left
g.fillRect(0, insets.top, insets.left, height-insets.top-insets.bottom);
// Right
g.fillRect(width-insets.right, insets.top, insets.right,
height-insets.top-insets.bottom);
g.translate(-x, -y);
}
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("My Border");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Border border = new RedGreenBorder();
JButton helloButton = new JButton("Hello");
helloButton.setBorder(border);
JButton braveButton = new JButton("Brave New");
braveButton.setBorder(border);
braveButton.setEnabled(false);
JButton worldButton = new JButton("World");
worldButton.setBorder(border);
frame.add(helloButton, BorderLayout.NORTH);
frame.add(braveButton, BorderLayout.CENTER);
frame.add(worldButton, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Note Another interesting custom border is one that displays an active component instead of a text title in
a TitledBorder . Imagine a border that has a JCheckBox or JRadioButton instead of a text string for the
title. You can also use a JLabel and pass in HTML for the text.
Summary
In this chapter, you learned about the use of the Border interface and its many predefined
implementations. You also learned how to create predefined borders using the Factory design
pattern provided by the BorderFactory class. Lastly, you saw how to define your own borders
and why subclassing AbstractBorder is beneficial.
In Chapter 8, you'll move beyond low-level components and examine the window-like
container objects available in Swing.
 
Search WWH ::




Custom Search