Programming Examples

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

Java

Articles on Java programming language.

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 →

JSplitPane – Splitting the Components

JSplitPane Example

The example shows two JTextField controls. A JSplitPane splits these two text fields vertically by running a divider horizontally. You can see the divider with a pair of arrows on the left side. These arrows help to expand the component when clicked. For example, if you click the down-arrow, top text field expands and bottom text field holds only the minimal needed space. When you click the down-arrow again, the JSplitPane hides bottom text field and shows only the top text field. This aspect is called One-Click-Expandable.

We can also adjust the size of the component by manually moving the divider. Note, the split pane can divide the component horizontally as well. Now, we will create this example and learn about the JSplitPane and in the meantime, you can watch the below YouTube video to learn the basics.

Continue Reading →

JTabbedPane – Add Tabs Dynamically

JTabbed Pane Example

Marker 1 and 2 show the controls placed on a panel and the panel placed on the top portion of the Frame Window. The Marker 3 and 4 together form the JTabbedPane. Marker three shows three tabs: Person Name, Person Age and Education. This shows an idea of how we packed related information in a Tab. The above screen shows that the user is on the Person Name tab. To switch between the tabs, the user will click on the Tab Name.

Buttons Top, Bottom, Left and Right changes the tab placement. For example, if you click Left, the tabs will get arranged in the left edge of the Frame Window. The Radio buttons Scroll, and Wrap will decide how the JTabbedPane arranges the tabs when there is no space to fit-in the new tab.

Continue Reading →

JProgressBar – Long Running Task Progress

Java-Swing-JProgressBar-Example

After the JLabel, we have a JProgressBar component which occupies the entire width of the Frame window. Towards the bottom we have a JButton which will increment the progress bar value on each click. The first checkbox will set the progress maximum to 500 when checked. Second check box will change the JProgressBar Color and the third one will set the progress label on the progress bar. So, to test how ProgressBar works, one can make the combination of the three check boxes and use the JButton to mimic the progress of the long running task.

Continue Reading →