Java Swing Complete Notes
1. Basic Components
a) JFrame
• Definition: Top-level container representing a window.
• Syntax:
JFrame frame = new JFrame("Title");
frame.setSize(400,300);
frame.setVisible(true);
• Working: Creates a window with title, borders, and can hold components.
• Example:
import javax.swing.*;
public class FrameExample {
public static void main(String[] args) {
JFrame f = new JFrame("My Frame");
f.setSize(400, 300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
b) JLabel
• Definition: Displays text or an image; read-only.
• Syntax: JLabel label = new JLabel("Hello World");
• Working: Added to frames/panels for static text/icons.
• Example:
JLabel l = new JLabel("Welcome to Swing!");
frame.add(l);
c) JPanel
• Definition: Container to group components.
• Syntax: JPanel panel = new JPanel();
• Working: Add components to panel, then panel to frame.
• Example:
1
JPanel p = new JPanel();
p.add(new JButton("Click Me"));
frame.add(p);
d) JButton
• Definition: Push button to trigger actions.
• Syntax: JButton btn = new JButton("Click Here");
• Working: Generates ActionEvents handled by ActionListener.
• Example:
JButton b = new JButton("Submit");
frame.add(b);
2. Layout Managers
a) BorderLayout
• Definition: Divides container into five regions: North, South, East, West, Center.
• Syntax:
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.NORTH);
• Working: Components are arranged in specified regions; Center fills remaining space.
• Example:
frame.setLayout(new BorderLayout());
frame.add(new JButton("North"), BorderLayout.NORTH);
frame.add(new JButton("Center"), BorderLayout.CENTER);
b) FlowLayout
• Definition: Arranges components in a row, left to right.
• Syntax: frame.setLayout(new FlowLayout());
• Working: Components flow naturally and wrap to next line if space runs out.
• Example:
frame.setLayout(new FlowLayout());
frame.add(new JButton("A"));
frame.add(new JButton("B"));
2
c) GridLayout
• Definition: Arranges components in a grid of rows and columns.
• Syntax: frame.setLayout(new GridLayout(2,3));
• Working: All cells have equal size; fills row-wise.
• Example:
frame.setLayout(new GridLayout(2,2));
frame.add(new JButton("1"));
frame.add(new JButton("2"));
3. Advanced Elements
a) JLayeredPane
• Definition: Allows overlapping components in layers.
• Syntax: JLayeredPane layeredPane = new JLayeredPane();
• Working: Each component has a depth value; higher layers appear above.
• Example:
JLayeredPane lp = new JLayeredPane();
JButton b1 = new JButton("Bottom");
JButton b2 = new JButton("Top");
lp.add(b1, new Integer(1));
lp.add(b2, new Integer(2));
b) JOptionPane
• Definition: Predefined dialogs for messages, inputs, and confirmations.
• Syntax: JOptionPane.showMessageDialog(frame, "Message");
• Working: Pop-up dialog blocks execution until closed.
• Example:
JOptionPane.showMessageDialog(frame, "Hello User!");
c) JTextField
• Definition: Single-line input field.
• Syntax: JTextField tf = new JTextField(20);
• Working: User can type text; value retrieved via getText() .
• Example:
3
JTextField tf = new JTextField(20);
panel.add(tf);
d) JCheckBox
• Definition: Option that can be checked/unchecked.
• Syntax: JCheckBox cb = new JCheckBox("Option");
• Working: Generates ItemEvent when toggled.
• Example:
JCheckBox cb = new JCheckBox("Subscribe");
panel.add(cb);
e) JRadioButton
• Definition: Mutually exclusive options in a group.
• Syntax: JRadioButton rb = new JRadioButton("Yes");
• Working: Add to ButtonGroup to enforce exclusivity.
• Example:
JRadioButton rb1 = new JRadioButton("Male");
JRadioButton rb2 = new JRadioButton("Female");
ButtonGroup bg = new ButtonGroup();
bg.add(rb1); bg.add(rb2);
f) JComboBox
• Definition: Drop-down list of items.
• Syntax: JComboBox<String> cb = new JComboBox<>(new String[]{"A","B"});
• Working: User selects one item; selection retrieved via getSelectedItem() .
• Example:
JComboBox<String> combo = new JComboBox<>(new String[]{"Red","Green"});
panel.add(combo);
4. Interactive Features
a) JSlider
• Definition: Allows user to select numeric value by sliding.
• Syntax: JSlider slider = new JSlider(0,100,50);
• Example:
4
JSlider slider = new JSlider(0,100,50);
panel.add(slider);
b) JProgressBar
• Definition: Shows progress of task.
• Syntax: JProgressBar progress = new JProgressBar(0,100);
• Example:
JProgressBar progress = new JProgressBar();
progress.setValue(50);
panel.add(progress);
c) JMenuBar
• Definition: Menu bar to hold menus.
• Syntax:
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("File");
mb.add(file);
frame.setJMenuBar(mb);
d) JFileChooser
• Definition: File selection dialog.
• Syntax:
JFileChooser fc = new JFileChooser();
int i = fc.showOpenDialog(frame);
e) JColorChooser
• Definition: Color selection dialog.
• Syntax: Color c = JColorChooser.showDialog(frame,"Select Color", Color.RED);
5. Event Handling
a) KeyListener
• Definition: Captures keyboard events.
• Syntax:
5
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
});
b) MouseListener
• Definition: Captures mouse events (click, press, release, enter, exit).
• Syntax similar to KeyListener.
c) Drag & Drop
• Definition: Enables dragging components or data.
• Uses DragSource and DropTarget classes.
d) Key Bindings
• Definition: Maps a keystroke to an action without using KeyListener.
• Syntax:
component.getInputMap().put(KeyStroke.getKeyStroke("ENTER"),"actionName");
component.getActionMap().put("actionName", new AbstractAction() {
public void actionPerformed(ActionEvent e) {}
});
6. Graphics & Animation
a) 2D Graphics
• Definition: Drawing shapes, text, and images using Graphics2D .
• Syntax:
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawRect(50,50,100,100);
}
b) Animation
• Definition: Moving graphics by updating positions repeatedly.
• Syntax:
6
Timer t = new Timer(50, e -> { x++; panel.repaint(); });
t.start();
• Working: repaint() triggers paintComponent() to refresh graphics.
End of Notes