Question
stringlengths 1
113
| Answer
stringlengths 22
6.98k
|
|---|---|
What are the Method Inherited by Canvas Class?
|
The Canvas has inherited above methods from the following classes:
lang.Component
lang.Object
|
Can you provide the Java AWT Canvas Example?
|
In the following example, we are creating a Canvas in the Frame and painting a red colored oval inside it.
CanvasExample.java
// importing awt class
import java.awt.*;
// class to construct a frame and containing main method
public class CanvasExample
{
// class constructor
public CanvasExample()
{
// creating a frame
Frame f = new Frame("Canvas Example");
// adding canvas to frame
f.add(new MyCanvas());
// setting layout, size and visibility of frame
f.setLayout(null);
f.setSize(400, 400);
f.setVisible(true);
}
// main method
public static void main(String args[])
{
new CanvasExample();
}
}
// class which inherits the Canvas class
// to create Canvas
class MyCanvas extends Canvas
{
// class constructor
public MyCanvas() {
setBackground (Color.GRAY);
setSize(300, 200);
}
// paint() method to draw inside the canvas
public void paint(Graphics g)
{
// adding specifications
g.setColor(Color.red);
g.fillOval(75, 75, 150, 75);
}
}
|
What is Java AWT Scrollbar?
|
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns.
It can be added to top-level container like Frame or a component like Panel. The Scrollbar class extends the Component class.
|
What is AWT Scrollbar Class Declaration?
|
public class Scrollbar extends Component implements Adjustable, Accessible
|
What are the Scrollbar Class Fields?
|
static int HORIZONTAL - It is a constant to indicate a horizontal scroll bar.
static int VERTICAL - It is a constant to indicate a vertical scroll bar.
|
What are the Constructors of the Scrollbar Class?
|
Sr. no.1
Constructor: Scrollbar()
Description: Constructs a new vertical scroll bar.
Sr. no.2
Constructor: Scrollbar(int orientation)
Description: Constructs a new scroll bar with the specified orientation.
Sr. no.3
Constructor: Scrollbar(int orientation, int value, int visible, int minimum, int maximum)
Description: Constructs a new scroll bar with the specified orientation, initial value, visible amount, and minimum and maximum values.
|
What are the Method Inherited by Scrollbar?
|
The methods of Scrollbar class are inherited from the following classes:
java.awt.Component
java.lang.Object
|
What are the Methods of the Scrollbar Class?
|
Sr. no. 1 Method name: void addAdjustmentListener (AdjustmentListener l)
Description: It adds the given adjustment listener to receive instances of AdjustmentEvent from the scroll bar.
Sr. no. 2 Method name: void addNotify()
Description: It creates the peer of scroll bar.
Sr. no. 3 Method name: int getBlockIncrement()
Description: It gets the block increment of the scroll bar.
Sr. no. 4 Method name: int getMaximum()
Description: It gets the maximum value of the scroll bar.
Sr. no. 5 Method name: int getMinimum()
Description: It gets the minimum value of the scroll bar.
Sr. no. 6 Method name: int getOrientation()
Description: It returns the orientation of scroll bar.
Sr. no. 7 Method name: int getUnitIncrement()
Description: int getUnitIncrement()
Sr. no. 8 Method name: int getValue()
Description: It fetches the current value of scroll bar.
Sr. no. 9 Method name: int getVisibleAmount()
Description: It fetches the visible amount of scroll bar.
Sr. no. 10 Method name: boolean getValueIsAdjusting()
Description: It returns true if the value is in process of changing where action results are being taken by the user.
Sr. no. 11 Method name: protected String paramString()
Description: It returns a string representing the state of Scroll bar.
Sr. no. 12 Method name: protected void processAdjustmentEvent (AdjustmentEvent e)
Description: It processes the adjustment event occurring on scroll bar by dispatching them to any registered AdjustmentListener objects.
Sr. no. 13 Method name: protected void processEvent(AWTEvent e)
Description: It processes the events on the scroll bar.
Sr. no. 14 Method name: void removeAdjustmentListener (AdjustmentListener l)
Description: It removes the given adjustment listener. Thus it no longer receives the instances of AdjustmentEvent from the scroll bar.
Sr. no. 15 Method name: void setBlockIncrement(int v)
Description: It sets the block increment from scroll bar.
Sr. no. 16 Method name: void setMaximum (int newMaximum)
Description: It sets the maximum value of the scroll bar.
Sr. no. 17 Method name: void setMinimum (int newMinimum)
Description: It sets the minimum value of the scroll bar.
Sr. no. 18 Method name: void setOrientation (int orientation)
Description: It sets the orientation for the scroll bar.
Sr. no. 19 Method name: void setUnitIncrement(int v)
Description: It sets the unit increment for the scroll bar.
Sr. no. 20 Method name: void setValue (int newValue)
Description: It sets the value of scroll bar with the given argument value.
Sr. no. 21 Method name: void setValueIsAdjusting (boolean b)
Description: It sets the valueIsAdjusting property to scroll bar.
Sr. no. 22 Method name: void setValues (int value, int visible, int minimum, int maximum)
Description: It sets the values of four properties for scroll bar: value, visible amount, minimum and maximum.
Sr. no. 23 Method name: void setVisibleAmount (int newAmount)
Description: It sets the visible amount of the scroll bar.
Sr. no. 24 Method name: AccessibleContext getAccessibleContext()
Description: It gets the accessible context related to the scroll bar.
Sr. no. 25 Method name: AdjustmentListener[] getAdjustmentListeners()
Description: It returns an array of al lthe adjustment listeners registered on the scroll bar.
Sr. no. 26 Method name: T[] getListeners(Class listenerType)
Description: It returns an array if all objects that are registered as FooListeners on the scroll bar currently.
|
Can you provide a Java AWT Scrollbar Example?
|
The object of Scrollbar class is used to add horizontal and vertical scrollbar. Scrollbar is a GUI component allows us to see invisible number of rows and columns.
It can be added to top-level container like Frame or a component like Panel. The Scrollbar class extends the Component class.
|
Can you provide a Java AWT Scrollbar Example with AdjustmentListener?
|
In the following example, we are creating a Scrollbar and adding it into the Frame. Here, we are using addAdjustmentListener() method of Scrollbar class which receives the instances of AdjustmentEvent and eventually we display it in the form of Label.
ScrollbarExample2.java
// importing necessary packages
import java.awt.*;
import java.awt.event.*;
public class ScrollbarExample2 {
// class constructor
ScrollbarExample2() {
// creating a Frame with a title
Frame f = new Frame("Scrollbar Example");
// creating a final object of Label
final Label label = new Label();
// setting alignment and size of label object
label.setAlignment(Label.CENTER);
label.setSize(400, 100);
// creating a final object of Scrollbar class
final Scrollbar s = new Scrollbar();
// setting the position of scroll bar
s.setBounds(100, 100, 50, 100);
// adding Scrollbar and Label to the Frame
f.add(s);
f.add(label);
// setting the size, layout, and visibility of frame
f.setSize(400, 400);
f.setLayout(null);
f.setVisible(true);
// adding AdjustmentListener to the scrollbar object
s.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
label.setText("Vertical Scrollbar value is:"+ s.getValue());
}
});
}
// main method
public static void main(String args[]){
new ScrollbarExample2();
}
}
|
What is Java AWT MenuItem and Menu?
|
The object of MenuItem class adds a simple labeled menu item on menu. The items used in a menu must belong to the MenuItem or any of its subclass.
The object of Menu class is a pull down menu component which is displayed on the menu bar. It inherits the MenuItem class.
|
What is AWT MenuItem class declaration?
|
public class MenuItem extends MenuComponent implements Accessible
|
What is AWT Menu class declaration?
|
public class Menu extends MenuItem implements MenuContainer, Accessible
|
Can you provide a Java AWT MenuItem and Menu Example?
|
import java.awt.*;
class MenuExample
{
MenuExample(){
Frame f= new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu submenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
submenu.add(i4);
submenu.add(i5);
menu.add(submenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();
}
}
|
What is Java AWT PopupMenu?
|
PopupMenu can be dynamically popped up at specific position within a component. It inherits the Menu class.
|
What is AWT PopupMenu class declaration?
|
public class PopupMenu extends Menu implements MenuContainer, Accessible
|
Can you provide a Java AWT PopupMenu Example?
|
import java.awt.*;
import java.awt.event.*;
class PopupMenuExample
{
PopupMenuExample(){
final Frame f= new Frame("PopupMenu Example");
final PopupMenu popupmenu = new PopupMenu("Edit");
MenuItem cut = new MenuItem("Cut");
cut.setActionCommand("Cut");
MenuItem copy = new MenuItem("Copy");
copy.setActionCommand("Copy");
MenuItem paste = new MenuItem("Paste");
paste.setActionCommand("Paste");
popupmenu.add(cut);
popupmenu.add(copy);
popupmenu.add(paste);
f.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
popupmenu.show(f , e.getX(), e.getY());
}
});
f.add(popupmenu);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PopupMenuExample();
}
}
|
What is Java AWT Panel?
|
The Panel is a simplest container class. It provides space in which an application can attach any other component. It inherits the Container class.
|
What is AWT Panel class declaration?
|
public class Panel extends Container implements Accessible
|
Can you provide a Java AWT Panel Example?
|
import java.awt.*;
public class PanelExample {
PanelExample()
{
Frame f= new Frame("Panel Example");
Panel panel=new Panel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
Button b1=new Button("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
Button b2=new Button("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PanelExample();
}
}
|
What is Java AWT Dialog?
|
The Dialog control represents a top level window with a border and a title used to take some form of input from the user. It inherits the Window class.
Unlike Frame, it doesn't have maximize and minimize buttons.
|
Frame vs Dialog
|
Frame and Dialog both inherits Window class. Frame has maximize and minimize buttons but Dialog doesn't have.
|
What is AWT Dialog class declaration?
|
public class Dialog extends Window
|
Can you provide a Java AWT Dialog Example?
|
import java.awt.*;
import java.awt.event.*;
public class DialogExample {
private static Dialog d;
DialogExample() {
Frame f= new Frame();
d = new Dialog(f , "Dialog Example", true);
d.setLayout( new FlowLayout() );
Button b = new Button ("OK");
b.addActionListener ( new ActionListener()
{
public void actionPerformed( ActionEvent e )
{
DialogExample.d.setVisible(false);
}
});
d.add( new Label ("Click button to continue."));
d.add(b);
d.setSize(300,300);
d.setVisible(true);
}
public static void main(String args[])
{
new DialogExample();
}
}
|
What is Java AWT Toolkit?
|
Toolkit class is the abstract superclass of every implementation in the Abstract Window Toolkit. Subclasses of Toolkit are used to bind various components. It inherits Object class.
|
What is AWT Toolkit class declaration?
|
public abstract class Toolkit extends Object
|
Can you provide a Java AWT Toolkit Example?
|
import java.awt.*;
public class ToolkitExample {
public static void main(String[] args) {
Toolkit t = Toolkit.getDefaultToolkit();
System.out.println("Screen resolution = " + t.getScreenResolution());
Dimension d = t.getScreenSize();
System.out.println("Screen width = " + d.width);
System.out.println("Screen height = " + d.height);
}
}
Output:
Screen resolution = 96
Screen width = 1366
Screen height = 768
|
Can you provide a Java AWT Toolkit Example: Change TitleBar Icon?
|
import java.awt.*;
class ToolkitExample {
ToolkitExample(){
Frame f=new Frame();
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
f.setIconImage(icon);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public static void main(String args[]){
new ToolkitExample();
}
}
|
Can you provide a Java ActionListener Example: On Button click?
|
import java.awt.*;
import java.awt.event.*;
//1st step
public class ActionListenerExample implements ActionListener{
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
//2nd step
b.addActionListener(this);
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
//3rd step
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
}
|
Can you provide a Java ActionListener Example: Using Anonymous class?
|
We can also use the anonymous class to implement the ActionListener. It is the shorthand way, so you do not need to follow the 3 steps:
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
Let us see the full code of ActionListener using anonymous class.
import java.awt.*;
import java.awt.event.*;
public class ActionListenerExample {
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
|
What is Java MouseListener Interface?
|
The Java MouseListener is notified whenever you change the state of mouse. It is notified against MouseEvent. The MouseListener interface is found in java.awt.event package. It has five methods.
|
What are the Methods of MouseListener interface?
|
The signature of 5 methods found in MouseListener interface are given below:
public abstract void mouseClicked(MouseEvent e);
public abstract void mouseEntered(MouseEvent e);
public abstract void mouseExited(MouseEvent e);
public abstract void mousePressed(MouseEvent e);
public abstract void mouseReleased(MouseEvent e);
|
Can you provide a Java MouseListener Example?
|
import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
|
What is Java MouseMotionListener Interface?
|
The Java MouseMotionListener is notified whenever you move or drag mouse. It is notified against MouseEvent. The MouseMotionListener interface is found in java.awt.event package. It has two methods.
|
What are the Methods of MouseMotionListener interface?
|
The signature of 2 methods found in MouseMotionListener interface are given below:
public abstract void mouseDragged(MouseEvent e);
public abstract void mouseMoved(MouseEvent e);
|
Can you provide a Java MouseMotionListener Example?
|
import java.awt.*;
import java.awt.event.*;
public class MouseMotionListenerExample extends Frame implements MouseMotionListener{
MouseMotionListenerExample(){
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args) {
new MouseMotionListenerExample();
}
}
|
What is Java ItemListener Interface?
|
The Java ItemListener is notified whenever you click on the checkbox. It is notified against ItemEvent. The ItemListener interface is found in java.awt.event package. It has only one method: itemStateChanged().
|
What is itemStateChanged() method?
|
The itemStateChanged() method is invoked automatically whenever you click or unclick on the registered checkbox component.
public abstract void itemStateChanged(ItemEvent e);
|
Can you provide a Java ItemListener Example?
|
import java.awt.*;
import java.awt.event.*;
public class ItemListenerExample implements ItemListener{
Checkbox checkBox1,checkBox2;
Label label;
ItemListenerExample(){
Frame f= new Frame("CheckBox Example");
label = new Label();
label.setAlignment(Label.CENTER);
label.setSize(400,100);
checkBox1 = new Checkbox("C++");
checkBox1.setBounds(100,100, 50,50);
checkBox2 = new Checkbox("Java");
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1); f.add(checkBox2); f.add(label);
checkBox1.addItemListener(this);
checkBox2.addItemListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void itemStateChanged(ItemEvent e) {
if(e.getSource()==checkBox1)
label.setText("C++ Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
if(e.getSource()==checkBox2)
label.setText("Java Checkbox: "
+ (e.getStateChange()==1?"checked":"unchecked"));
}
public static void main(String args[])
{
new ItemListenerExample();
}
}
|
What is Java KeyListener Interface?
|
The Java KeyListener is notified whenever you change the state of key. It is notified against KeyEvent. The KeyListener interface is found in java.awt.event package, and it has three methods.
|
What are the Methods of KeyListener interface?
|
Methods of KeyListener interface
The signature of 3 methods found in KeyListener interface are given below:
Method name: public abstract void keyPressed (KeyEvent e);
Description: It is invoked when a key has been pressed.
Method name: public abstract void keyReleased (KeyEvent e);
Description: It is invoked when a key has been released.
Method name: public abstract void keyTyped (KeyEvent e);
Description: It is invoked when a key has been typed.
|
Can you provide a Java KeyListener Example?
|
In the following example, we are implementing the methods of the KeyListener interface.
KeyListenerExample.java
// importing awt libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits Frame class and implements KeyListener interface
public class KeyListenerExample extends Frame implements KeyListener {
// creating object of Label class and TextArea class
Label l;
TextArea area;
// class constructor
KeyListenerExample() {
// creating the label
l = new Label();
// setting the location of the label in frame
l.setBounds (20, 50, 100, 20);
// creating the text area
area = new TextArea();
// setting the location of text area
area.setBounds (20, 80, 300, 300);
// adding the KeyListener to the text area
area.addKeyListener(this);
// adding the label and text area to the frame
add(l);
add(area);
// setting the size, layout and visibility of frame
setSize (400, 400);
setLayout (null);
setVisible (true);
}
// overriding the keyPressed() method of KeyListener interface where we set the text of the label when key is pressed
public void keyPressed (KeyEvent e) {
l.setText ("Key Pressed");
}
// overriding the keyReleased() method of KeyListener interface where we set the text of the label when key is released
public void keyReleased (KeyEvent e) {
l.setText ("Key Released");
}
// overriding the keyTyped() method of KeyListener interface where we set the text of the label when a key is typed
public void keyTyped (KeyEvent e) {
l.setText ("Key Typed");
}
// main method
public static void main(String[] args) {
new KeyListenerExample();
}
}
|
What is Java WindowListener Interface?
|
The Java WindowListener is notified whenever you change the state of window. It is notified against WindowEvent. The WindowListener interface is found in java.awt.event package. It has three methods.
|
What is WindowListener interface declaration?
|
The declaration for java.awt.event.WindowListener interface is shown below:
public interface WindowListener extends EventListener
|
What are the Methods of WindowListener interface?
|
Methods of WindowListener interface
The signature of 7 methods found in WindowListener interface with their usage are given below:
Method signature: public abstract void windowActivated (WindowEvent e);
Description: It is called when the Window is set to be an active Window.
Method signature: public abstract void windowClosed (WindowEvent e);
Description: It is called when a window has been closed as the result of calling dispose on the window.
Method signature: public abstract void windowClosing (WindowEvent e);
Description: It is called when the user attempts to close the window from the system menu of the window.
Method signature: public abstract void windowDeactivated (WindowEvent e);
Description: It is called when a Window is not an active Window anymore.
Method signature: public abstract void windowDeiconified (WindowEvent e);
Description: It is called when a window is changed from a minimized to a normal state.
Method signature: public abstract void windowIconified (WindowEvent e);
Description: It is called when a window is changed from a normal to a minimized state.
Method signature: public abstract void windowOpened (WindowEvent e);
Description: It is called when window is made visible for the first time.
|
What is Working of WindowListener interface?
|
-If a class needs to process some Window events, an object should exist which can implement the interface.
-As the object is already registered with Listener, an event will be generated on all the states of window.
-This helps in generation of invocation of relevant method in listener's object. And then WindowEvent is passed after invocation.
|
Can you provide a Java WindowListener Example?
|
In the following example, we are going to implement all the method of WindowListener interface one by one.
WindowExample.java
// importing necessary libraries of awt
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// class which inherits Frame class and implements WindowListener interface
public class WindowExample extends Frame implements WindowListener {
// class constructor
WindowExample() {
// adding WindowListener to the frame
addWindowListener(this);
// setting the size, layout and visibility of frame
setSize (400, 400);
setLayout (null);
setVisible (true);
}
// main method
public static void main(String[] args) {
new WindowExample();
}
// overriding windowActivated() method of WindowListener interface which prints the given string when window is set to be active
public void windowActivated (WindowEvent arg0) {
System.out.println("activated");
}
// overriding windowClosed() method of WindowListener interface which prints the given string when window is closed
public void windowClosed (WindowEvent arg0) {
System.out.println("closed");
}
// overriding windowClosing() method of WindowListener interface which prints the given string when we attempt to close window from system menu
public void windowClosing (WindowEvent arg0) {
System.out.println("closing");
dispose();
}
// overriding windowDeactivated() method of WindowListener interface which prints the given string when window is not active
public void windowDeactivated (WindowEvent arg0) {
System.out.println("deactivated");
}
// overriding windowDeiconified() method of WindowListener interface which prints the given string when window is modified from minimized to normal state
public void windowDeiconified (WindowEvent arg0) {
System.out.println("deiconified");
}
// overriding windowIconified() method of WindowListener interface which prints the given string when window is modified from normal to minimized state
public void windowIconified(WindowEvent arg0) {
System.out.println("iconified");
}
// overriding windowOpened() method of WindowListener interface which prints the given string when window is first opened
public void windowOpened(WindowEvent arg0) {
System.out.println("opened");
}
}
|
What is Java Adapter Classes?
|
Java adapter classes provide the default implementation of listener interfaces. If you inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. So it saves code.
|
What are the Pros of using Adapter classes?
|
It assists the unrelated classes to work combinedly.
It provides ways to use classes in different ways.
It increases the transparency of classes.
It provides a way to include related patterns in the class.
It provides a pluggable kit for developing an application.
It increases the reusability of the class.
|
What is the list of java.awt.event Adapter classes?
|
Adapter class : WindowAdapter
Listener interface : WindowListener
Adapter class : KeyAdapter
Listener interface : KeyListener
Adapter class : MouseAdapter
Listener interface : MouseListener
Adapter class : MouseMotionAdapter
Listener interface : MouseMotionListener
Adapter class : FocusAdapter
Listener interface : FocusListener
Adapter class : ComponentAdapter
Listener interface : ComponentListener
Adapter class : ContainerAdapter
Listener interface : ContainerListener
Adapter class : HierarchyBoundsAdapter
Listener interface : HierarchyBoundsListener
|
What is the list java.awt.dnd Adapter classes?
|
Adapter class : DragSourceAdapter
Listener interface : DragSourceListener
Adapter class : DragTargetAdapter
Listener interface : DragTargetListener
|
What is the list of javax.swing.event Adapter classes?
|
Adapter class : MouseInputAdapter
Listener interface : MouseInputListener
Adapter class : InternalFrameAdapter
Listener interface : InternalFrameListener
|
Can you provide a Java WindowAdapter Example?
|
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
public class AdapterExample {
// object of Frame
Frame f;
// class constructor
AdapterExample() {
// creating a frame with the title
f = new Frame ("Window Adapter");
// adding the WindowListener to the frame
// overriding the windowClosing() method
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
// setting the size, layout and
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// main method
public static void main(String[] args) {
new AdapterExample();
}
}
|
Can you provide a Java MouseAdapter Example?
|
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the MouseAdapter class
public class MouseAdapterExample extends MouseAdapter {
// object of Frame class
Frame f;
// class constructor
MouseAdapterExample() {
// creating the frame with the title
f = new Frame ("Mouse Adapter");
// adding MouseListener to the Frame
f.addMouseListener(this);
// setting the size, layout and visibility of the frame
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
// overriding the mouseClicked() method of the MouseAdapter class
public void mouseClicked (MouseEvent e) {
// creating the Graphics object and fetching them from the Frame object using getGraphics() method
Graphics g = f.getGraphics();
// setting the color of graphics object
g.setColor (Color.BLUE);
// setting the shape of graphics object
g.fillOval (e.getX(), e.getY(), 30, 30);
}
// main method
public static void main(String[] args) {
new MouseAdapterExample();
}
}
|
Can you provide a Java MouseMotionAdapter Example?
|
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the MouseMotionAdapter class
public class MouseMotionAdapterExample extends MouseMotionAdapter {
// object of Frame class
Frame f;
// class constructor
MouseMotionAdapterExample() {
// creating the frame with the title
f = new Frame ("Mouse Motion Adapter");
// adding MouseMotionListener to the Frame
f.addMouseMotionListener (this);
// setting the size, layout and visibility of the frame
f.setSize (300, 300);
f.setLayout (null);
f.setVisible (true);
}
// overriding the mouseDragged() method
public void mouseDragged (MouseEvent e) {
// creating the Graphics object and fetching them from the Frame object using getGraphics() method
Graphics g = f.getGraphics();
// setting the color of graphics object
g.setColor (Color.ORANGE);
// setting the shape of graphics object
g.fillOval (e.getX(), e.getY(), 20, 20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
|
Can you provide a Java KeyAdapter Example?
|
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the KeyAdapter class
public class KeyAdapterExample extends KeyAdapter {
// creating objects of Label, TextArea and Frame
Label l;
TextArea area;
Frame f;
// class constructor
KeyAdapterExample() {
// creating the Frame with title
f = new Frame ("Key Adapter");
// creating the Label
l = new Label();
// setting the location of label
l.setBounds (20, 50, 200, 20);
// creating the text area
area = new TextArea();
// setting the location of text area
area.setBounds (20, 80, 300, 300);
// adding KeyListener to text area
area.addKeyListener(this);
// adding the label and text area to frame
f.add(l);
f.add(area);
// setting the size, layout and visibility of frame
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// overriding the keyReleased() method
public void keyReleased (KeyEvent e) {
// creating the String object to get the text fromTextArea
String text = area.getText();
// splitting the String into words
String words[] = text.split ("\\s");
// setting the label text to print the number of words and characters of given string
l.setText ("Words: " + words.length + " Characters:" + text.length());
}
// main method
public static void main(String[] args) {
new KeyAdapterExample();
}
}
|
How to close AWT Window in Java?
|
We can close the AWT Window or Frame by calling dispose() or System.exit() inside windowClosing() method. The windowClosing() method is found in WindowListener interface and WindowAdapter class.
|
What are the Different ways to override windowClosing() method?
|
By anonymous class
By inheriting WindowAdapter class
By implementing WindowListener interface
|
Can you provide a Close AWT Window Example 1: Anonymous class?
|
// importing necessary awt libraries
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
// class which inherits the Frame class
public class WindowExample extends Frame {
// class constructor
WindowExample() {
// adding WindowListener to the Frame
// and using the windowClosing() method of WindowAdapter class
addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
dispose();
}
});
// setting the size, layout and visibility of frame
setSize (400, 400);
setLayout (null);
setVisible (true);
}
// main method
public static void main (String[] args) {
new WindowExample();
}
|
Can you provide a Close AWT Window Example 2: extending WindowAdapter?
|
// importing the necessary libraries
import java.awt.*;
import java.awt.event.*;
// class which inherits the WindowAdapter class
public class AdapterExample extends WindowAdapter {
// object of Frame
Frame f;
// class constructor
AdapterExample() {
// creating the frame
f = new Frame();
// adding WindowListener to the frame
f.addWindowListener (this);
// setting the size, layout and visibility of frame
f.setSize (400, 400);
f.setLayout (null);
f.setVisible (true);
}
// overriding the windowClosing() method
public void windowClosing (WindowEvent e) {
f.dispose();
}
// main method
public static void main(String[] args) {
new AdapterExample();
}
}
|
What is the Difference between AWT and Swing?
|
No.1 Java AWT : AWT components are platform-dependent.
Java Swing: Java swing components are platform-independent.
No.2 Java AWT : AWT components are heavyweight.
Java Swing: Swing components are lightweight.
No.3 Java AWT : AWT doesn't support pluggable look and feel.
Java Swing: Swing supports pluggable look and feel
No.4 Java AWT : AWT provides less components than Swing.
Java Swing: Swing provides more powerful components such as tables, lists, scrollpanes, colorchooser, tabbedpane etc.
No.5 Java AWT : 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.
Java Swing: Swing follows MVC.
|
What is JFC?
|
The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of desktop applications.
|
What are the Commonly used Methods of Component class?
|
Method: public void add(Component c)
Description: add a component on another component.
Method: public void setSize(int width,int height)
Description: sets size of the component.
Method: public void setLayout(LayoutManager m)
Description: sets the layout manager for the component.
Method: public void setVisible(boolean b)
Description: sets the visibility of the component. It is by default false.
|
What is Java JButton?
|
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.
|
What is JButton class declaration?
|
public class JButton extends AbstractButton implements Accessible
|
What are the Commonly used Constructors:?
|
Constructor: JButton()
Description: It creates a button with no text and icon.
Constructor: JButton(String s)
Description: It creates a button with the specified text.
Constructor: JButton(Icon i)
Description: It creates a button with the specified icon object.
|
What are the Commonly used Methods of AbstractButton class?
|
Methods: void setText(String s)
Description: It is used to set specified text on button
Methods: String getText()
Description: It is used to return the text of the button.
Methods: void setEnabled(boolean b)
Description: It is used to enable or disable the button.
Methods: void setIcon(Icon b)
Description: It is used to set the specified Icon on the button.
Methods: Icon getIcon()
Description: It is used to get the Icon of the button.
Methods: void setMnemonic(int a)
Description: It is used to set the mnemonic on the button.
Methods: It is used to set the mnemonic on the button.
Description: It is used to add the action listener to this object.
|
Can you provide a Java JButton Example?
|
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
|
Can you provide a Java JButton Example with ActionListener?
|
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
|
Can you provide an Example of displaying image on the button?
|
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:\\icon.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}
|
What is Java JLabel?
|
The object of JLabel 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. It inherits JComponent class.
|
What is JLabel class declaration?
|
public class JLabel extends JComponent implements SwingConstants, Accessible
|
Can you provide a Java JLabel Example?
|
import javax.swing.*;
class LabelExample
{
public static void main(String args[])
{
JFrame f= new JFrame("Label Example");
JLabel l1,l2;
l1=new JLabel("First Label.");
l1.setBounds(50,50, 100,30);
l2=new JLabel("Second Label.");
l2.setBounds(50,100, 100,30);
f.add(l1); f.add(l2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
|
Can you provide a Java JLabel Example with ActionListener?
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LabelExample extends Frame implements ActionListener{
JTextField tf; JLabel l; JButton b;
LabelExample(){
tf=new JTextField();
tf.setBounds(50,50, 150,20);
l=new JLabel();
l.setBounds(50,100, 250,20);
b=new JButton("Find IP");
b.setBounds(50,150,95,30);
b.addActionListener(this);
add(b);add(tf);add(l);
setSize(400,400);
setLayout(null);
setVisible(true);
}
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);}
}
public static void main(String[] args) {
new LabelExample();
} }
|
What is Java JTextField?
|
The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class.
|
What is JTextField class declaration?
|
public class JTextField extends JTextComponent implements SwingConstants
|
Can you provide a Java JTextField Example?
|
import javax.swing.*;
class TextFieldExample
{
public static void main(String args[])
{
JFrame f= new JFrame("TextField Example");
JTextField t1,t2;
t1=new JTextField("Welcome to Javatpoint.");
t1.setBounds(50,100, 200,30);
t2=new JTextField("AWT Tutorial");
t2.setBounds(50,150, 200,30);
f.add(t1); f.add(t2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
|
Can you provide a Java JTextField Example with ActionListener?
|
import javax.swing.*;
import java.awt.event.*;
public class TextFieldExample implements ActionListener{
JTextField tf1,tf2,tf3;
JButton b1,b2;
TextFieldExample(){
JFrame f= new JFrame();
tf1=new JTextField();
tf1.setBounds(50,50,150,20);
tf2=new JTextField();
tf2.setBounds(50,100,150,20);
tf3=new JTextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new JButton("+");
b1.setBounds(50,200,50,50);
b2=new JButton("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new TextFieldExample();
} }
|
What is Java JTextArea?
|
The object of a JTextArea class is a multi line region that displays text. It allows the editing of multiple line text. It inherits JTextComponent class
|
What is JTextArea class declaration?
|
public class JTextArea extends JTextComponent
|
Can you provide a Java JTextArea Example?
|
import javax.swing.*;
public class TextAreaExample
{
TextAreaExample(){
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
area.setBounds(10,30, 200,200);
f.add(area);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new TextAreaExample();
}}
|
Can you provide a Java JTextArea Example with ActionListener?
|
import javax.swing.*;
import java.awt.event.*;
public class TextAreaExample implements ActionListener{
JLabel l1,l2;
JTextArea area;
JButton b;
TextAreaExample() {
JFrame f= new JFrame();
l1=new JLabel();
l1.setBounds(50,25,100,30);
l2=new JLabel();
l2.setBounds(160,25,100,30);
area=new JTextArea();
area.setBounds(20,75,250,200);
b=new JButton("Count Words");
b.setBounds(100,300,120,30);
b.addActionListener(this);
f.add(l1);f.add(l2);f.add(area);f.add(b);
f.setSize(450,450);
f.setLayout(null);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String text=area.getText();
String words[]=text.split("\\s");
l1.setText("Words: "+words.length);
l2.setText("Characters: "+text.length());
}
public static void main(String[] args) {
new TextAreaExample();
}
}
|
What is JLabel class declaration?
|
public class JLabel extends JComponent implements SwingConstants, Accessible
|
What is JPasswordField class declaration?
|
public class JPasswordField extends JTextField
|
Can you provide a Java JPasswordField Example?
|
import javax.swing.*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
JPasswordField value = new JPasswordField();
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
value.setBounds(100,100,100,30);
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
|
Can you provide a Java JPasswordField Example with ActionListener?
|
import javax.swing.*;
import java.awt.event.*;
public class PasswordFieldExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
final JLabel label = new JLabel();
label.setBounds(20,150, 200,50);
final JPasswordField value = new JPasswordField();
value.setBounds(100,75,100,30);
JLabel l1=new JLabel("Username:");
l1.setBounds(20,20, 80,30);
JLabel l2=new JLabel("Password:");
l2.setBounds(20,75, 80,30);
JButton b = new JButton("Login");
b.setBounds(100,120, 80,30);
final JTextField text = new JTextField();
text.setBounds(100,20, 100,30);
f.add(value); f.add(l1); f.add(label); f.add(l2); f.add(b); f.add(text);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Username " + text.getText();
data += ", Password: "
+ new String(value.getPassword());
label.setText(data);
}
});
}
}
|
What is Java JCheckBox?
|
The JCheckBox 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 ".It inherits JToggleButton class.
|
What is JCheckBox class declaration?
|
public class JCheckBox extends JToggleButton implements Accessible
|
Can you provide a Java JCheckBox Example?
|
import javax.swing.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);
f.add(checkBox1);
f.add(checkBox2);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}}
|
Can you provide a Java JCheckBox Example with ItemListener?
|
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxExample
{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
JCheckBox checkbox1 = new JCheckBox("C++");
checkbox1.setBounds(150,100, 50,50);
JCheckBox checkbox2 = new JCheckBox("Java");
checkbox2.setBounds(150,150, 50,50);
f.add(checkbox1); f.add(checkbox2); f.add(label);
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"));
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new CheckBoxExample();
}
}
|
What is Java JRadioButton?
|
The JRadioButton class is used to create a radio button. It is used to choose one option from multiple options. It is widely used in exam systems or quiz.
It should be added in ButtonGroup to select one radio button only.
|
What is JRadioButton class declaration?
|
public class JRadioButton extends JToggleButton implements Accessible
|
Can you provide a Java JRadioButton Example?
|
import javax.swing.*;
public class RadioButtonExample {
JFrame f;
RadioButtonExample(){
f=new JFrame();
JRadioButton r1=new JRadioButton("A) Male");
JRadioButton r2=new JRadioButton("B) Female");
r1.setBounds(75,50,100,30);
r2.setBounds(75,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(r1);bg.add(r2);
f.add(r1);f.add(r2);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new RadioButtonExample();
}
}
|
Can you provide a Java JRadioButton Example with ActionListener?
|
import javax.swing.*;
import java.awt.event.*;
class RadioButtonExample extends JFrame implements ActionListener{
JRadioButton rb1,rb2;
JButton b;
RadioButtonExample(){
rb1=new JRadioButton("Male");
rb1.setBounds(100,50,100,30);
rb2=new JRadioButton("Female");
rb2.setBounds(100,100,100,30);
ButtonGroup bg=new ButtonGroup();
bg.add(rb1);bg.add(rb2);
b=new JButton("click");
b.setBounds(100,150,80,30);
b.addActionListener(this);
add(rb1);add(rb2);add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(rb1.isSelected()){
JOptionPane.showMessageDialog(this,"You are Male.");
}
if(rb2.isSelected()){
JOptionPane.showMessageDialog(this,"You are Female.");
}
}
public static void main(String args[]){
new RadioButtonExample();
}}
|
What is Java JComboBox?
|
The object of Choice class is used to show popup menu of choices. Choice selected by user is shown on the top of a menu. It inherits JComponent class.
|
What is JComboBox class declaration?
|
public class JComboBox extends JComponent implements ItemSelectable, ListDataListener, ActionListener, Accessible
|
Can you provide an Example of Java JComboBox?
|
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
|
Can you provide a Java JComboBox Example with ActionListener?
|
import javax.swing.*;
import java.awt.event.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
JButton b=new JButton("Show");
b.setBounds(200,100,75,20);
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
cb.setBounds(50, 100,90,20);
f.add(cb); f.add(label); f.add(b);
f.setLayout(null);
f.setSize(350,350);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "
+ cb.getItemAt(cb.getSelectedIndex());
label.setText(data);
}
});
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
|
What is Java JTable?
|
The JTable class is used to display data in tabular form. It is composed of rows and columns.
|
What is JTable class declaration?
|
Constructor: JTable()
Description: Creates a table with empty cells.
Constructor: JTable(Object[][] rows, Object[] columns)
Description: Creates a table with the specified data.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.