hereral/Clara-Training-Data huggingface dataset:
Question: Can you give me the Displaying Source Code of a Webpage by URLConnecton Class?
Answer: The URLConnection class provides many methods. We can display all the data of a webpage by using the getInputStream() method. It returns all the data of the specified URL in the stream that can be read and displayed.

Example of Java URLConnection Class

import java.io.*;    
import java.net.*;    
public class URLConnectionExample {    
public static void main(String[] args){    
try{    
URL url=new URL("http://www.javatpoint.com/java-tutorial");    
URLConnection urlcon=url.openConnection();    
InputStream stream=urlcon.getInputStream();    
int i;    
while((i=stream.read())!=-1){    
System.out.print((char)i);    
}    
}catch(Exception e){System.out.println(e);}    
}    
}
Question: What is Java HttpURLConnection class?
Answer: The Java HttpURLConnection class is http specific URLConnection. It works for HTTP protocol only.

By the help of HttpURLConnection class, you can retrieve information of any HTTP URL such as header information, status code, response code etc.

The java.net.HttpURLConnection is subclass of URLConnection class.
Question: Can you give me the HttpURLConnection Class Constructor?
Answer: Constructor: protected HttpURLConnection(URL u)
Description: It constructs the instance of HttpURLConnection class.
Question: Can you give me the Java HttpURLConnection Methods?
Answer: Method: void disconnect()
Description: It shows that other requests from the server are unlikely in the near future.

Method: InputStream getErrorStream()
Description: It returns the error stream if the connection failed but the server sent useful data.

Method: Static boolean getFollowRedirects()
Description: It returns a boolean value to check whether or not HTTP redirects should be automatically followed.

Method: String getHeaderField(int n)
Description: It returns the value of nth header file.

Method: long getHeaderFieldDate(String name, long Default)
Description: It returns the value of the named field parsed as a date.

Method: String getHeaderFieldKey(int n)
Description: It returns the key for the nth header file.

Method: boolean getInstanceFollowRedirects()
Description: It returns the value of HttpURLConnection's instance FollowRedirects field.

Method: Permission getPermission()
Description: It returns the SocketPermission object representing the permission to connect to the destination host and port.

Method: String getRequestMethod()
Description: It gets the request method.

Method: int getResponseCode()
Description: It gets the response code from an HTTP response message.

Method: String getResponseMessage()
Description:It gets the response message sent along with the response code from a server.

Method: void setChunkedStreamingMode(int chunklen)
Description: The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance.

Method: void setFixedLengthStreamingMode(int contentlength)
Description: The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is known in advance.

Method: void setFixedLengthStreamingMode(long contentlength)
Description: The method is used to enable streaming of a HTTP request body without internal buffering, when the content length is not known in advance.

Method: static void setFollowRedirects(boolean set)
Description:It sets whether HTTP redirects (requests with response code) should be automatically followed by HttpURLConnection class.

 
Method: void setInstanceFollowRedirects(boolean followRedirects)
Description: It sets whether HTTP redirects (requests with response code) should be automatically followed by instance of HttpURLConnection class.

Method: void setRequestMethod(String method)
Description: Sets the method for the URL request, one of: GET POST HEAD OPTIONS PUT DELETE TRACE are legal, subject to protocol restrictions.

Method: abstract boolean usingProxy()
Description:   It shows if the connection is going through a proxy.
Question: How to get the object of HttpURLConnection class?
Answer: The openConnection() method of URL class returns the object of URLConnection class.

Syntax:

public URLConnection openConnection()throws IOException{}  

You can typecast it to HttpURLConnection type as given below.

URL url=new URL("http://www.javatpoint.com/java-tutorial");    
HttpURLConnection huc=(HttpURLConnection)url.openConnection();  
Java HttpURLConnection Example
import java.io.*;    
import java.net.*;    
public class HttpURLConnectionDemo{    
public static void main(String[] args){    
try{    
URL url=new URL("http://www.javatpoint.com/java-tutorial");    
HttpURLConnection huc=(HttpURLConnection)url.openConnection();  
for(int i=1;i<=8;i++){  
System.out.println(huc.getHeaderFieldKey(i)+" = "+huc.getHeaderField(i));  
}  
huc.disconnect();   
}catch(Exception e){System.out.println(e);}    
}    
}	


Output:

Date = Thu, 22 Jul 2021 18:08:17 GMT
Server = Apache
Location = https://www.javatpoint.com/java-tutorial
Cache-Control = max-age=2592000
Expires = Sat, 21 Aug 2021 18:08:17 GMT
Content-Length = 248
Keep-Alive =timeout=5, max=1500
Connection = Keep-Alive
Question: What is Java InetAddress class?
Answer: Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the IP of any host name for example www.javatpoint.com, www.google.com, www.facebook.com, etc.

An IP address is represented by 32-bit or 128-bit unsigned number. An instance of InetAddress represents the IP address with its corresponding host name. There are two types of addresses: Unicast and Multicast. The Unicast is an identifier for a single interface whereas Multicast is an identifier for a set of interfaces.

Moreover, InetAddress has a cache mechanism to store successful and unsuccessful host name resolutions.

IP Address

-An IP address helps to identify a specific resource on the network using a numerical representation.
-Most networks combine IP with TCP (Transmission Control Protocol). It builds a virtual bridge among the destination and the source.
There are two versions of IP address:
1. IPv4

IPv4 is the primary Internet protocol. It is the first version of IP deployed for production in the ARAPNET in 1983. It is a widely used IP version to differentiate devices on network using an addressing scheme. A 32-bit addressing scheme is used to store 232 addresses that is more than 4 million addresses.
Features of IPv4:
-It is a connectionless protocol.
-It utilizes less memory and the addresses can be remembered easily with the class based addressing scheme.
-It also offers video conferencing and libraries.
2. IPv6

IPv6 is the latest version of Internet protocol. It aims at fulfilling the need of more internet addresses. It provides solutions for the problems present in IPv4. It provides 128-bit address space that can be used to form a network of 340 undecillion unique IP addresses. IPv6 is also identified with a name IPng (Internet Protocol next generation).

Features of IPv6:

-It has a stateful and stateless both configurations.
-It provides support for quality of service (QoS).
-It has a hierarchical addressing and routing infrastructure.
TCP/IP Protocol

-TCP/IP is a communication protocol model used connect devices over a network via internet.
-TCP/IP helps in the process of addressing, transmitting, routing and receiving the data packets over the internet.

-The two main protocols used in this communication model are:

1.TCP i.e. Transmission Control Protocol. TCP provides the way to create a communication channel across the network. It also helps in transmission of packets at sender end as well as receiver end.
2.IP i.e. Internet Protocol. IP provides the address to the nodes connected on the internet. It uses a gateway computer to check whether the IP address is correct and the message is forwarded correctly or not.
Question: Can you give me the Java InetAddress Class Methods?
Answer: Method: public static InetAddress getByName(String host) throws UnknownHostException
Description: It returns the instance of InetAddress containing LocalHost IP and name.

Method: public static InetAddress getLocalHost() throws UnknownHostException
Description: It returns the instance of InetAdddress containing local host name and address.

Method: public String getHostName()
Description: It returns the host name of the IP address.

Method: public String getHostAddress()
Description: It returns the IP address in string format.
Question: Can you provide an Example of Java InetAddress Class?
Answer: Let's see a simple example of InetAddress class to get ip address of www.javatpoint.com website.

InetDemo.java

import java.io.*;  
import java.net.*;  
public class InetDemo{  
public static void main(String[] args){  
try{  
InetAddress ip=InetAddress.getByName("www.javatpoint.com");  
  
System.out.println("Host Name: "+ip.getHostName());  
System.out.println("IP Address: "+ip.getHostAddress());  
}catch(Exception e){System.out.println(e);}  
}  
} 

Output:
Host Name: www.javatpoint.com
IP Address: 172.67.196.82
Question: Can you give me the Program to demonstrate methods of InetAddress class?
Answer: InetDemo2.java

import java.net.Inet4Address;  
import java.util.Arrays;  
import java.net.InetAddress;  
public class InetDemo2  
{  
public static void main(String[] arg) throws Exception  
{  
InetAddress ip =  Inet4Address.getByName("www.javatpoint.com");  
InetAddress ip1[] = InetAddress.getAllByName("www.javatpoint.com");  
byte addr[]={72, 3, 2, 12};  
System.out.println("ip : "+ip);  
System.out.print("\nip1 : "+ip1);  
InetAddress ip2 =  InetAddress.getByAddress(addr);  
System.out.print("\nip2 : "+ip2);  
System.out.print("\nAddress : " +Arrays.toString(ip.getAddress()));  
System.out.print("\nHost Address : " +ip.getHostAddress());  
System.out.print("\nisAnyLocalAddress : " +ip.isAnyLocalAddress());  
System.out.print("\nisLinkLocalAddress : " +ip.isLinkLocalAddress());  
System.out.print("\nisLoopbackAddress : " +ip.isLoopbackAddress());  
System.out.print("\nisMCGlobal : " +ip.isMCGlobal());  
System.out.print("\nisMCLinkLocal : " +ip.isMCLinkLocal());  
System.out.print("\nisMCNodeLocal : " +ip.isMCNodeLocal());  
System.out.print("\nisMCOrgLocal : " +ip.isMCOrgLocal());  
System.out.print("\nisMCSiteLocal : " +ip.isMCSiteLocal());  
System.out.print("\nisMulticastAddress : " +ip.isMulticastAddress());  
System.out.print("\nisSiteLocalAddress : " +ip.isSiteLocalAddress());  
System.out.print("\nhashCode : " +ip.hashCode());  
System.out.print("\n Is ip1 == ip2 : " +ip.equals(ip2));  
}  
}
Question: What is Java DatagramSocket and DatagramPacket?
Answer: Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming using the UDP instead of TCP.

Datagram

Datagrams are collection of information sent from one device to another device via the established network. When the datagram is sent to the targeted device, there is no assurance that it will reach to the target device safely and completely. It may get damaged or lost in between. Likewise, the receiving device also never know if the datagram received is damaged or not. The UDP protocol is used to implement the datagrams in Java.
Question: What is Java DatagramSocket class?
Answer: Java DatagramSocket class represents a connection-less socket for sending and receiving datagram packets. It is a mechanism used for transmitting datagram packets over network.`

A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.
Question: What is Commonly used Constructors of DatagramSocket class?
Answer: -DatagramSocket() throws SocketEeption: it creates a datagram socket and binds it with the available Port Number on the localhost machine.
-DatagramSocket(int port) throws SocketEeption: it creates a datagram socket and binds it with the given Port Number.
-DatagramSocket(int port, InetAddress address) throws SocketEeption: it creates a datagram socket and binds it with the specified port number and host address.
Question: Can you give me the Java DatagramSocket Class?
Answer: Method: void bind(SocketAddress addr)
Description: It binds the DatagramSocket to a specific address and port.

Method: void close()
Description: It closes the datagram socket.


Method: void connect(InetAddress address, int port)
Description:It connects the socket to a remote address for the socket.
 

Method: void disconnect()
Description: It disconnects the socket.


Method: boolean getBroadcast()
Description: It tests if SO_BROADCAST is enabled.


Method:DatagramChannel getChannel()
Description: It returns the unique DatagramChannel object associated with the datagram socket.


Method: InetAddress getInetAddress()
Description: It returns the address to where the socket is connected.


Method: InetAddress getLocalAddress()
Description: It gets the local address to which the socket is connected.


Method: int getLocalPort()
Description:  It returns the port number on the local host to which the socket is bound.


Method: SocketAddress getLocalSocketAddress()
Description: It returns the address of the endpoint the socket is bound to.


Method: int getPort()
Description: It returns the port number to which the socket is connected.


Method: int getReceiverBufferSize()
Description: It gets the value of the SO_RCVBUF option for this DatagramSocket that is the buffer size used by the platform for input on the DatagramSocket.


Method: boolean isClosed()
Description: It returns the status of socket i.e. closed or not.


Method: boolean isConnected()
Description: It returns the connection state of the socket.


Method: void send(DatagramPacket p)
Description: It sends the datagram packet from the socket.


Method: void receive(DatagramPacket p)
Description:It receives the datagram packet from the socket.
Question: What is Java DatagramPacket Class?
Answer: Java DatagramPacket is a message that can be sent or received. It is a data container. If you send multiple packet, it may arrive in any order. Additionally, packet delivery is not guaranteed.
Question: What is Commonly used Constructors of DatagramPacket class?
Answer: -DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used to receive the packets.

-DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram packet. This constructor is used to send the packets.
Question: Can you give me the Java DatagramPacket Class Methods?
Answer: Method: InetAddress getAddress()
Description: It returns the IP address of the machine to which the datagram is being sent or from which the datagram was received.

Method: byte[] getData()
Description: It returns the data buffer.

Method:  int getLength()
Description: It returns the length of the data to be sent or the length of the data received.

Method: int getOffset()
Description: It returns the offset of the data to be sent or the offset of the data received.

Method: int getPort()
Description: It returns the port number on the remote host to which the datagram is being sent or from which the datagram was received.

Method: SocketAddress getSocketAddress()
Description: It gets the SocketAddress (IP address + port number) of the remote host that the packet is being sent to or is coming from.

Method: void setAddress(InetAddress iaddr)
Description: It sets the IP address of the machine to which the datagram is being sent.

Method:  void setData(byte[] buff)
Description: It sets the data buffer for the packet.

Method:  void setLength(int length)
Description: It sets the length of the packet.

Method: void setPort(int iport)
Description: It sets the port number on the remote host to which the datagram is being sent.

Method: void setSocketAddress(SocketAddress addr)
Description: It sets the SocketAddress (IP address + port number) of the remote host to which the datagram is being sent.
Question: Can you provide an Example of Sending DatagramPacket by DatagramSocket?
Answer: //DSender.java  
import java.net.*;  
public class DSender{  
  public static void main(String[] args) throws Exception {  
    DatagramSocket ds = new DatagramSocket();  
    String str = "Welcome java";  
    InetAddress ip = InetAddress.getByName("127.0.0.1");  
     
    DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);  
    ds.send(dp);  
    ds.close();  
  }  
}
Question: What is Java AWT Tutorial?
Answer: Java AWT (Abstract Window Toolkit) is an API to develop Graphical User Interface (GUI) or windows-based applications in Java.

Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. AWT is heavy weight i.e. its components are using the resources of underlying operating system (OS).

The java.awt package provides classes for AWT API such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.

The AWT tutorial will help the user to understand Java GUI programming in simple and easy steps.
Question: Why AWT is platform independent?
Answer: Java AWT calls the native platform calls the native platform (operating systems) subroutine for creating API components like TextField, ChechBox, button, etc.

For example, an AWT GUI with components like TextField, label and button will have different look and feel for the different platforms like Windows, MAC OS, and Unix. The reason for this is the platforms have different view for their native components and AWT directly calls the native subroutine that creates those components.

In simple words, an AWT application will look like a windows application in Windows OS whereas it will look like a Mac application in the MAC OS.
Question: Can you give me the Useful Methods of Component Class?
Answer: Method:  public void add(Component c)
Description: Inserts a component on this component.

Method: public void setSize(int width,int height)
Description: Sets the size (width and height) of the component.
 
Method:public void setLayout(LayoutManager m) 
Description:  Defines the layout manager for the component.

Method: public void setVisible(boolean status)
Description: Changes the visibility of the component, by default false.
Question: Can you provide the Java AWT Example?
Answer: To create simple AWT example, you need a frame. There are two ways to create a GUI using Frame in AWT.

1.By extending Frame class (inheritance)
2.By creating the object of Frame class (association)
Question: Can you provide the AWT Example by Inheritance?
Answer: Let's see a simple example of AWT where we are inheriting Frame class. Here, we are showing Button component on the Frame.

AWTExample1.java

// importing Java AWT class  
import java.awt.*;    
  
// extending Frame class to our class AWTExample1  
public class AWTExample1 extends Frame {    
  
   // initializing using constructor  
   AWTExample1() {  
  
      // creating a button   
      Button b = new Button("Click Me!!");  
  
      // setting button position on screen  
      b.setBounds(30,100,80,30);  
  
      // adding button into frame    
      add(b);  
  
      // frame size 300 width and 300 height    
      setSize(300,300);  
  
      // setting the title of Frame  
      setTitle("This is our basic AWT example");   
          
      // no layout manager   
      setLayout(null);   
  
      // now frame will be visible, by default it is not visible    
      setVisible(true);  
}    
  
// main method  
public static void main(String args[]) {   
  
// creating instance of Frame class   
AWTExample1 f = new AWTExample1();    
  
}  
  
}	

The setBounds(int x-axis, int y-axis, int width, int height) method is used in the above example that sets the position of the awt button.
Question: What is Event and Listener (Java Event Handling)?
Answer: 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.
Question: Can you give me the Java Event classes and Listener interfaces?
Answer: Event class:  ActionEvent
Listener Interface: ActionListener

Event class:  MouseEvent
Listener Interface: MouseListener and MouseMotionListener

Event class:  MouseWheelEvent
Listener Interface: MouseWheelListener

Event class:  KeyEvent
Listener Interface: KeyListener

Event class:  ItemEvent
Listener Interface: ItemListener

Event class:  TextEvent
Listener Interface: TextListener

Event class:  AdjustmentEvent
Listener Interface:AdjustmentListener

Event class:  WindowEvent
Listener Interface: WindowListener

Event class: ComponentEvent
Listener Interface: ComponentListener

Event class:  ContainerEvent
Listener Interface: ContainerListener

Event class:  FocusEvent
Listener Interface: FocusListener
Question: Can you give me the Registration Methods?
Answer: For registering the component with the Listener, many classes provide the registration methods. For example:

-Button
-public void addActionListener(ActionListener a){}

-MenuItem
-public void addActionListener(ActionListener a){}

-TextField
-public void addActionListener(ActionListener a){}
-public void addTextListener(TextListener a){}

-TextArea
-public void addTextListener(TextListener a){}

-Checkbox
-public void addItemListener(ItemListener a){}

-Choice
-public void addItemListener(ItemListener a){}

-List
-public void addActionListener(ActionListener a){}
-public void addItemListener(ItemListener a){}
Question: How to use Java Event Handling Code?
Answer: We can put the event handling code into one of the following places:

1.Within class
2.Other class
3.Anonymous class
Question: Can you give me the Java event handling by implementing ActionListener?
Answer: import java.awt.*;  
import java.awt.event.*;  
class AEvent extends Frame implements ActionListener{  
TextField tf;  
AEvent(){  
  
//create components  
tf=new TextField();  
tf.setBounds(60,50,170,20);  
Button b=new Button("click me");  
b.setBounds(100,120,80,30);  
  
//register listener  
b.addActionListener(this);//passing current instance  
  
//add components and set size, layout and visibility  
add(b);add(tf);  
setSize(300,300);  
setLayout(null);  
setVisible(true);  
}  
public void actionPerformed(ActionEvent e){  
tf.setText("Welcome");  
}  
public static void main(String args[]){  
new AEvent();  
}  
} 

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.
Question: Can you provide the Java event handling by outer class?
Answer: import java.awt.*;  
import java.awt.event.*;  
class AEvent2 extends Frame{  
TextField tf;  
AEvent2(){  
//create components  
tf=new TextField();  
tf.setBounds(60,50,170,20);  
Button b=new Button("click me");  
b.setBounds(100,120,80,30);  
//register listener  
Outer o=new Outer(this);  
b.addActionListener(o);//passing outer class instance  
//add components and set size, layout and visibility  
add(b);add(tf);  
setSize(300,300);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String args[]){  
new AEvent2();  
}  
} 

import java.awt.event.*;  
class Outer implements ActionListener{  
AEvent2 obj;  
Outer(AEvent2 obj){  
this.obj=obj;  
}  
public void actionPerformed(ActionEvent e){  
obj.tf.setText("welcome");  
}  
}
Question: Can you give the Java event handling by anonymous class?
Answer: import java.awt.*;  
import java.awt.event.*;  
class AEvent3 extends Frame{  
TextField tf;  
AEvent3(){  
tf=new TextField();  
tf.setBounds(60,50,170,20);  
Button b=new Button("click me");  
b.setBounds(50,120,80,30);  
  
b.addActionListener(new ActionListener(){  
public void actionPerformed(){  
tf.setText("hello");  
}  
});  
add(b);add(tf);  
setSize(300,300);  
setLayout(null);  
setVisible(true);  
}  
public static void main(String args[]){  
new AEvent3();  
}  
}
Question: What is Java AWT Button?
Answer: A button is basically a control component with a label that generates an event when pushed. 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.

When we press a button and release it, AWT sends an instance of ActionEvent to that button by calling processEvent on the button. The processEvent method of the button receives the all the events, then it passes an action event by calling its own method processActionEvent. This method passes the action event on to action listeners that are interested in the action events generated by the button.

To perform an action on a button being pressed and released, the ActionListener interface needs to be implemented. The registered new listener can receive events from the button by calling addActionListener method of the button. The Java application can use the button's action command as a messaging protocol.
Question: What is AWT Button Class Declaration?
Answer: public class Button extends Component implements Accessible
Question: Can you give me the Button Class Constructors?
Answer: Following table shows the types of Button class constructors


Sr. no :   1
Constructor:Button( )
Description: It constructs a new button with an empty string i.e. it has no label.

Sr. no :  2
Constructor: Button (String text)
Description: It constructs a new button with given string as its label.
Question: Can you give me the Button Class Methods?
Answer: Sr. no :   1
Method:  void setText (String text)
Description: It sets the string message on the button

Sr. no :   2
Method: String getText()
Description:It fetches the String message on the button.

Sr. no :   3
Method: void setLabel (String label)
Description: It sets the label of button with the specified string.

Sr. no :   4
Method: String getLabel()
Description:It fetches the label of the button.

Sr. no :   5
Method:  void addNotify()
Description:It creates the peer of the button

Sr. no :   6
Method: AccessibleContext getAccessibleContext()
Description:It fetched the accessible context associated with the button.

Sr. no :   7
Method:  void addActionListener(ActionListener l)
Description: It adds the specified action listener to get the action events from the button.

Sr. no :   8
Method: String getActionCommand()
Description: It returns the command name of the action event fired by the button.

Sr. no :   9
Method: ActionListener[ ] getActionListeners()
Description:It returns an array of all the action listeners registered on the button.

Sr. no :   10
Method: T[ ] getListeners(Class listenerType)
Description: It returns an array of all the objects currently registered as FooListeners upon this Button.
Sr. no :   11
Method: protected String paramString()
Description:It returns the string which represents the state of button.

Sr. no :   12
Method: protected void processActionEvent (ActionEvent e)
Description: It process the action events on the button by dispatching them to a registered ActionListener object.

Sr. no :   13
Method: protected void processEvent (AWTEvent e)
Description:It process the events on the button

Sr. no :   14
Method: void removeActionListener (ActionListener l)
Description:It removes the specified action listener so that it no longer receives action events from the button.

Sr. no :   15
Method: void setActionCommand(String command)
Description:It sets the command name for the action event given by the button.
Question: Can you provide the Java AWT Button Example?
Answer: ButtonExample.java

import java.awt.*;    
public class ButtonExample {    
public static void main (String[] args) {   
  
    // create instance of frame with the label   
    Frame f = new Frame("Button Example");    
  
    // create instance of button with label  
    Button b = new Button("Click Here");   
  
    // set the position for the button in frame   
    b.setBounds(50,100,80,30);    
  
    // add button to the frame  
    f.add(b);    
    // set size, layout and visibility of frame  
    f.setSize(400,400);    
    f.setLayout(null);    
    f.setVisible(true);     
}    
}    

To compile the program using command prompt type the following commands

C:\Users\Anurati\Desktop\abcDemo>javac ButtonExample.java  

If there's no error, we can execute the code using:

C:\Users\Anurati\Desktop\abcDemo>java ButtonExample
Question: What is Java AWT Label?
Answer: The object of the 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 a programmer but a user cannot edit it directly.

It is called a passive control as it does not create any event when it is accessed. To create a label, we need to create the object of Label class.
Question: What is AWT Label Class Declaration?
Answer: public class Label extends Component implements Accessible
Question: What is AWT Label Fields?
Answer: The java.awt.Component class has following fields:
1.static int LEFT: It specifies that the label should be left justified.
2.static int RIGHT: It specifies that the label should be right justified.
3.static int CENTER: It specifies that the label should be placed in center
Question: Can you give me the Label class Constructors?
Answer: Sr. no :1
Constructor: Label()
Description: It constructs an empty label.

Sr. no :2
Constructor: Label(String text)
Description:  It constructs a label with the given string (left justified by default).

Sr. no :3
Constructor: Label(String text, int alignement)
Description: It constructs a label with the specified string and the specified alignment.
Question: Can you give me the Label Class Methods?
Answer: Specified

Sr. no :1
Method name: void setText(String text)
Description:It sets the texts for label with the specified text..

Sr. no :2
Method name:  void setAlignment(int alignment)
Description:It sets the alignment for label with the specified alignment.

Sr. no :3
Method name:  String getText()
Description:It gets the text of the label

Sr. no :4
Method name:  int getAlignment()
Description: It gets the current alignment of the label.

Sr. no :5
Method name:  void addNotify()
Description: It creates the peer for the label

Sr. no :6
Method name:  AccessibleContext getAccessibleContext()
Description:It gets the Accessible Context associated with the label.

Sr. no :7
Method name: protected String paramString()
Description: It returns the string the state of the label.
Question: Can you provide the Java AWT Label Example with ActionListener?
Answer: In the following example, we are creating the objects of TextField, Label and Button classes and adding them to the Frame. Using the actionPerformed() method an event is generated over the button. When we add the website in the text field and click on the button, we get the IP address of website.

LabelExample2.java

import java.awt.*;    
import java.awt.event.*;    
  
// creating class which implements ActionListener interface and inherits Frame class  
public class LabelExample2 extends Frame implements ActionListener{    
      
    // creating objects of TextField, Label and Button class  
    TextField tf;   
    Label l;   
    Button b;   
  
    // constructor to instantiate the above objects  
    LabelExample2() {    
        tf = new TextField();    
        tf.setBounds(50, 50, 150, 20);   
  
        l = new Label();    
        l.setBounds(50, 100, 250, 20);    
  
        b = new Button("Find IP");    
        b.setBounds(50,150,60,30);    
        b.addActionListener(this);   
  
        add(b);  
        add(tf);  
        add(l);   
  
        setSize(400,400);    
        setLayout(null);    
        setVisible(true);    
    }    
  
    // defining actionPerformed method to generate an event  
    public void actionPerformed(ActionEvent e) {    
        try {    
        String host = tf.getText();    
        String ip = java.net.InetAddress.getByName(host).getHostAddress();    
        l.setText("IP of "+host+" is: "+ip);    
        }   
        catch (Exception ex) {  
            System.out.println(ex);  
        }    
    }    
  
    // main method  
    public static void main(String[] args) {    
        new LabelExample2();    
    }    
}
Question: What is Java AWT TextField?
Answer: The object of a TextField class is a text component that allows a user to enter a single line text and edit it. It inherits TextComponent class, which further inherits Component class.

When we enter a key in the text field (like key pressed, key released or key typed), the event is sent to TextField. Then the KeyEvent is passed to the registered KeyListener. It can also be done using ActionEvent; if the ActionEvent is enabled on the text field, then the ActionEvent may be fired by pressing return key. The event is handled by the ActionListener interface.
Question: What is AWT TextField Class Declaration?
Answer: public class TextField extends TextComponent
Question: Can you give me the TextField Class constructors?
Answer: Sr. no: 1
Constructor: TextField()
Description: It constructs a new text field component.

Sr. no:2
Constructor: TextField(String text)
Description:  It constructs a new text field initialized with the given string text to be displayed.

Sr. no:3
Constructor: TextField(int columns)
Description: It constructs a new textfield (empty) with given number of columns.

Sr. no:4
Constructor:  TextField(String text, int columns)
Description: It constructs a new text field with the given text and given number of columns (width).
Question: Can you give me the Method Inherited?
Answer: The AWT TextField class inherits the methods from below classes:

1.java.awt.TextComponent
2.java.awt.Component
3.java.lang.Object
Question: Can you provide the Java AWT TextField Example?
Answer: TextFieldExample1.java

// importing AWT class  
import java.awt.*;   
public class TextFieldExample1 {  
    // main method  
    public static void main(String args[]) {    
    // creating a frame  
    Frame f = new Frame("TextField Example");    
  
    // creating objects of textfield  
    TextField t1, t2;    
    // instantiating the textfield objects  
    // setting the location of those objects in the frame  
    t1 = new TextField("Welcome to Javatpoint.");    
    t1.setBounds(50, 100, 200, 30);    
    t2 = new TextField("AWT Tutorial");    
    t2.setBounds(50, 150, 200, 30);    
    // adding the components to frame  
    f.add(t1);  
    f.add(t2);   
    // setting size, layout and visibility of frame   
    f.setSize(400,400);    
    f.setLayout(null);    
    f.setVisible(true);    
}    
}
Question: What is Java AWT TextArea?
Answer: The object of a TextArea class is a multiline region that displays text. It allows the editing of multiple line text. It inherits TextComponent class.

The text area allows us to type as much text as we want. When the text in the text area becomes larger than the viewable area, the scroll bar appears automatically which helps us to scroll the text up and down, or right and left.
Question: What is AWT TextArea Class Declaration?
Answer: public class TextArea extends TextComponent
Question: Can you provide the Fields of TextArea Class?
Answer: The fields of java.awt.TextArea class are as follows:

-static int SCROLLBARS_BOTH - It creates and displays both horizontal and vertical scrollbars.
-static int SCROLLBARS_HORIZONTAL_ONLY - It creates and displays only the horizontal scrollbar.
–static int SCROLLBARS_VERTICAL_ONLY - It creates and displays only the vertical scrollbar.
-static int SCROLLBARS_NONE - It doesn't create or display any scrollbar in the text area.
Question: Can you give me the Class constructors?
Answer: Sr. no: 1
Constructor: TextArea()
Description: It constructs a new and empty text area with no text in it.

Sr. no: 2
Constructor: TextArea (int row, int column)
Description: It constructs a new text area with specified number of rows and columns and empty string as text.

Sr. no: 3
Constructor: TextArea (String text)
Description: It constructs a new text area and displays the specified text in it.

Sr. no: 4
Constructor: TextArea (String text, int row, int column)
Description: It constructs a new text area with the specified text in the text area and specified number of rows and columns.

Sr. no: 5
Constructor: TextArea (String text, int row, int column, int scrollbars)
Description: It construcst a new text area with specified text in text area and specified number of rows and columns and visibility.
Question: Can you give me the TetArea Class Methods?

Answer: Sr. no:1
Method name: void addNotify()
Description:It creates a peer of text area.

Sr. no:2
Method name:void append(String str)
Description: It appends the specified text to the current text of text area.


Sr. no:3
Method name: AccessibleContext getAccessibleContext()
Description: It returns the accessible context related to the text area


Sr. no:4
Method name: int getColumns()
Description: It returns the number of columns of text area.


Sr. no:5
Method name: Dimension getMinimumSize()
Description: It determines the minimum size of a text area.
 

Sr. no:6
Method name: Dimension getMinimumSize(int rows, int columns)
Description: It determines the minimum size of a text area with the given number of rows and columns.

Sr. no:7
Method name: Dimension getPreferredSize()
Description: It determines the preferred size of a text area.

Sr. no:8
Method name: Dimension preferredSize(int rows, int columns)
Description: It determines the preferred size of a text area with given number of rows and columns.

Sr. no:9
Method name: int getRows()
Description: It returns the number of rows of text area.

Sr. no:10
Method name: int getScrollbarVisibility()
Description: It returns an enumerated value that indicates which scroll bars the text area uses.

Sr. no:11
Method name: void insert(String str, int pos)
Description: It inserts the specified text at the specified position in this text area.

Sr. no:12
Method name: protected String paramString()
Description: It returns a string representing the state of this TextArea.

Sr. no:13
Method name: void replaceRange(String str, int start, int end)
Description: It replaces text between the indicated start and end positions with the specified replacement text.

Sr. no:14
Method name: void setColumns(int columns)
Description: It sets the number of columns for this text area.

Sr. no:15
Method name: void setRows(int rows)
Description: It sets the number of rows for this text area.
Question: Can you provide the Java AWT TextArea Example?
Answer: The below example illustrates the simple implementation of TextArea where we are creating a text area using the constructor TextArea(String text) and adding it to the frame.

TextAreaExample .java

//importing AWT class  
import java.awt.*;    
public class TextAreaExample    
{    
// constructor to initialize  
     TextAreaExample() {    
// creating a frame  
        Frame f = new Frame();    
// creating a text area  
            TextArea area = new TextArea("Welcome to javatpoint");    
// setting location of text area in frame  
        area.setBounds(10, 30, 300, 300);    
// adding text area to frame  
        f.add(area);  
// setting size, layout and visibility of frame    
        f.setSize(400, 400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
// main method  
public static void main(String args[])    
{    
   new TextAreaExample();    
}    
}
Question: Can you give me the Java AWT TextArea Example with ActionListener?
Answer: The following example displays a text area in the frame where it extends the Frame class and implements ActionListener interface. Using ActionListener the event is generated on the button press, where we are counting the number of character and words entered in the text area.

TextAreaExample2.java

// importing necessary libraries  
import java.awt.*;    
import java.awt.event.*;    
// our class extends the Frame class to inherit its properties  
// and implements ActionListener interface to override its methods  
public class TextAreaExample2 extends Frame implements ActionListener {    
// creating objects of Label, TextArea and Button class.  
Label l1, l2;    
TextArea area;    
Button b;    
// constructor to instantiate  
TextAreaExample2() {    
// instantiating and setting the location of components on the frame  
    l1 = new Label();    
    l1.setBounds(50, 50, 100, 30);    
    l2 = new Label();    
    l2.setBounds(160, 50, 100, 30);    
    area = new TextArea();    
    area.setBounds(20, 100, 300, 300);    
    b = new Button("Count Words");    
    b.setBounds(100, 400, 100, 30);    
  
// adding ActionListener to button  
    b.addActionListener(this);    
  
// adding components to frame  
    add(l1);  
add(l2);  
add(area);  
add(b);    
// setting the size, layout and visibility of frame  
    setSize(400, 450);    
    setLayout(null);    
    setVisible(true);    
}    
// generating event text area to count number of words and characters  
public void actionPerformed(ActionEvent e) {    
    String text = area.getText();    
    String words[]=text.split("\\s");    
    l1.setText("Words: "+words.length);    
    l2.setText("Characters: "+text.length());    
}    
// main method  
public static void main(String[] args) {    
    new TextAreaExample2();    
}    
}
Question: What is Java AWT Checkbox?
Answer: The Checkbox class is used to create a checkbox. It is used to turn an option on (true) or off (false). Clicking on a Checkbox changes its state from "on" to "off" or from "off" to "on".
Question: What is AWT Checkbox Class Declaration?
Answer: public class Checkbox extends Component implements ItemSelectable, Accessible
Question: Can you give me the Method inherited by Checkbox?
Answer: Sr. no:1
Constructor: Checkbox()
Description: It constructs a checkbox with no string as the label.

Sr. no:2
Constructor: Checkbox(String label)
Description: It constructs a checkbox with the given label.

Sr. no:3
Constructor: Checkbox(String label, boolean state)
Description: It constructs a checkbox with the given label and sets the given state.

Sr. no:4
Constructor: Checkbox(String label, boolean state, CheckboxGroup group)
Description: It constructs a checkbox with the given label, set the given state in the specified checkbox group.

Sr. no:5
Constructor: Checkbox(String label, CheckboxGroup group, boolean state)
Description: It constructs a checkbox with the given label, in the given checkbox group and set to the specified state.
Question: Can you give me the Checkbox Class Methods?
Answer: Sr. no: 1
Method name: void addItemListener(ItemListener IL)
Description: It adds the given item listener to get the item events from the checkbox.

Sr. no:2
Method name: AccessibleContext getAccessibleContext()
Description: It fetches the accessible context of checkbox.
 

Sr. no:3
Method name: void addNotify()
Description: It creates the peer of checkbox.


Sr. no:4
Method name: CheckboxGroup getCheckboxGroup()
Description:  It determines the group of checkbox.


Sr. no:5
Method name: ItemListener[] getItemListeners()
Description: It returns an array of the item listeners registered on checkbox.


Sr. no:6
Method name: String getLabel()
Description: It fetched the label of checkbox.

Sr. no:7
Method name: T[] getListeners(Class listenerType)
Description: It returns an array of all the objects registered as FooListeners.


Sr. no:8
Method name: Object[] getSelectedObjects()
Description: It returns an array (size 1) containing checkbox label and returns null if checkbox is not selected.


Sr. no:9
Method name: boolean getState()
Description: It returns true if the checkbox is on, else returns off.


Sr. no:10
Method name: protected String paramString()
Description: It returns a string representing the state of checkbox.


Sr. no:11
Method name: protected void processEvent(AWTEvent e)
Description: It processes the event on checkbox.


Sr. no:12
Method name: protected void processItemEvent(ItemEvent e)
Description: It process the item events occurring in the checkbox by dispatching them to registered ItemListener object.


Sr. no:13
Method name: void removeItemListener(ItemListener l)
Description: It removes the specified item listener so that the item listener doesn't receive item events from the checkbox anymore.

Sr. no:14
Method name: void setCheckboxGroup(CheckboxGroup g)
Description: It sets the checkbox's group to the given checkbox.

Sr. no:15
Method name: void setLabel(String label)
Description: It sets the checkbox's label to the string argument.
 
Sr. no:16
Method name:   void setState(boolean state)
Description: It sets the state of checkbox to the specified state.
Question: Can you provide the Java AWT Checkbox Example?
Answer: In the following example we are creating two checkboxes using the Checkbox(String label) constructo and adding them into the Frame using add() method.

CheckboxExample1.java

// importing AWT class  
import java.awt.*;    
public class CheckboxExample1  
{    
// constructor to initialize  
     CheckboxExample1() {    
// creating the frame with the title  
       Frame f = new Frame("Checkbox Example");    
// creating the checkboxes  
        Checkbox checkbox1 = new Checkbox("C++");    
        checkbox1.setBounds(100, 100,  50, 50);    
        Checkbox checkbox2 = new Checkbox("Java", true);    
// setting location of checkbox in frame          
checkbox2.setBounds(100, 150,  50, 50);    
// adding checkboxes to frame  
        f.add(checkbox1);    
        f.add(checkbox2);    
  
// setting size, layout and visibility of frame  
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
// main method  
public static void main (String args[])    
{    
    new CheckboxExample1();    
}    
}
Question: Can you give me the Java AWT Checkbox Example with ItemListener?
Answer: In the following example, we have created two checkboxes and adding them into the Frame. Here, we are adding the ItemListener with the checkbox which displays the state of the checkbox (whether it is checked or unchecked) using the getStateChange() method.

CheckboxExample2.java

// importing necessary packages  
import java.awt.*;    
import java.awt.event.*;    
public class CheckboxExample2  
{    
// constructor to initialize   
     CheckboxExample2() {    
// creating the frame  
        Frame f = new Frame ("CheckBox Example");    
// creating the label  
        final Label label = new Label();            
// setting the alignment, size of label       
   label.setAlignment(Label.CENTER);    
        label.setSize(400,100);    
// creating the checkboxes  
        Checkbox checkbox1 = new Checkbox("C++");    
        checkbox1.setBounds(100, 100,  50, 50);    
        Checkbox checkbox2 = new Checkbox("Java");    
        checkbox2.setBounds(100, 150,  50, 50);    
// adding the checkbox to frame  
f.add(checkbox1);  
f.add(checkbox2);   
f.add(label);    
  
// adding event to the checkboxes  
        checkbox1.addItemListener(new ItemListener() {    
             public void itemStateChanged(ItemEvent e) {                 
                label.setText("C++ Checkbox: "     
                + (e.getStateChange()==1?"checked":"unchecked"));    
             }    
          });    
        checkbox2.addItemListener(new ItemListener() {    
             public void itemStateChanged(ItemEvent e) {                 
                label.setText("Java Checkbox: "     
                + (e.getStateChange()==1?"checked":"unchecked"));    
             }    
          });    
// setting size, layout and visibility of frame  
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
     }    
// main method  
public static void main(String args[])    
{    
    new CheckboxExample2();    
}    
}
Question: What is Java AWT CheckboxGroup?
Answer: The object of CheckboxGroup class is used to group together a set of Checkbox. At a time only one check box button is allowed to be in "on" state and remaining check box button in "off" state. It inherits the object class.
Question: What is AWT CheckboxGroup Class Declaration?
Answer: public class CheckboxGroup extends Object implements Serializable