Advance Java - Questions Bank

Advance Java Questions Bank
1 2 3 4 5 6 7 8 9

Q.What are the differences between AWT and Swing

SWING AWT

Swing components are light weight.

AWT components are heavy weight.

Swing components are drawn by java itself that's why it'splatform independent. AWT components are platform dependent.
Look and Feel of swing can be changed. There is no such feature in AWT.
All of the buttons, entry fields, etc. are drawn by the Swing package on the drawing surface provided by the window object. This is the reason that Swing has more code. AWT is a thin layer of code on top of OS.
Swing components are generally slower than AWT. Use of native peers speeds component performance.
Swing supports a wider range of features like icons and pop-up tool-tips for components. AWT components do not support features like icons and tool-tips.
Swing has many advanced features like JTabel, Jtabbed pane which is not available in AWT. These feature is not available in AWT.



Q.Why AWT is considered heavy weight as compared to Swing

AWT components are platform dependent. Every AWT component has a native object known as peer that's why AWT components are considered heavy weight.

Each GUI component that AWT provides has a peer object. Peer is a implementation of that component in native operating system. It means when programmer create an AWT component that component actually drawn by native environment not by AWT. Glue which allow AWT component and peer to work together, is known as peer interface.



Q.What is pluggable look and feel? How do you set the look and feel of the components

Pluggable look and feel is a mechanism used in the Java Swing widget toolkit allowing to change the look and feel of the graphical user interface at runtime.

Following code can be used to change look & feel

// set metal look & feel
UIManager.setLookAndFeel ("java.awt.swing.plaf.metal.MetalLookAndFeel" ) ;
 SwingUtilities.updateComponentTreeUI ( this ) ;

// set windows look & feel
UIManager.setLookAndFeel ("java.awt.swing.plaf.windows.WindowsLookAndFeel" ) ; 
SwingUtilities.updateComponentTreeUI ( this ) ;

// set motif look & feel
UIManager.setLookAndFeel ("java.awt.swing.plaf.motif.MotifLookAndFeel" ) ; 
SwingUtilities.updateComponentTreeUI ( this ) ;



Q.What is use of setDefaultCloseOperation method and what are possible values

This method is used to set close operation for window. Following are the values which can be used.

  • JFrame.EXIT_ON_CLOSE

    This option terminate application. So when user click on close button frame will disappear and application terminates.


  • JFrame.HIDE_ON_CLOSE

    This option simply hide the window and keep application running.


  • JFrame.DISPOSE_ON_CLOSE

    This option will destroy frame object and keep application running. In case if there is only one frame then JVM will terminate the application as well.


  • JFrame.DO_NOTHING_ON_CLOSE

    As name implies this option doesn't do anything on pressing of close button.



Q.Write a program to create a frame in swing

/* 
   Simple frame using Swing 
   www.jgyan.com
*/
import javax.swing.*;
class firstFrame
 {
  public static void main(String arg[])
   {
    JFrame jf=new JFrame("First Frame");
    jf.setSize(400,400);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 }

Read More..





Q.How to use setBounds() method? Give an example

setBounds method used in absence of layout manager. It is used to set position and size of components.

setBounds method need four arguments. First two arguments are x and y coordinates of top-left corner of component. Third argument is width of component and fourth is for width.

Example

/* 
  
   www.jgyan.com
*/
import javax.swing.*;
import java.awt.*;

class nolayout
 {
  public static void main(String arg[])
   {
    JFrame jf=new JFrame("BorderLayout");
    jf.setSize(400,400);
	// Setting layout null
	jf.setLayout(null);
	
	// Creating Button 
	JButton b1=new JButton("yes");
	
	// Setting position and size of button
	b1.setBounds(80,30,70,30);
	
	jf.add(b1);
	jf.setVisible(true);
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 
 }




Q.What is a Layout Manager and what are the different Layout Managers available in java.awt

A layout Manager is an object that is used to organize components in a container. The different layouts available in java.awt are:

  • FlowLayout:
    The elements of a FlowLayout are organized in a top to bottom, left to right fashion.

  • BorderLayout:
    The elements of a BorderLayout are organized at the borders (North, South, East and West) and the center of a container.

  • CardLayout:
    The elements of a CardLayout are stacked, one on top of the other, like a deck of cards.

  • GridLayout:
    The elements of a GridLayout are of equal size and are laid out using the square of a grid.

  • GridBagLayout:
    The elements of a GridBagLayout are organized according to a grid.However, the elements are of different sizes and may occupy more than one row or column of the grid. In addition, the rows and columns may have different sizes.



Q.Explain use of BorderLayout with help of example

BorderLayout layout manager can place GUI components in frame at five possible positions. It actually put components in frame along with one of frame's border.

These position are

  1. BorderLayout.PAGE_START
  2. BorderLayout.PAGE_END
  3. BorderLayout.LINE_START
  4. BorderLayout.LINE_END
  5. BorderLayout.CENTER
Note BorderLayout is default layout for a JFrame. While using BorderLayout if no position is specified for adding component, that component will be placed on CENTER position by default.

Program

/* 
   Use of BorderLayout 
   www.jgyan.com
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class borderlayout
 {
  public static void main(String arg[])
   {
    JFrame jf=new JFrame("BorderLayout");
    jf.setSize(400,400);
	
	// Creating Button 
	JButton b1=new JButton("TOP");
	JButton b2=new JButton("DOWN");
	JButton b3=new JButton("LEFT");
	JButton b4=new JButton("RIGHT");
	JButton b5=new JButton("CENTER");
    
	// Add buttons on different locations
	
	jf.add(b1,BorderLayout.PAGE_START);
	jf.add(b2,BorderLayout.PAGE_END);
	jf.add(b3,BorderLayout.LINE_START);
	jf.add(b4,BorderLayout.LINE_END);
	jf.add(b5,BorderLayout.CENTER);
	
	jf.setVisible(true);
	jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
 
 }




Q.Why Swing is called light weight

Most of Swing component are inherited form JComponent and doesn't required a native peer and that's why they are referred as light weight component. light weight component implement look and feel in Java rather than using look and feel of native component and that's why Swing's look and feel remains same across platform.



Q.What is difference between Container and Component

Main difference between Container and Component is that former can hold other components e.g. JFrame which is used as container to hold other components e.g. JButton






1 2 3 4 5 6 7 8 9