Chapter 5 User Interface with AWT and Swing
1m
(d) What is AutoBoxing and unboxing ?
d) What is AWT?
Answer: AWT stands for Abstract Window Toolkit. It is a part of JFC (Java Foundation Classes). It is a standard API for providing graphical user interface (GUI) for java program.
j) List any two listener.
Answer: Listener Is an object that watch for events and handles them when they occur.
Types of listeners:
1) Mouse listener: This interface is used for receiving mouse events.
2) Key listener: This interface is used for receiving key events.
3) Action listener: This interface is used for receiving action events.
i) What is Anonymous classes?
Answer: It is a class in java which has no name. They are used for creating event listeners where short implementation is needed.
a) Explain Inner and Nested class with example.
Answer:
2 mark
e) What is anonymous inner class?
Answer: Anonymous classes in java are more accurately known as anonymous inner class. They are defined insider another class.
4 mark
(j) Why swing objects are called as light weight components ?
Answer: The swing objects are light weight components because they are rendered mostly using pure JAVA code instead of operating system calls.
(c) Explain inner class with an example.
a) Write a Java program using AWT to change background color of table to 'RED' by clicking on button.
c) Differentiate between AWT and swing.
Answer:
Feature | AWT | Swing |
---|---|---|
Platform Dependency | Platform-dependent | Platform-independent |
Component Type | Heavyweight | Lightweight |
Customization | Limited | Extensive |
Components | Basic set | Rich set |
Performance | May have issues | Generally better |
Popularity | Widely used historically | Common in modern GUI development |
(B) (i) Explain Layout Managers used in AWT. [4]
Answer: In AWT layout managers are used to control placement and sizing of components within a container. They help us organize components within a container.
AWT provides several layout managers:
1) FlowLayout Manager:
-Here the components are arranged in left to right flow.
- It wraps the component to next row if there is not enough space.
- Few of it's constructors are
FlowLayout() : It centers all components and leaves five pixel spaces between each component.
FlowLayout(int align): It allows to specify how each line of component is align
i.e. Flowlayout.LEFT
Flowlayout.RIGHT
Flowlayout.Centre
2) Grid Layout Manager:
: It arranges components in a two dimensional grid. It has a defined number of rows and columns.
- It places items in rows (left to right) and columns (top to bottom).
- The components are present in cells and each of them have same size.
- Few of it's contructors:
GridLayout(): It creates single column grid layout.
GridLayout(int numRows, int numColumns) : It creates a grid layout with specified number of rows and columns.
3) Card Layout Manager:
- It arranges each component in a container as a card.
- Only one card is visible at a time, container acts as stack of cards.
- It has following constructors:
CardLayout(): It creates default card layout.
CardLayout(int horz, int vert): It allows us to specify horizontal and vertical space left between components horizontically and vertically respectively.
- (c) What is use of layout manager ? Explain any one layout manager ?
a) Write a Java program which will create a frame if we try to close it. It should change it’s color Red and it display closing message on the screen.(Use swing)
b) What are the different types of dialogs in Java? Write any one in detail
Answer: Dialogs are GUI elements in java used to interact with user, to gather input or present information.
Types of Dialogs
1) Message Dialogs: It displays informative messages to users like warning, error or general information.
JOptionPane.showMessageDialog();
2) JFile Chooser: Allows users to select files or directories.
JFileChooser(File currentDirectory)
3) JColor Chooser: Allows user to pick a color.
JColorChooser(Color initialColor): This constructor creates a color chooser pane with specified initial color.
c) Which swing classes are used to create menu? [2]
Answer: Swing classes used to create menus are :
1) JMenuBar: It represents menu bar , which is located at top of swing application.
2) JMenu: It represents a menu inside a menu bar.
3) JMenuItem: It represents an item in a menu.
4) JPopupMenu: It represents a pop-up menu that can be shown at specific location.
(b) How is menu created in java ? Explain with suitable example.
Answer:
(i) What is the use of Checkboxes and RadioButtons ? Explain with suitable example.
Answer: Checkboxes and Radiobuttons are components used in GUI allows users to make selections or choices. They are types of input components. They are used when users need to provide input by selecting one or more options from a set of choices.
Checkboxes are used when users can make multiple selections from a group of options.
Example of checkboxes:
JCheckBox checkbox1= new JCheckBox("Option 1");
JCheckBox checkbox2= newJCheckBox("Option 2");
JCheckBox checkbox3= newJCheckBox("Option 3");
RadioButtons are used when users need to make a single selection from a group of mutually exclusive options.
JRadioButton radioButton1 = new JRadioButton("Option 1"); JRadioButton radioButton2 = new JRadioButton("Option 2"); JRadioButton radioButton3 = new JRadioButton("Option 3");
(h) Write a syntax of JFileChooser class.
Answer: The JFileChooser class in Java Swing is used to create a file dialog that allows user to select files or directories.
import javax.swing.JFileChooser;
JFileChooser fileChooser = new JFileChooser(); int result = fileChooser.showOpenDialog(null); if (result == JFileChooser.APPROVE_OPTION) { java.io.File selectedFile = fileChooser.getSelectedFile(); // Process the selected file } else { // User canceled file selection }