"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."
Call +91 74 88 34 7779 | Email : email@example.com
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 220.127.116.11
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:
There are many differences between StringBuffer and StringBuilder. A list of differences between StringBuffer and StringBuilder are given below:
|1)||StringBuffer is synchronized i.e. thread safe. It means two threads can't call the methods of StringBuffer simultaneously.||StringBuilder is non-synchronized i.e. not thread safe. It means two threads can call the methods of StringBuilder simultaneously.|
|2)||StringBuffer is less efficient than StringBuilder.||StringBuilder is more efficient than StringBuffer.|
Let's see the code to check the performance of StringBuffer and StringBuilder classes.
There are many differences between String and StringBuffer. A list of differences between String and StringBuffer are given below:
|1)||String class is immutable.||StringBuffer class is mutable.|
|2)||String is slow and consumes more memory when you concat too many strings because every time it creates new instance.||StringBuffer is fast and consumes less memory when you cancat strings.|
|3)||String class overrides the equals() method of Object class. So you can compare the contents of two strings by equals() method.||StringBuffer class doesn't override the equals() method of Object class.|
As you can see in the program given below, String returns new hashcode value when you concat string but StringBuffer returns same.
Java StringBuilder class is used to create mutable (modifiable) string. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5.
|StringBuilder()||creates an empty string Builder with the initial capacity of 16.|
|StringBuilder(String str)||creates a string Builder with the specified string.|
|StringBuilder(int length)||creates an empty string Builder with the specified capacity as length.|
|public StringBuilder append(String s)||is used to append the specified string with this string. The append() method is overloaded like append(char), append(boolean), append(int), append(float), append(double) etc.|
|public StringBuilder insert(int offset, String s)||is used to insert the specified string with this string at the specified position. The insert() method is overloaded like insert(int, char), insert(int, boolean), insert(int, int), insert(int, float), insert(int, double) etc.|
|public StringBuilder replace(int startIndex, int endIndex, String str)||is used to replace the string from specified startIndex and endIndex.|
|public StringBuilder delete(int startIndex, int endIndex)||is used to delete the string from specified startIndex and endIndex.|
|public StringBuilder reverse()||is used to reverse the string.|
|public int capacity()||is used to return the current capacity.|
|public void ensureCapacity(int minimumCapacity)||is used to ensure the capacity at least equal to the given minimum.|
|public char charAt(int index)||is used to return the character at the specified position.|
|public int length()||is used to return the length of the string i.e. total number of characters.|
|public String substring(int beginIndex)||is used to return the substring from the specified beginIndex.|
|public String substring(int beginIndex, int endIndex)||is used to return the substring from the specified beginIndex and endIndex.|
Let's see the examples of different methods of StringBuilder class.
The StringBuilder append() method concatenates the given argument with this string.
The StringBuilder insert() method inserts the given string with this string at the given position.
The StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex.
The delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex.
The reverse() method of StringBuilder class reverses the current string.
The capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.
The ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34.