"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."
Call +91 74 88 34 7779 | Email : firstname.lastname@example.org
The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.
Let's see the declaration for javax.swing.JButton class.
|JButton()||It creates a button with no text and icon.|
|JButton(String s)||It creates a button with the specified text.|
|JButton(Icon i)||It creates a button with the specified icon object.|
|void setText(String s)||It is used to set specified text on button|
|String getText()||It is used to return the text of the button.|
|void setEnabled(boolean b)||It is used to enable or disable the button.|
|void setIcon(Icon b)||It is used to set the specified Icon on the button.|
|Icon getIcon()||It is used to get the Icon of the button.|
|void setMnemonic(int a)||It is used to set the mnemonic on the button.|
|void addActionListener(ActionListener a)||It is used to add the action listener to this object.|
Java Swing tutorial is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight components.
The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
There are many differences between java awt and swing that are given below.
|No.||Java AWT||Java Swing|
|1)||AWT components are platform-dependent.||Java swing components are platform-independent.|
|2)||AWT components are heavyweight.||Swing components are lightweight.|
|3)||AWT doesn't support pluggable look and feel.||Swing supports pluggable look and feel.|
|4)||AWT provides less components than Swing.||Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.|
|5)||AWT doesn't follows MVC(Model View Controller) where model represents data, view represents presentation and controller acts as an interface between model and view.||Swing follows MVC.|
The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.
The hierarchy of java swing API is given below.
The methods of Component class are widely used in java swing that are given below.
|public void add(Component c)||add a component on another component.|
|public void setSize(int width,int height)||sets size of the component.|
|public void setLayout(LayoutManager m)||sets the layout manager for the component.|
|public void setVisible(boolean b)||sets the visibility of the component. It is by default false.|
There are two ways to create a frame:
We can write the code of swing inside the main(), constructor or any other method.
Let's see a simple swing example where we are creating one button and adding it on the JFrame object inside the main() method.
We can also write all the codes of creating JFrame, JButton and method call inside the java constructor.
The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that sets the position of the button.
We can also inherit the JFrame class, so there is no need to create the instance of JFrame class explicitly.
The object of Label class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly.
IP Of www.toshiyas.in is 184.108.40.206
Button : Find IP
The button class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed.
welcome to Toshiyas IT
|Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. The java.awt.event package provides many event classes and Listener interfaces for event handling.|
|Event Classes||Listener Interfaces|
|MouseEvent||MouseListener and MouseMotionListener|
Following steps are required to perform event handling:
For registering the component with the Listener, many classes provide the registration methods. For example:
We can put the event handling code into one of the following places:
public void setBounds(int xaxis, int yaxis, int width, int height); have been used in the above example that sets the position of the component it may be button, textfield etc.
Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java.
Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavyweight i.e. its components are using the resources of OS.
The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
The hierarchy of Java AWT classes are given below.
The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel.
The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window.
The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.
The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
|public void add(Component c)||inserts a component on this component.|
|public void setSize(int width,int height)||sets the size (width and height) of the component.|
|public void setLayout(LayoutManager m)||defines the layout manager for the component.|
|public void setVisible(boolean status)||changes the visibility of the component, by default false.|
To create simple awt example, you need a frame. There are two ways to create a frame in AWT.
Let's see a simple example of AWT where we are inheriting Frame class. Here, we are showing Button component on the Frame.
The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example that sets the position of the awt button.
Let's see a simple example of AWT where we are creating instance of Frame class. Here, we are showing Button component on the Frame.
If you want to represent any object as a string, toString() method comes into existence.
The toString() method returns the string representation of the object.
If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
By overriding the toString() method of the Object class, we can return values of the object, so we don't need to write much code.
Let's see the simple code that prints reference.
As you can see in the above example, printing s1 and s2 prints the hashcode values of the objects but I want to print the values of these objects. Since java compiler internally calls toString() method, overriding this method will return the specified values. Let's understand it with the example given below:
Example of Java toString() method
Now let's see the real example of toString() method.
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members as the example given below:
|In this example, we have created a final class named Employee. It have one final datamember, a parameterized constructor and getter method.|
The above class is immutable because: