Chapter 6.7.2 – Borders | Introduction to Programming Using Java

Chapter 6.7.2 – Borders | Introduction to Programming Using Java

 

6.7.2 Borders

 

We have seen how to leave gaps between the components in a container, but what if you would like to leave a border around the outside of the container? This problem is not handled by layout managers. Instead, borders in Swing are represented by objects. A Border object can be added to any JComponent, not just to containers. Borders can be more than just empty space. The class javax.swing.BorderFactory contains a large number of static methods for creating border objects.

 

Java programming 7 Chapter 6.7.2 - Borders | Introduction to Programming Using Java

 

For example, the function BorderFactory.createLineBorder(Color.BLACK) returns an object that represents a one-pixel wide black line around the outside of a component. If comp is a JComponent, a border can be added to comp using its setBorder() method. For example:

 

18 Chapter 6.7.2 - Borders | Introduction to Programming Using Java

 

When a border has been set for a JComponent, the border is drawn automatically, without any further effort on the part of the programmer. The border is drawn along the edges of the component, just inside its boundary. The layout manager of a JPanel or other container will take the space occupied by the border into account. The components that are added to the container will be displayed in the area inside the border. I don’t recommend using a border on a JPanel that is being used as a drawing surface.

However, if you do this, you should take the border into account. If you draw in the area occupied by the border, that part of your drawing will be covered by the border.

 

Java programming 2 Chapter 6.7.2 - Borders | Introduction to Programming Using Java

 

Here are some of the static methods that can be used to create borders:

  • createEmptyBorder(top,left,bottom,right) — leaves an empty border around the edges of a component. Nothing is drawn in this space, so the background color of the component will appear in the area occupied by the border. The parameters are integers that give the width of the border along the top, left, bottom, and right edges of the component. This is actually very useful when used on a JPanel that contains other components. It puts some space between the components and the edge of the panel. It can also be useful on a JLabel, which otherwise would not have any space between the text and the edge of the label.
  • createLineBorder(color,thickness) — draws a line around all four edges of a component. The first parameter is of type Color and specifies the color of the line. The second parameter is an integer that specifies the thickness of the border. If the second parameter is omitted, a line of thickness 1 is drawn.
  • createMatteBorder(top,left,bottom,right,color) — is similar to createLineBorder, except that you can specify individual thicknesses for the top, left, bottom, and right edges of the component.
  • createEtchedBorder() — creates a border that looks like a groove etched around the boundary of the component. The effect is achieved using lighter and darker shades of the component’s background color, and it does not work well with every background color.
  • createLoweredBevelBorder()—gives a component a three-dimensional effect that makes it look like it is lowered into the computer screen. As with an EtchedBorder, this only works well for certain background colors.
  • createRaisedBevelBorder()—similar to a LoweredBevelBorder, but the component looks like it is raised above the computer screen.
  • createTitledBorder(title)—creates a border with a title. The title is a String, which is displayed in the upper left corner of the border.

There are many other methods in the BorderFactory class, most of them providing variations of the basic border styles given here. The following illustration shows six components with six different border styles. The text in each component is the command that created the border for that component:

 

borders

 

(The source code for the applet that produced this picture can be found in BorderDemo.java.)

 

java 2 Chapter 6.7.2 - Borders | Introduction to Programming Using Java

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

1 thought on “Chapter 6.7.2 – Borders | Introduction to Programming Using Java”

Leave a Comment