|
|
|
Last
revision of this document: |
This
step of the tutorial tells how to create a Graphical User Interface
(GUI). It will just contain one button.
In a later step, this
button will be used to create another frame which allows the
maintenance (create, change, delete data) for the error-messages.
Preface:The
code written in this tutorial is far away from being
optimized.
Emphasis of this tutorial is to develop the application
in small steps where the completion of each step allows the
application to be run eror-free and showing the result aimed by the
step.
Therefore the code is written to be understandable in favor
of being optimized.
Credits:A
reference of the elements for the GUI in JAVA can be found under
http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
.
JS_FC01ca
- Create a project for the base-classes
or JS_FC01cb
- Download the jar-file for the base-classes
completed - and its
prerequisites too.
Recommended:
JS_Base02
- DataBase-Loader with a GUI completed.
This
is not absolutely a prerequisite but gives you a more indepth
knowledge about developing an application with a GUI.
Create
and code the Class for the GUI:The
code responsible for the GUI-part is the class we start with.
N.B.:
1.) To separate the code for actions and for handling events
(explanation follows later) two more classes are created later.
2.)
The Visual Editor offers several templates for GUI-Styles. To explain
the creation of each GUI-element this element is developed 'from
scratch' without selecting a template.
Right
click onto the project 'JS_FC01' and select
>New>Other...
In
the following window expand 'Java' (if not alredy done), select
>Visual Class and
click the [ Next > ] button.
Enter
the Package (js_errdb.clientframes),
the (Class-)Name (JS_ErrDB_CommandCenter)
and the
Superclass (js_base.frame.JSBS_StartFrame).
Check [ ]
public static void main(String[] args) and click the [ Finish ]
button.
Eclipse
has already generated a template and the individual code can be
entered.
In
the following explanations all new code is written in bold
letters.
Code generated by the template or entered in previous
steps (old code) is in normal.
Larger blocks of old code may be
skipped in this documentation.
For
a more detailed explanation to develop a GUI please refer to the
tutorial
JS_Base02
- DataBase-Loader with a GUI.
The
entered text can be saved by clicking the right mouse-button and
selecting Save
from
the context menu.
First
the comment is entered - so a stranger knows what this class is
for.package
js_errdb.clientframes;
import
js_base.frame.JSBS_StartFrame;
/***
* @author kurt@javascout.biz
*
@date 2006-05-07
*
* @description
* Command Center for the
Fat-Client-Application to maintain
* the database with the
error-messages*
* @change-log
* when who why
*
--------------------------------------------------------
*
*/public
class JS_ErrDB_CommandCenter
extends
JSBS_StartFrame
{ //*
*
@param args
*/
public
static void main(String[]
args) {
//
TODO Auto genereated method
stub
}
}
As
next step, a constructor-method is implemented.This
'constructor' .is
called from the main-method
and may contains further calls of
methods that have to be performed at the time when the object is
'constructed'
The only method that
is called now is , which is a reserved word in Java and calls the
'constructor' in the inherited object (i.e. JSBS_StartFrame
in this example). public
class JS_ErrDB_CommandCenter
extends
JSBS_StartFrame
{ public
JS_ErrDB_CommandCenter(){
super();
}
//*
*
@param args
*/
public
static void main(String[]
args) { } try
{
JS_ErrDB_CommandCenter
a JS_ErrDB_CommandCenter
= new
JS_ErrDB_CommandCenter();
}
catch(Throwable
exc) {
System.out.println(„Exception
occured in main() of
JS_ErrDB_CommandCenter“);
exc.printStackTrace(System.out);
}
}
Till
now, no element of the GUI is 'constructed' - so nothing would be
shown on the monitor.
There is an unwritten convention for
Java-programming, that the 'constructor' calls one or more
initialize methods.
I
decided, to split the initialization into 3 groups:
*
initialize_before_frame which
performs work to be done before the GUI is displayed, e.g. -
loading text-elements of the selected language from a file,.
-
displaying a small frame with the information that 'application is
loading', -
connecting to database or Java-Application-Server. -
defining colors for foregroung, backgroung and borders,*
initialize_frame which
'constructs' the elements of the GUI.*
initialize_after_frame which
performs work to be done after the GUI is displayed, e.g.
-
getting personalized values for the user (frame-size, function keys)
from the database.
For now, only initialize_frame
is filled with the 'construction' of an empty framepublic
class JS_ErrDB_CommandCenter
extends JSBS_StartFrame { public
JS_ErrDB_CommandCenter(){
();super
initialize_frame;
}
private
void initialize_frame()
{
setVisible(true);
setSize(600,
400);
setTitle(„JavaScout-Tutorial
– Fat-Client-01 – Maintain
Error-Messages“);
}
//*
*
@param args
*/
public
static void main(String[]
args) { }
{try
a JS_ErrDB_CommandCenter
= JS_ErrDB_CommandCenter
new();JS_ErrDB_CommandCenter
}
(Throwable
exc) {catch
System.out.println(„Exception
occured in main() of
JS_ErrDB_CommandCenter“);
exc.printStackTrace(System.out);
}
}
To
check if the code is error-free till now, the program can be
run.
How to run a program in Eclipse, please refer to Run/Test
in JS_Base01 - HelloWorld.The
result should be a frame like this: .
As
you might have realised, the 'Title'-text is fixed in the code.
This
contradicts to the aim, that text-elements should be
language-dependant and loaded from a file in XML-format.
Before
the access to an XML-file is implemented, all other elements of the
GUI are defined now.For
a more detailed description of the elements of a GUI please refer to
the tutorial
JS_Base02
- DataBase-Loader with a GUI./**package
js_errdb.clientframes;
importimport
importjava.awt.*;
javax.swing.*;
js_base.frame.JSBS_StartFrame;
....
....
*/
public class
JS_ErrDB_CommandCenter
extends JSBS_StartFrame {
private
JPanel pnl_Main=null; private
JLabel lbl_Welcome=null; private
JButton btn_Maintain=null;public
JS_ErrDB_CommandCenter(){
super();
initialize_frame();
}
private
JLabel get_lbl_Welcome() {/*
The code of this method auto-creates the element if it is not
already defined */ if
(lbl_Welcome
== null) {
try
{
lbl_Welcome
= new
JLabel(); }lbl_Welcome.setName(“);lbl_Welcome“ lbl_Welcome.setText(“.....“); catch
(Throwable Exc) { }
System.out.println(“Error
while building
);lbl_Welcome“
Exc.printStackTrace();
}
return
lbl_Welcome;
}
private
JButton get_btn_Maintain() {/*
The code of this method auto-creates the element if it is not
already defined */ if
(btn_Maintain
== null) {
try
{
btn_Maintain
= new
JButton(); btn_ }Maintain.setName(“btn_);Maintain“ btn_Maintain.setText(“.....“); catch
(Throwable Exc) { }
System.out.println(“Error
while building
btn_);Maintain“
Exc.printStackTrace();
}
return
btn_Maintain;
}
private
JPanel get_pnl_Main() {/*
The code of this method auto-creates the element if it is not
already defined */
if
(pnl_Main
== null) {
try
{
pnl_Main
= new
JPanel(); }.setName(pnl_Main“);“pnl_Main .setLayout(pnl_Mainnew);
GridBagLayout()/*
* Define GridBagConstraints for the element to be added
*/ GridBagConstraints
gbc_lbl_Welcome = new;
GridBagConstraints() gbc_.lbl_Welcomegridx
= 0; gbc_.lbl_Welcomegridy
= 0; gbc_.lbl_Welcomefill
=
GridBagConstraints.HORIZONTAL; gbc_.lbl_Welcomeweightx
= 1; gbc_.lbl_Welcomeanchor
= GridBagConstraints.CENTER;/*
Add the element to the Panel; element-position is controlled by
GridBagConstraints
*/ get_pnl_Main().add(get_gbc_lbl_Welcome(),
lbl_Welcome);/*
* Define GridBagConstraints for the element to be
added */ GridBagConstraints
gbc_btn_Maintain = new;
GridBagConstraints() gbc_btn_.Maintaingridx
= 0; gbc_btn_.Maintaingridy
= 1; gbc_btn_.Maintainfill
=
GridBagConstraints.HORIZONTAL; gbc_btn_.Maintainweightx
= 1; gbc_btn_.Maintainanchor
= GridBagConstraints.CENTER;/*
Add the element to the Panel; element-position is controlled by
GridBagConstraints
*/ get_pnl_Main().add(get_btn_gbc_btn_Maintain(),
Maintain;) catch
(Throwable Exc) { }
System.out.println(“Error
while building
pnl_Main“);
Exc.printStackTrace();
}
return
pnl_Main;
}
private
void initialize()
{
setVisible(true);
setSize(600,
400); setTitle(„JavaScout-Tutorial
– Fat-Client-01 – Maintain
Error-Messages“); }
setContentPane(get_pnl_Main());
To
see the result of the just typed code, run the application again by
selecting >Run>Run
As>Java Application.
The
application should look like this containing the button.
Pretty
sexy - isn't it ? (Sarkasm !).
As stated before, the
text-elements do not contain descriptive text.
Before language
dependant text can be set at the GUI-elements, a method to load a
file with an XML-structure has to be loaded and the XML-tree has to
be searched.
This is done in the next step.
Next
Steps:Before
the file with the XML-structure can be read, some code is needed to
find out in which directory the application is stored.
This is
needed as the directory with the XML-files is stored relatively to
it.
JS_FC01e
- Universal structure for parameters and getting the directories.
After
that, the structure of the XML-file with the language dependant
text-elements will be introduced and a class to read this file will
be coded.
JS_FC01f
- Code a class to read files with XML-structure.