public void itemStateChanged(ItemEvent e){if(e.getItemSelectable()==checkbox1){text.setText("Hello from Swing");}else if(e.getItemSelectable()==checkbox2){text.setText("Hello from Swing");}else if(e.getItemSelectable()==checkbox3){text.setText("Hello from Swing");}else if(e.getItemSelectable()==checkbox4){text.setText("Hello from Swing");}

Swaing button let you do a lot more than AWT buttons. Some of the things you can do with the JButton class include using setToolTipText to add a tooltip to the button, using setMargin to set the inserts in the button itself, using doClick to click the button from code, adding images to the button, and adding mnemonics (Keyboard shortcuts) to the button. Of course, you can do standard AWT things with JButton too, such as using setEnabled to enable or disable the button, using addActionListener to register an action listener with the button and adding action commands to JButton objects with setActionCommand.

removeNotify() Overrides JComponent.removeNotify to check if this button is currently set as the default button on the RootPane, and if so, sets the RootPane's default button to null to ensure the RootPane doesn't hold onto an invalid button reference.

void

setDefaultCapable(boolean defaultCapable) Sets the defaultCapable property, which determines whether this button can be made the default button for its root pane.

void

updateUI() Resets the UI property to a value from the current look and feel.

It is easy to display images in buttons using the ImageIcon class, because several JButton constructors let you add icons to buttons. You can also set the alignment of the text and image using these AbstractButton methods.

Besides making buttons into default buttons, you can also give each button a mnemonic, which is a keyboard shortcut, much like those you see in menus. You underline one (case-insensitive) letter in a button's caption, and when the button has the focus, typing that character activates the button.

isValidateRoot() Calls to revalidate that come from within the textfield itself will be handled by validating the textfield, unless the textfield is contained within a JViewport, in which case this returns false.

getDisabledIcon() Returns the value of the disabledIcon property if it's been set, If it hasn't been set and the value of the icon property is an ImageIcon, we compute a "grayed out" version of the icon and update the disabledIcon property with that.

Suppose you want to create an applet that handles button clicks. To create a standard applet, you can derive a class from the java.applet.Applet class, and to handle button clicks, you use another class, ActionListener. Therefore, it looks as through you'll have to base your applet on the Applet and ActionListener classes.

In this case, that means you can extend your applet from the Applet class and use the implements keyword to add the button-click handling.

Java now lets you creates within classes, and the enclosed class is called an inner class. You might not see much need for defining classes within classes now, but it will become more apparent when we start handling user interface events, such as when the user closes a window.

Events are handled with interfaces and when you implement an interface, you must also provide implementations of several abstract methods inside the interface. To make this process easier, java provides adapter classes (Inner Class)

Class: A class is a template from which you can create objects. The definition of the class includes the formal specifications for the class and any data and methods in it. Example:

Object: An object is an instance of a class, much as a variable is an instance of a data type. You can think of a class as the type of an object and you can think of the object as an instance of a class. Objects encapsulate methods and variables. Example:

Data member: it is those variable that are part of a class, and they're how you store the data the object uses.

Method: it is a function built into a class or object. Example:

Inheritance: It is the of driving one class, called the drive class, from another, called the base class, and bring able to make use of the base class's methods in the derive class.

Example: There are two Java files one is cal.java and another is appClass.java

cal.java file contains -

public class cal //Here cal is a Class{ public int c=2; // Here c is a Variable or Data Member declared in class cal public int calculate(int a, int b){ // Here calculate is a Method declared in class cal return a+b; }}

appClass file contains -

public class appClass //Here appClass is the main class because it contains main(String[] args{ public static void main(String[] args) { int y; cal cal1= new cal(); //Here cal1 is a Object of class cal y=cal1.calculate(7,cal1.c); //Here cal1 takes the functionality of the method (calculate) and value of the variable (c) of class cal System.out.println(y); }}

=> Take a moment to study this example; note that I'm declaring and defining two classes, printer and app, in the same file here. Only one class can be public in one file, and that's app in this case. The file, itself, must be named after that class, which here means the containing file must be app.java.

Ex. We can also divide this example into two files, one for each class. Here's printer.java:

=> The possible value for access are public, private and protected. When you declare a class member public, it's accessible from anywhere in your program. If you declare it private, it's accessible only in the class it's member of. If you declare it protected, it's available to the current class, other classes in the same package. If you don't use an access specifier, the default access is that the class member is visible to the class it's a member of, to classes derived from that class in the same package, and to other classes in the same package.

Ex. if we wanted to make the instance variable data_string private to the Data class created in the previous topic, we can declare it private,

If you need to perform some calculation to initialize static variables, you can do so in a static code block, which you label with the static keyword; that code is executed joust once, when the class is first loaded.

We've been using methods even since printing out our first message with System.out.println, so we're certainly familiar with the concept. A method is a code block that you can transfer control to and so execute that code.

Each time we calla method in java, java allocates new space on its internal stack for all the variables in the method, which means there's no reason you can't call the same method again- a new set of variables will be allocated on the stack automatically. What's more, a method can call itself in java - this is a technique called recursion.

Ex. To calculate the factorial the factorial of positive integer n, called "n!", you calculate the following:

n! = n * (n-1) * (n-2) . . . * 2 * 1

This process lends itself to recursion easily, because each stage of the recursion can calculate one multiplication in which it multiples the number it has been passed by the factorial of the number minus 1. When the number has finally been reduced to 1 through successive calls, the method simply returns and control comes back through the successive stages, performing one multiplication at each stage until all nested calls have returned and you have the factorial.

In some languages, such as C++, you use the new operator to allocate new memory and then you use the delete operator to get rid of it when you don't need it anymore. However, Java does not have a delete operator.

In Java, you have to rely on a built-in process called garbage collection. This process occurs automatically, although you can't predict when it will happen. Java, itself, will dispose of allocated memory that on longer has any references to it. To make garbage collection happen, you can set any references to an item to null.

Ex. In this case, class a has an internal reference to an object of class b, and class b has an internal reference to an object of class a. When the code in main sets the reference it has to one of these objects to null, these objects will continue to sit in memory until the program ends.

The garbage collector will call a method named finalize in the object, if it exist. In the method, you can execute cleanup code and it's often a good idea to get rid of any reference to other objects that the current object has in order to eliminate the possibility of circular references.

Overloading constructors works like overloading other methods. You just define the constructor a number of times, each time with a parameter list with parameters that differ from the other lists in some way.

When you pass an item of a simple data type to a method, Java passes a copy of the data in the item, which is called passing by value. Because the method only gets a copy of the data item, the code in the method can't affect the original data item at all.

However, when you pass an object to a method, Java actually passes a reference to the object, which is called passing by reference. Passing by reference means that the code in the method can reach the original object. In fact, any changes made to the passed object affect the original object.

Ex. We pass an object of class Data to the print method of the printer class in order to print out the data in the object.

Ex. We pass an object of the Data class to a method named rewrite that changes the data_string instance variable in the object. This variable starts out with the string "Hello from Java!" in it, but the rewrite method is able to change the string to "Hello to Java!" in this code.