Swing
Swing
exam questions related to Java Swing, which might be useful for your study and exam
preparation:
Swing is a part of Java Foundation Classes (JFC) used for building graphical user
interfaces (GUIs).
It extends Abstract Window Toolkit (AWT) and offers more powerful and flexible
components.
Key Libraries:
AWT (Abstract Window Toolkit): Provides the base upon which Swing is built. It
includes classes for window, event handling, and graphics.
Swing: Contains richer and more flexible components than AWT, such as JButton,
JTextField, JLabel, etc.
Swing Components:
JComponent is the base class for all Swing components except top-level containers
(JFrame, JDialog, JApplet).
It provides common functionality such as painting, borders, and tooltips, making it
fundamental for creating custom Swing components.
Describe the basic steps to create a Swing application.
JButton: An interactive component that can trigger actions when clicked by the
user.
JLabel: A non-interactive component used to display text or images.
What is the role of JPanel in a Swing application?
JPanel is a flexible container that can group multiple components together. It can
use different layout managers to arrange its child components.
Layouts
Compare and contrast FlowLayout and BorderLayout.
FlowLayout:
Places components in a row, starting a new row if the current row is full.
Components are arranged left to right, and rows are top to bottom.
BorderLayout:
Divides the container into five regions: North, South, East, West, and Center.
Each region can hold only one component, and the center region expands to fill the
remaining space.
Explain how GridLayout manages components.
GridLayout arranges components in a grid of cells, with each cell having the same
size.
Components are added in a left-to-right, top-to-bottom order, and the layout
manager ensures that all cells are of equal size.
Event Handling
What is the delegation event model used in Swing?
The delegation event model is a pattern where events are generated by components
and dispatched to listeners that handle the events.
Components generate events (e.g., button clicks) and listeners (e.g.,
ActionListener) are registered to handle these events.
How do you add an ActionListener to a JButton?
Create an instance of ActionListener and register it with the JButton using the
addActionListener method.
java
Copy code
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Handle the button click event
}
});
What are some common event listener interfaces used in Swing?
Model: Represents the data and the business logic of the application.
View: Represents the presentation layer (UI components).
Controller: Manages the interaction between the Model and the View.
Swing components follow the MVC architecture, where the model represents the state,
the view is the component itself, and the controller is implicitly handled by the
component's internal mechanisms.
How does Swing achieve platform independence for its look and feel?
Swing uses a pluggable look and feel (PLAF) architecture, allowing the appearance
of components to be changed independently of their behavior.
Swing provides several built-in look and feel options (e.g., Metal, Nimbus,
Windows), and developers can create custom look and feel implementations.
Advanced Components
What is JTable and where is it used?
Layout managers handle the arrangement and sizing of components within a container.
They provide flexibility, allowing the GUI to adapt to different screen sizes and
resolutions.
They simplify the process of designing complex user interfaces by managing
component positioning automatically.
Explain how you would approach designing a user interface with multiple nested
panels.