Java Examples

I wrote the code examples for this part of the site for a couple of reasons. The first is to create a repository of examples that can be used as templates for various projects that I am working on. By placing them on my website, they are accessible to from anywhere as long as I have the web available (which is most of the time). The second is that others might find these examples useful. Your welcome copy any of them and use them as you need to or just to see how they work. The code links are simple text files that are opened in another browser tab. This makes it easier to copy into an editor or an IDE.

Be sure to stay tuned, more will be added later ...

Java using Swing

Simple Frame Example - This is a complete java file of a basic frame. It uses Swing, but has no components. These can be added in later.

Simple Text Box Example - This example builds on the previous example by adding a label, two text fields, a submit button, and an Action Listener. Text written in the text field will display in a label directly after the submit button. This example uses simple flow layout and no panels.

Simple Check Box Example Three check boxes are used in this example. The label of the last one checked will be displayed after the submit button is pressed. Multiple check boxes can be chosen, but only the last one checked will be displayed. If none was checked just before the submit button was clicked, then the answer to life, the universe, and everything will be displayed!

Simple Panel Example This example contains three flow layout panels arranged in a frame. Each panel is contained in a grid layout 3 rows deep by 1 column wide. Each of the rows are indentified by a single label component. The diagram below shows the layout.

Simple Panel Example Diagram

Simple Scroll Pane Example This example uses three scroll panes. It is built on the Simple Panel Example, so it has a 3 row, 1 column, Grid Layout for the frame. Flow Layout is used within each of the rows by default, this is why there is no code defining these.

In each row there is a Scroll Pane that is 5 rows by 20 characters. The first uses no word wrap and vertical and horizontal scroll bars as needed. The second uses the scroll pane method, setLineWrap only and never uses vertical or horizontal scroll bars. Finally, the third uses both scroll pane methods; setLineWrap and setWrapStyleWord. The results are shown below:

Simple Scroll Pane Example Diagram