5. JavaProgramming
5. JavaProgramming
Controlling
Java
Program
Exception
Flow
Modifiers
and Access
Specifiers
Java Programming
Inner Classes
Inner Classes
In order to create an object of the inner class you need to use an object of the
outer class.
The following line of code could have been written inside the method of the
enclosing class:
The following line of code is used to create an object of the inner class outside of
the enclosing class:
If you don’t need a connection between them, then you can make the inner class static.
A static inner class means:
You don’t need an outer-class object in order to create an object of a static inner class.
You can’t access an outer-class object from an object of a static inner class.
2. Static Inner Class
Static Inner Class:
When you create an object of static inner class, you don’t need to use an object of the
outer class (remember: it’s static!).
Since it is static, such inner class will only be able to access the static members of the
outer class.
2. Static Inner Class (Example)
public class OuterClass{
int x ;
static int y;
The object of the local inner class can only be created below the
definition of the local inner class (within the same method).
The local inner class can access the member variables of the outer class.
It can also access the local variables of the enclosing method if they are
declared final.
4. Anonymous Inner Class
public class MyClass
{
int x ;
public void init()
{
Thread th = new Thread(new Runnable()
{
public void run()
{
..
}
});
th.start();
}
}
4. Anonymous Inner Class
The whole point of using an anonymous inner class is to implement an
interface or extend a class and then override one or more methods.
When you compile the java file, two class files will be produced:
MyClass.class
MyClass$1.class
Example:
Java Programming
Event Handling
Event Handling
void addActionListener(ActionListener l)
void addActionListener(ActionListener l)
componentHidden (ComponentEvent e)
componentShown (ComponentEvent e)
ComponentEvent ComponentListener
componentMoved (ComponentEvent e)
componentResized (ComponentEvent e)
componentAdded (ComponentEvent e)
ContainerEvent ContainerListener
componentRemoved (ComponentEvent e)
Events Classes and Listener Interfaces
Listener
Event Method(s)
Interface(s)
focusGained (FocusEvent e)
FocusEvent FocusListener
focusLost (FocusEvent e)
windowClosed (WindowEvent e)
windowClosing (WindowEvent e)
windowOpened (WindowEvent e)
WindowEvent WindowListener windowActivated (WindowEvent e)
windowDeactivated (WindowEvent e)
windowIconified (WindowEvent e)
windowDeiconfied (WindowEvent e)
Events Classes and Listener Interfaces
Event Listener Interface(s) Method(s)
keyPressed (KeyEvent e)
KeyEvent KeyListener keyReleased (KeyEvent e)
keyTyped (KeyEvent e)
mousePressed (MouseEvent e)
mouseReleased (MouseEvent e)
MouseListener mouseClicked (MouseEvent e)
mouseEntered (MouseEvent e)
MouseEvent mouseExited (MouseEvent e)
mouseMoved (MouseEvent e)
MouseMotionListener
mouseDragged (MouseEvent e)
For All of your labs don’t forget to use: setFocusable(true) in the constructor of your
JPanel
Lab Exercise 2
• Create a frame that displays string which user can move
it using arrow keys.
Lab Exercise 3
• Create a frame that allows the user to draw one line by
dragging the mouse on the frame.
Lab Exercise 4
• Modify the previous exercise to allow the user to draw multiple lines on the
frame.
• Store the drawn lines in an array to be able to redraw them again when the panel
is repainted.
Course Outline
Controlling
Java
Program
Exception
Flow
Modifiers
and Access
Specifiers