Last
revision of this document: |
This
step of the tutorial deals how to handle when a button is clicked (an
action event occurs) and to code a method, that allows the selection
of a file (the one containing the SQL-commands that should be
executed against the database in a further step).
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
tutorial for dealing with event in JAVA can be found under
http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html
.
JS_Base02, Step 1 – GUI-Design completed – and its prerequisites too.
Basic knowledge of Java programming language; particular the usage of libraries.
Additional
code to handle 'Clicks' onto buttons:
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 italics.
Larger blocks of old code may be
skipped.
To
react to button-clicks, the ActionListener interface must be
implemented.
package
js_base02.application;
import
javax.awt.*;
import
import
javax.awt.event.*;
javax.swing.*;
public
class JS_DBLoad
extends
JFrame
{
implements
ActionListener
This
interface requests to implement a method called
actionPerformed
.
public
static void main(String[] args)
{
try
{
JS_DBLoad
aJS_DBLoad = new
JS_DBLoad();
aJS_DBLoad.setVisible(true
);
}
catch
(Throwable
exc) {
System.out.println(„Exception
occured in main() of
JS_DBLoad“);
exc.printStackTrace(System.out);
}
}
}
private
void
actionPerformed(ActionEvent
e) {
}
That
a click onto a button calls
actionPerformed
the ActionListener must be registered at the buttons.
This is
shown for the button 'Select Input File' with all the existing
code.
private
JButton get_btn_Select {
/* The code
of this method auto-creates the element if it is not already defined
*/
if
(btn_Select
== null) {
try
{
btn_Select
= new
JButton(); btn_Select.setName(
}“
btn_Select“
); btn_Select.setToolTipText(
“
Select
Input-File containing
SQL-commands“
); btn_Select.setMargin(new
Insets(3,3,3,3));
btn_Select.addActionListener( btn_Select.setVerticalTextPosition(SwingConstants.
CENTER
); btn_Select.setHorizontalTextPosition(SwingConstants.
CENTER
); btn_Select.setText(
“
Select
Input-File“
);this
); btn_Select.setActionCommand(
“btn_Select“
); catch
(Throwable Exc) {
}
System.out.println(“
Error
while building
btn_Select“
);
Exc.printStackTrace();
}
return
btn_Select;
}
To
see a result of the code just implemented, again an extraordinary
piece of code is added:
private
void
actionPerformed(ActionEvent
e) {
get_txt_Report().append(„Button
clicked: „ + e.getActionCommand() + „\n“);
}
Please
remind to save the code just typed.
Now
it is time to run the code already implemented and see the
result.
Run the application again by selecting
>Run>Run
As>Java Application.
After clicking several time onto the button 'Select Input File',
the application should look like this:
If
you click onto the other button, nothing happens. This is due to the
missing
addActionListener
at the other buttons.
So 4 more lines of code have to be
added:
btn
}_
.setText(Start
“Start
SQL
execution“
); btn_
Start
.addActionListener(this
);
btn_Start
.setActionCommand(“btn_
);Start
“ catch
(Throwable Exc) {
btn
}_Print
.setText(“Print
the Report“
); btn_
Print
.addActionListener(this
);
btn_
Print
.setActionCommand(“btn_
);Print
“ catch
(Throwable Exc) {
Again,
a little check if the code was implemented as planned.
Remind to
save !
Run the application again by selecting
>Run>Run
As>Java Application.
After clicking several time onto the buttons, the application
should look like this:
Additional
class for 'actions':
To
avoid overloading of the class
JS_DBLoad
with the elements with the GUI-elements, a class to hold the code for
all actions is created.
Right
click onto the package 'js_base02.application' and select
>New>Class
Check
that the Package (js_base02.application)
was filled and enter the (Class-)Name
(JS_DBLoad__Action_Handler).
Make
sure that none of the check-fields for 'Which method stubs would
you like to create ?' is checked and click the [Finish]
button.
Eclipse
has already generated a template and the individual code can be
entered.
Please see the comment for a description of the
code.package
js_base02.application;
import
java.awt.*;
import
java.awt.event.*;
/**
*
* @author kurt@javascout.biz
Tutorial how to
*
@date 2006-02-10
*
* @description
*
* - create a GUI using javax.swing components
*
- load parameters for DB-connection from a XML-file
*
* -
connect to a database
* - read SQL-commands from a
text-file and perform them
*
@change-log
* when who why
*
--------------------------------------------------------
*
*/public
class JS_DBLoad__Action_Handler
{protected
static void handleEvent(JS_DBLoad parmCallingFrame,
ActionEvent parmActionEvent){
/*
String
cmd = parmActionEvent.getActionCommand().trim();
*
Extract the action-command (in form of a String) that triggered the
Event from the Event */ /*
*
Compare the String with the action-command and call the adjacent
method */
if
(cmd.equals(„btn_Select“))
processSelectFile(parmCallingFrame);
}protected
static void
processSelectFile(
{JS_DBLoad
parmCallingFrame)/*
FileDialog
fd =
*
Create a File-Dialog to let the user choose the Input-file */
new
FileDialog(parmCallingFrame,
„Select
input-file containing SQL-commands“,
FileDialog.LOAD); fd.setFile(„“);
fd.setDirectory(„“);
fd.setVisible(true
);/*
String
strDirectoryAndFileName = fd.getDirectory + fd.getFile();
*
Extract the directory and file-name chosen and put it into a String
*/ /*
parmCallingFrame.get_txt_InputFile().setText(strDirectoryAndFileName);
*
Store the chosen directory and file-name in the TextField */
}}
As
a result you will get one planned error - telling you 'The method
get_txt_InputFile() from the type JS_DBLoad is not visible'.
This
is due to the definition of the method as 'private'.
To allow a
call of the method within a package, the method must be defined as
protected
.
At
the same time, the field will be set as not 'Editable'. This forces
the user to choose the input-file only via the
file-dialog.
protected
JTextField get_txt_InputFile
{
/* The code of this method auto-creates
the element if it is not already defined */
if
(txt_InputFile ==
null) {
try
{
txt_InputFile
= new
JTextField();
}txt_InputFile
.setName(“
);txt_InputFile
“
txt_InputFile
.setToolTipText(“Selected
Input-File containing
SQL-commands“
);
txt_InputFile
.setMargin(new
Insets(3,3,3,3));txt_InputFile
.setEditable(false); catch
(Throwable Exc) {
To
see what the additional code is doing - agin a check.
Remind to
save - both classes !
In the editor select the tab with
'JS_DBLoad.java' - otherwise you will not be able to run a 'Java
Application' as no main
method will be found.
Run the application again
by selecting
>Run>Run
As>Java Application.
After clicking the [ Select Input File ]-button, the
file-dialog opens.
As
there is no input-file (those with the SQL-commands) created, I took
one of the Java-classes.and after clicking the [ OK ]-button,
the directory and the file-name is transfered to the text-field
(top, center).
I resized the window to see more text.
Creating
directory and files for input (SQL-commands) and database-parameters:
As
this step did not become so large, the creation of the files for
input (SQL-commands) and parameters for the database-connection are
covered now.
The
files are stored in a folder 'input' underneath the folder 'src'.
To
create this folder, expand the project 'JS_Base02' (if not already
expanded - click onto the little triangle left of the folder name).
Right click onto the folder 'src' and select >New>Folder
On
the window 'New Folder' enter the 'Folder name':
Input
and
click the button [ Finish ].
Finally,
two files have to be created within the folder 'Input'.
To
create a (empty) file, right click onto the folder 'Input' and
select >New>File
On
the window 'New File' enter the 'File name':
Commands.txt
and
click the button [ Finish ].
Repeat
the 2 previous steps and create a file named
DBParms.xml
.
Eclipse
has opened the 2 files in the editor. As the content is entered
later, close the 2 files now.
Next
Step:
The
file '
DBParms.xml'
(containing the parameters to connect to the database) is
read.
JS_Base02d
- Read parameters for the connection to the database.