Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Swing

JCheckBoxMenuItem & Checked

Java Swing JCheckBox MenuItem Example

The example has a menu called fruits. When the user clicks the Fruits menu, they can see four Checkbox menu items along with Image Icons. At the bottom of the screen, there is a text field which reports all the checked check boxes. In the above picture, Banana & Cherry check boxes are checked and hence the text field shows Banana and Cherry in it. When the user clicks a menu item, a check mark appears in the check box, and it disappears when the user clicks it again.

Continue Reading →

JMenuBar, JMenu, JMenuItem

Java Swing JMenu, JMenuItem - Example

This example has a JMenuBar with two JMenu in it. The menus are File and Edit. File Menu has 4 MenuItems which include a JSeparator. This separator is separating Open, Save menu items with the Exit menu items. When you click Open MenuItem under the File Menu, a dialog will be displayed. This helps us to learn how to respond to the Menu Item click event. The Edit menu has 3 Menu Items. Now let us code this example.

Continue Reading →

BoxLayout – Learning Glue & Strut

Java Swing Glue and Strut Example

Here, we will create two examples. One on the left, which makes use of Glue component and the one on the right uses the struts. In both the examples, we have three JPanels with BoxLayout. Two JPanels use Vertical BoxLayout and one JPanel in the Frame’s top uses horizontal BoxLayout. The JPanel contents are below:

1) Right Panel: Button1, Button2, Button3
2) Left Panel: Button7, Button8, Button9
3) Top Panel: Button4, Button5, Button6

With these two examples, we can study how the Struts and Glues behave when the containing container resizes. Note, to study the resizing of Glues and Struts, we will use the edges of the BorderLayout.

Continue Reading →

BoxLayout – Vertical & Horizontal

Java Swing BoxLayout Example

In our example, we have a JFrame window. A FlowLayout Manager takes care of laying out the controls. We have three JPanels. JFrame’s FlowLayout Manager packs the JPanels and in our above screenshot, we have ample space to layout all three JPanels in a single row. First JPanel on the left and third JPanel on the Right, both are taken over by two BoxLayout managers. Both these panel pack the controls vertically. The Layout in the middle packs two JTextArea controls and this panel packs the TextArea horizontally. Note, there is JSeparator which divides both these JTextArea, and the separator run vertical.

Continue Reading →