Wednesday, October 31, 2007

Layout Manager

  • A layout manager is an object that implements the LayoutManager interface and determines the size and position of the components within a container.
  • Content pane defaults to BorderLayout
  • JPanel layout defaults to FlowLayout
  • A panel's layout manager can be set using the constructor
JPanel panel = new JPanel(new BorderLayout());
  • Alternately it can be set using
panel.setLayout(new FlowLayout());
  • Instead of using a layout absolute positioning can be used by setting the layout to null
  • Arguments to add method depend on the layout manager. For example, BorderLayout requires to specify the area to which the component is to be added

Wednesday, October 17, 2007

Autoboxing/Unboxing

Manual conversion between primitive types (such as an int) and wrapper classes (such as Integer) is necessary when adding a primitive data type to a collection. As an example, consider an int being stored and then retrieved from an ArrayList:

list.add(0, new Integer(59));
int n = ((Integer)(list.get(0))).intValue();

The new autoboxing/unboxing feature eliminates this manual conversion. The above segment of code can be written as:

list.add(0, 59);
int total = list.get(0);

However, note that the wrapper class, Integer for example, must be used as a generic type:

List list = new ArrayList();

Programming language critics may have a lot to say about the autoboxing/unboxing feature. On one hand, Java developers agree that the distinction between primitive data types and references can be burdensome. In a pure object-oriented language there should be no difference between a primitive data type and a reference, as everything is an object. The other issue is that of identity: many think of primitive data types as entities that represent mathematical values, which are different from references.

The purpose of the autoboxing/unboxing feature, however, is simply to ease the interoperability between primitive types and references without any radical changes.


(http://java.sun.com/developer/technicalArticles/releases/j2se15langfeat/)