Java swing

What is Swing? Explain the need of Swing.

1. Swing is a part the Java Foundation Classes
2. An extension library to AWT

3. GUI programming is simple and elegant

Need of Swing

1. The uniform look and feel across platforms is an advantage

2. Compatible for both standalone/GUI applications,Applets and Servlets

3. Employs model/view design pattern.

4. Has ability to replace the objects on-the-fly

5. Light weight components

6. Has the pluggable look and feel

Provide a Swing example.
import javax.swing.JFrame;
import javax.swing.JLabel;

public class HelloWorldFrame extends JFrame
{
   // initialize a label and display in a window. Invest a constructor ,
   // since there is no business logic
   // creates and makes the label ready

   HelloWorldFrame()
   {
       JLabel jlbHelloWorld = new JLabel("Hello World");
       add(jlbHelloWorld);
       this.setSize(100, 100);

       // make the frame visible
       setVisible(true);
   }

   public showLabel()
   {
       new HelloWorldFrame();
   }
}
Invoke the method showLabel() from another class

Write some important features of Swing.

1. Platform independent : Swing is platform independent in both expression and implementation

2. Extensible : Allows plugging of custom implementations of specified framework interfaces.Users can provide custom component implementations by overriding the default implementations

3. Customizable: The swing components are customizable by writing customize code for standard swing components by assigning specific Borders, Colors etc.

4. Configurable: Allows changes in its settings. For example, an application’s look and feel can be changed at runtime.

Every Swing component relies on the AWT container, as JComponent extends the AWT’s Container. So that Swing can be plugged into the host OS’s GUI management framework.

Describe Java Swing class hierarchy.

-The Swing class has the following hierarchy

1. Components – The basic building blocks

2. Containers - All top level containers are JFrame, JDialog, Applet

3. JFrame – A window with title, border and title

4. Dialog Boxes – Has Modality and more limited than frames

Types of Dialog Boxes:
1. JOptionPane – to create custom dialogs

2. ProgressMonitor – Shows the progress of a operation

3. JFileChooser – Allows to select a file

4. JDialog – To make a custom dialogs

5. JComponent has subclasses to create Textboxes,labels,lists,menubars etc.

6. Window is the super class of JFrame and JDialog clases

7. JToggleButton has sub classes – JcheckButton, JRadioButton

Explain the need of Layout manager.

1. Layout manager is used for laying the components that are added to the container.

2. The sizes and the positions of the components are set by using layout managers

3. The rules for arranging components are different for each layout mangers.

4. The default layout manger is FlowLayout.

5. Commonly used layout mangers are BorderLayout and GridLayout.

6. FlowLayout manager arranges the components in rows

7. BorderLayout manager arranges the components by dividing the container into 3-5 strips

8. GridLayout manager arranges the components in rectangular grid by having the same size for the components. A cell is used to place a component
Java layout manager
Java layout manager - Different layout manager in Java - Border Layout, Flow Layout, Card Layout, Grid Layout, Grid Bag Layout,...
Java exception handling
Java exception handling - Explain the need of Exception handling, Exceptions categories, i.e. checked and unchecked exceptions, Provide the general form of Exception handling constructs with explanation, What is user defined Exception?,...
Java multithreading
Java multithreading - What is Multithreading? Explain the life cycle of a thread, Explain how to use thread class for Multithreading in Java, What are the methods of thread class in java?, Can your explain thread priorities?...
Post your comment