0% found this document useful (0 votes)
4 views

Java_Swing_Programs

The document provides an overview of Java Swing, a part of Java Foundation Classes that offers a rich set of GUI components. It highlights key features, differences from AWT, and includes examples of basic and advanced components, as well as an explanation of the MVC architecture in Swing. Various code examples demonstrate how to create GUI elements like JFrame, JButton, and JTabbedPane, along with a simple MVC implementation.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Java_Swing_Programs

The document provides an overview of Java Swing, a part of Java Foundation Classes that offers a rich set of GUI components. It highlights key features, differences from AWT, and includes examples of basic and advanced components, as well as an explanation of the MVC architecture in Swing. Various code examples demonstrate how to create GUI elements like JFrame, JButton, and JTabbedPane, along with a simple MVC implementation.

Uploaded by

virajgadhe07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Java Swing Programs and Questions

2.1 Introduction to Swing


Swing is a part of Java Foundation Classes (JFC) that provides a rich set of GUI components.
Swing is built on top of AWT (Abstract Window Toolkit) and is entirely written in Java.

Features of Swing:
1. Platform-independent
2. Lightweight components
3. Supports MVC architecture
4. Pluggable look and feel

Difference between AWT and Swing:


- AWT is heavyweight; Swing is lightweight.
- AWT components rely on OS; Swing components are written in Java.
- Swing provides more flexible and advanced UI components.

2.2 Swing Components


Basic Swing components include JApplet, Icons, Labels, TextFields, and ComboBoxes.

Example 1: Creating a JFrame with a JLabel:


```java
import javax.swing.*;
public class LabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Label Example");
JLabel label = new JLabel("Hello, Swing!");
frame.add(label);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```

2.3 Buttons
Swing provides various buttons like JButton, JCheckBox, and JRadioButton.
Example 2: Creating JButton and adding ActionListener:
```java
import javax.swing.*;
import java.awt.event.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
JButton button = new JButton("Click Me");
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Button Clicked!"));
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```

2.4 Advanced Swing Components


Swing provides advanced components like JTabbedPane, JScrollPane, JTree, JTable,
JProgressBar, and ToolTips.

Example 3: Creating JTabbedPane:


```java
import javax.swing.*;
public class TabbedPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane Example");
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Tab 1", new JLabel("This is Tab 1"));
tabbedPane.add("Tab 2", new JLabel("This is Tab 2"));
frame.add(tabbedPane);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
2.5 MVC Architecture
Swing follows the Model-View-Controller (MVC) architecture, which separates UI logic (View), data
(Model), and control logic (Controller).

Example 4: Simple MVC in Swing:


```java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

// Model
class Counter {
private int count = 0;
public int getCount() { return count; }
public void increment() { count++; }
}

// View
class CounterView extends JFrame {
JButton button = new JButton("Increment");
JLabel label = new JLabel("Count: 0");

CounterView() {
setLayout(new java.awt.FlowLayout());
add(label);
add(button);
setSize(200, 150);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void updateCount(int count) {


label.setText("Count: " + count);
}
}

// Controller
class CounterController {
Counter model;
CounterView view;

CounterController(Counter model, CounterView view) {


this.model = model;
this.view = view;

view.button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.increment();
view.updateCount(model.getCount());
}
});
}
}

public class MVCExample {


public static void main(String[] args) {
Counter model = new Counter();
CounterView view = new CounterView();
new CounterController(model, view);
view.setVisible(true);
}
}
```

You might also like