Java tutorial

Revision as of 5 August 2014 15:33 by sannanordgren (Comments | Contribs)

This guide was written to help people with no Java experience get into the more advanced functions offered via the skin interface, for example the ability to write beanshell scripts (interpreted Java). This guide was very kindly written by Laura Seabrook and was previously posted to the forum. It is now presented here as a permanent reference.


glödlampa.png

You can run Java code samples interactively in the "jAlbum console window" available from the help menu.


This guide is broken down into four sections as follows:


Contents

1. Literals, Variables and Expressions

I'm writing this for people who, like me, aren't that familiar with Java as a programming language, but would like to use some of it to customise their jAlbum skins. I'm no expert -- in fact until I downloaded jAlbum I hadn't even looked at ANY Java coding. This is not a full tutorial because I don't know that much, but hopefully it will help you get started. The following has been culled from various books explaining Java, and from other topics in the forums, and repeats information from the help pages as well.

Scriptlets

The first thing to realise is that you can include Beanshell scripts directly on the htt pages. Beanshell is just a "shell" under which Java codes can be written and evaluated directly. To write such code, you need to place it like so...

<%
	CODE GOES HERE;
%>

or

<% CODE GOES HERE %> 

...in the location on the page that you want it to be evaluated. Within the section between the "<%" and "%>", you can place Java statements. Basically you put one statement per line, and end it with a ";", like...

userVar1 = 45;
uservar2 = userVar1 + 6;

If you DON'T put the semicolon at the end of a the statement, you'll probably get an error message when you try to generate an album -- the message will vary depending upon what the next statement is. It's also possible to place code in "blocks" delimited by curly brackets, e.g.

{
      LINE ONE OF CODE;
      LINE TWO OF CODE;
      LINE THREE OF CODE;
}

This comes in handy for IF statements (See PART 2 for details) because then a single IF can activate several lines of code. You can create a label by placing a colon after the label's name, e.g.

section_one:
section_two: STATEMENT;

...these can be handy for later documentation, and also breaking out of loops. You can also comment out bits of code in two ways:

// All code on this line is ignored
/* This is a block of comments that get ignored by Java until it is closed. */

The other important scriptlet to know is the EVAL function. Anything written like...

<%= EXPRESSION %>

...will return whatever value is indicated in the EXPRESSION listed. This might be a simple variable, like "userVar1", or something more complex like "filename.lastIndexOf('.')". This is important because if you're going to create and manipulate variables in a script, you need a way of putting the results into HTML codes.

Literals

The first thing to realise is that you can include Beanshell scripts directly on the htt pages. Beanshell is just a "shell" under which Java codes can be written and evaluated directly. To write such code, you need to place it like so...

<%
	CODE GOES HERE;
%>

or

<% CODE GOES HERE %> 

...in the location on the page that you want it to be evaluated. Within the section between the "<%" and "%>", you can place Java statements. Basically you put one statement per line, and end it with a ";", like...

userVar1 = 45;
uservar2 = userVar1 + 6;

If you DON'T put the semicolon at the end of a the statement, you'll probably get an error message when you try to generate an album -- the message will vary depending upon what the next statement is. It's also possible to place code in "blocks" delimited by curly brackets, e.g.

{
      LINE ONE OF CODE;
      LINE TWO OF CODE;
      LINE THREE OF CODE;
}

This comes in handy for IF statements (See PART 2 for details) because then a single IF can activate several lines of code. You can create a label by placing a colon after the label's name, e.g.

section_one:
section_two: STATEMENT;

...these can be handy for later documentation, and also breaking out of loops. You can also comment out bits of code in two ways:

// All code on this line is ignored
/* This is a block of comments that get ignored by Java until it is closed. */

The other important scriptlet to know is the EVAL function. Anything written like...

<%= EXPRESSION %>

...will return whatever value is indicated in the EXPRESSION listed. This might be a simple variable, like "userVar1", or something more complex like "filename.lastIndexOf('.')". This is important because if you're going to create and manipulate variables in a script, you need a way of putting the results into HTML codes.

Variables

Expressions

2. Decision Structures

Boolean conditions

if

switch and case

while

do while

for

break

3. Methods

Boolean

Numeric

Character

String

4. Interfacing with jAlbum

Original code

Example one - Tables

Example two - Filters