hello again and welcome back my name is Michael Futch
its lesson 18 in our learn to program in Java series
in this lesson will cover gooey programming
on the agenda for today
we'll talk about the Java Swing toolkit the common UI widgets
you can use with the toolkit and what their purpose is might be
and then I want you to note that everything that you do
when you type in politics eta is a string and so we have to revisit
converting strings to other types like integers and doubles and bullion values
first of all I know many of you are familiar with the graphical user
interface or do we
and up until this point we haven't been doing any Java programming in a girly
you might be asking yourself what why here we started to use the gooey from
the beginning
and really the answer to that is in order to learn to program
you don't need to use a girly goalies are things that you can pick up
afterwards
another important thing to note is that the nature
and behaviors are the various do we talk it's that are available
change over time some go out of fashion some new ones come in fashion
so the better thing to do is to learn how to be a good problem solver and
learn how to program
and then you be able to pick up just about any girly toolkit that becomes
invoke when you learn how to program
in this course will be using the Java Swing do we
this is the standard Java GUI toolkit right now the used to be other
Java toolkit to ease in earlier editions like a WT
we will be using those will be using swing the way swing works is the code
executes in response to a user that
like clicking a button or typing tax so in other words is gonna be a main event
loop
and it's going to just kinda wait until you do something and then in response to
doing something will execute code
this isn't like what we've written before where the code executes when you
hit the Run button they're still code being executed I want to emphasize
it's just the code that you end up writing is usually going to be in
response to an event
in the user interface 48 for instance
clicking about let's first talk about some other common classes are available
to you
through the swing toolkit J frame
is the most common class this one makes the window
J label displays text J button is for a button
J text field is for a box for you can enter text
J checkbox makes a checkbox for
Yes No J combo box is a dropdownlist
and then there's common methods you need get taxed to read
the value of the control set tax to set the value of the control
and is selected is a method you can call to determine whether
or not control was selected layout
this is useful for J check boxes or J combo boxes for example
okay I'd like to start
by running a demo just so you can see with the guy who is like
our talk over the various swing controls I have and then after I run the demo
I'll show you how I built the devil because building a swing project is much
different than
out some the other things we've done to date okay here I am in my NetBeans iDEN
I've opened up this example called emo swing controls that I wrote
this is our available with the lesson on get hob
you see I have one java file called emo swing controls J frame
what you'll notice about this java file and open it up as you don't see any cold
what you see instead of seeing a code is
a cue big reform and on my form I have
objects up certain classes that you can see
I've clicked on this box here that AJ
check box called eat at Joe's J checkbox
he did joes and
it's got various properties on it like collars and what are the tax
the text properties as he did joes I have a
this down here is AJ text field
and its Jay text field message is the name of the object the class is Jay text
field
and the text is blank I could have the tax a monkey if I what
it'll display monkey in there
what's different about what we've done so far is we now have these
graphical widgets that we include on a on a page
and then bill that in turn their those jet those graphical widgets generate
code for us
if I click to the source tad you'll see something that looks a little more
familiar scene like
package in my class in a public method
this public method here's a constructor in the constructor calls a net
components
and then down here are all the private members of the objects that I have on my
do we form and then here's a buncha plumbing code that
is it was created and what's interesting is this code that I wrote J but no
action perform so when you click
the the job 1 I'm gonna do are this stuff here
you can see everything is still here the same way it was before
is just as a little bit of extra stuff that was created for me
and this is this is pretty much a good thing because want the nice things if
about building a graphical user interfaces you won't be able to use
tooling like this
to make sure that you place your objects in the right place and
you can see kinda what's gonna happen when you run
run your program before you run your program so if you look at this layout
that I have year when actually run my program appear
the layout that I get on at runtime is
is very similar to the one that I get at design-time in this again is a good
thing
that when you're building your user interface up here
in NetBeans you don't have to constantly run it to make sure that you're doing it
correctly
now this is a very simple demo here that just kinda
iterates how you can get properties
are each control so I'm just gonna checkup
a budget things and do a bunch of things in here you know pick a combo box value
type in my name on it clicked me and that's gonna read the values other the
controls
see that service which sees around right
the values will change in this kinda gives me a sense of how I can use these
controls
in a program like a check box is great for yes no values and radio buttons are
good for
letting someone pick amongst a choice same thing with the combo boxes with a
more compact
text boxes for freeform taxed and then click me is my event
handler and if I were to look at the code disassociated
with the click me event handler you'll see that I'm just doing a bunch of
property setting it says are you know for example the text box
value a call to set text method
and I set it to I set this label
I said it to
what I get from the text field as the value
with get text
so probably the best way did explain I how this is
is working is for me too you know what I'm gonna do at this point put a
breakpoint here
debug it
okay see the cold ran but it has not hit my break point yet
break points that can be hit until I intel i click this button here because
that is what this
method is associated with his event of clicking a button
so let me just said a couple values here first
not hit the button them all fall into this area here
me see if I can do both line up at the same time now to sleep here
okay so this line says I wanna set the tax stuff
label message value which is right here to label message which is this
get the text song when hit f8 and then I'm gonna set
the taxed
the check box
eccentric cetera and then I'm gonna do the radio buttons
and then when the event handlers done the form gets repainted
and I see the values
see what see those values in real time is your debugging
but you can at least inspect the objects values for example only click it again
and so if I put a watch on this I put a watch on this one here
you see that that is Mike see that right there
and if I go back and look at my form that's what I typed
Mike
so you can see that you have to use the Set tax and get text methods
to read and write to and from
these user interface controls that's really the big secret
okay next we're gonna get real world and we're gonna make
a program from scratch that ass us for the length and with
the rectangle and then we'll calculate that rectangles area and perimeter
we have written this program before what we didn't do is write it swings style
so we will write it now as a graphical user interface program
what you wanna notice here is that
are I everything is a string so even though I type
in a length of 15 .5 that gets input as a string and is up to me
to convert that value into a double so that I can perform math
these are kind of the things that will people see as we go through the devil
let's get into it
okay I am back in NetBeans I think the first thing I want to do is take you
through how to build one of these types of Cooley projects from scratch
so I'm gonna do something we haven't done in a while I'm gonna do a new
project
usually I skip this part because I figure you know how to do it
pic job application here and I'm going to call this
rectangle redox
and this is not unlike anything we've done before and you see I have this
rectangle redox Java when it but i wanna do instead
is a one-hour make a new J frame form
and Mayor and I'm gonna call this wrecked tangle
redox J frame
and I don't need this other one so I can delete that
and now when I execute my program it asked me what what is going to be your
startup project and there's only one class left
so you want to select that class and now I get
my blank SLE where I can make my awesome
good week so let's start by setting some properties at that get us
close to what we want to do here right for some gonna set the title
let's call it area and parameter
I love rectangle
okay well I've done quite a bit have set up already I haven't completed all the
setup but I've done enough so that you don't have to watch and be bored
in the process when it done as i've labeled all my labels
right now just set the text properties I V to the labels
and then I've taken and are every time i text boxes here
I just given a value of 0.0 to start
and I what you started naming somebody is like for example this text box years
college a text field length this one here is called Jamie text field with
rather than keeping them you know J text field 1j text field to Jay text field
three
I like to give them names that make sense so that encoding I know which 1
I'm referring to
I label this button J button calculate
and I needed label these last two use a little evil this one
G text field
area
and this one J text field parameter
lookin good
now I need to do is write the code that actually
you know takes the values in here and then when I click this calculate button
we'll figure out the area forever so I have to tie code
to this but by do that by double clicking on the button it will take me
in
to a method that it will generate for me that
is associated with the G button calculate action performed method which
means
the button carries out its action that is you click on it and it does what it
intends
so let's set this up I should
use variables here should have double area
perimeter length
and with
when the four variables now the the general rule of thumb here
pattern I'm gonna followers I need to take the value
are the text box and i need to put it in a variable then you to calculate my area
and perimeter
and that's to come back on the text boxes the problem that we face is that
the text box but I type in here is extreme
it is not a double yeah I might type a double in there
but it still gets treated as a string so I have to rate the cold that will
convert
the string into the double that's all on me
so you probably don't remember how to do this cuz it's been a while so I'll walk
you through it again
but I'm gonna do something like this I'm gonna say length
is this start
J text field length dot
get text
that gets the links of I type in elective fourteen-point 0
this method will get that like
promises a string and I need a double so I need to use
double dot parse
double which takes a string is an argument
and then returns the double equivalent
up that string so again doubled up personal
takes your string converts to a double taking the double converting it into the
to the string is actually quite simple as we'll see and you know how to do as
well
so need to repeat this for with this is one other
kinda the disadvantages UHV using a gooey is that everything is treated as a
string and it's up to you to kinda plummet
out of a string into the native type that you need
this is a royal pain in the butt and what are the things that
people do not like a bow arm languages that use data types
this would be J
text field with
right
up for it with
area is length times with
parameter is
to times length plus
with
a call at pearl
now how do I take area and perimeter and get them back
onto the text form back into the text boxes on the form I should say
you say something like this: dot area
now in use the Set text method
and you'll see that sex
set taxed
takes
one argument of type string so I need to put a string in here so there's a couple
ways I could do this I could just say
I can to see area because area is a double and it's looking for string so I
have to
convert this to a string so you would use the string
string dot format
and if you don't remember
string dot format is the same missing system that out
to print def same kind of syntax
so string format
format it as a double what do you wanna format
area and I will rinse and repeat this pattern
for perimeter using have coursed J text field
perimeter
and then using perm here
and their is it that is all my cold
so let's give it a run and see what happens
right enter a length for
and her with
six-point to
Capulet
nope looks like I don't have enough room see
20.40 scissors are area is
24.80 scissors are
so it looks like I did leave myself enough are related to make these text
boxes on a wider
that's one way to to tackle this problem another way would be to
change by string dot format to use a fewer number of decimal places I could
do that as well
could probably do both just so that it doesn't look
so wacky so I'm gonna say music
point 2f and then point 2f
sole use two decimal places that should be sufficient let's give it a try
Anderlecht 2.5 her with for
calculate 10 and 30
ago
that two things kinda bugged me about this program one is
I would I don't like the fact that when I run the program
initially I have to
at it the zeros see that I
and I gonna take those output in a six or something I should just you know
start with blanks
and I should also the button that lets me reset
everything back to the way it was so
let's let's right that now
since I what when the form first loads
it to clear the values and then I wanna have a button on here
that says clear values right now take this calculate move it over
and then add another button
I'm gonna call this button J button Scott
clear values
and I want to name this J button clear values
and I'll get to what I'm talking about the second if I were to just double
click on this
and write code here clear values action performed then
if I read the code here it will clear the values out
but data how do I get that to happen at startup money me to show you what I mean
so I let's pause I say this J
area that set text
string doubt format
percent
point to you
at
and I wanted to be 0
so when you could clear but I want the area to be 0 I want the perimeter be 0
I want
the area not the area I'm sorry
I want the length on a set that
taxed
to empty that way you don't have to type anything
and i wanna do the same for with
sorts on that now
so that we're clear values
hope to screw something up it looks like a threw an exception down here
I did okay I'll string.format
point2 F and putting and South double
a double
try that again
okay so see what I had clear values and then I can put in a
no for and three calculate
yes clear values trip back to it was okay
when I need to do now is I want when it opens up for the first time it to run
the clear values method K
so if you look appear this is the constructor
and the constructor has our
when it first this is what happens when that when the program first
runs it runs a constructor so I could take all this cold here in just copy and
paste it in the constructor but what I'd rather do was make us
a private method just for this on a cut this out and I'm gonna make a private
method
called clear form
some reading my own method and I my clear for method is going to do this
because that I want my form cleared and then when you click this button
I'm gonna call clear for I guess for wanna be consistent with all the program
done today to should do this clear for
and of course whatever up here in the constructor after I initialize all
components I wanna call this
clear for
and now when I run it it is clear the form what it's for starts
and it did and then I could put in a three and a five-point
5 calculate awesome their values
well that concludes our lesson on
gooey programming with the swing toolkit hope you learned a little bit more about
how it all works in
you feel more comfortable with it it's not that bad actually
will continue to write more girly programs in the last two lessons
in the meantime I welcome your questions and comments
on this particular lesson thank you very much and we'll see again real soon
by now