
Most of the AWT UI components have become obsolete and should be replaced by newer Swing UI components. There are current three sets of Java APIs for graphics programming: AWT ( Abstract Windowing Toolkit), Swing and JavaFX. I will describe another important OO concept called nested class (or inner class) in this article. I shall assume that you have a good grasp of OOP, including composition, inheritance, polymorphism, abstract class and interface otherwise, read the earlier articles. However, re-using them are not so difficult, if you follow the API documentation, samples and templates provided. Writing your own graphics classes (and re-inventing the wheels) is mission impossible! These graphics classes, developed by expert programmers, are highly complex and involve many advanced design patterns. In this article, I shall show you how you can reuse the graphics classes provided in JDK for constructing your own Graphical User Interface (GUI) applications. These classes are self-contained and are reusable. You can create high-level abstract data types called classes to mimic real-life things. As discussed, OOP permits higher level of abstraction than traditional Procedural-Oriented Languages (such as C).

You can get the orientation of a container based on a given locale by using method getOrientation() before setting orientation.So far, we have covered the basic programming constructs (such as variables, data types, decision, loop, array and method) and introduced the important concept of Object-Oriented Programming (OOP). To set component’s orientation, you can use either of these methods: setComponentOrientation() or appl圜omponentOrientation(). In other languages such as Arabic, the component should be laid out from right to left. It means components are laid out inside a container from left to right and from top to bottom. In English, we have left to right and top to bottom orientation. This component will take up space in a container, therefore, add more space between each component.ĭifferent languages have different orientations. Lightweight invisible component: you can adding space between components by creating the lightweight invisible component.Border: by adding an empty border to change the amount of space between components regardless of Layout Manager.

Some Layout managers allow you to add space, others do not. Some Layout managers also automatically add space between components for you, so refer to each individual Layout Manager to see how it works. Layout Manager: it depends on layout manager in term of manipulating space between components.It is important to understand those methods in order to manipulate space between component effectively. There are several ways you can use to add spaces between components inside a container. Set the horizontal alignment of the componentĪdding space between components inside a container Public void setAlignmentY(float alignmentY) Set the vertical alignment of the component Public void setAlignmentX(float alignmentX) Public void setPreferredSize(Dimension preferredSize) Public void setMinimumSize(Dimension minimumSize) Public void setMaximumSize(Dimension maximumSize) In order to allow the container to laid out the components inside it accurately, you need to provide the alignment and size hints using a list of Swing component methods. Those methods are mostly dealing with minimum, maximum, preferred size, vertical and horizontal alignment of a component.
Set the layout manager to use the default flowlayout code#
To set BoxLayout to a JPanel you can use the following code snippet: If you want to have different layout manager, you can change it when creating new container objects or using setLayout() method when the object had been already created. Setting the layout manager to a containerīy default, JPanel uses FlowLayout and content pane uses BorderLayout when those containers are initialized.

