0% found this document useful (0 votes)
20 views22 pages

Cse4019 Assignment 2

Uploaded by

morenikhil2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views22 pages

Cse4019 Assignment 2

Uploaded by

morenikhil2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

CSE4019 ASSIGNMENT

Name: Aniruddha Pal


Reg no.: 22BCE11524

Program 1: Menu with Keyboard Mnemonics and


Accelerators
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CustomMenuDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom Menu Demo");
JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");


fileMenu.setMnemonic('F');

JMenuItem openItem = new JMenuItem("Open");


openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
ActionEvent.CTRL_MASK));
openItem.addActionListener(e ->
JOptionPane.showMessageDialog(frame, "Open selected!"));
JMenuItem helpItem = new JMenuItem("Help");
helpItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
ActionEvent.CTRL_MASK));
helpItem.addActionListener(e ->
JOptionPane.showMessageDialog(frame, "Help menu clicked!"));

JMenuItem exitItem = new JMenuItem("Exit");


exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
ActionEvent.CTRL_MASK));
exitItem.addActionListener(e -> {
frame.getContentPane().setBackground(Color.RED);
System.exit(0);
});

fileMenu.add(openItem);
fileMenu.add(helpItem);
fileMenu.add(exitItem);

menuBar.add(fileMenu);
frame.setJMenuBar(menuBar);

frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
Program 2: Toolbars

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CustomToolbarDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom Toolbar Demo");
JToolBar toolBar = new JToolBar();

JButton newButton = new JButton("New");


newButton.setToolTipText("Create a new file");
newButton.addActionListener(e ->
newButton.setBackground(Color.CYAN));
toolBar.add(newButton);

JButton openButton = new JButton("Open");


openButton.setToolTipText("Open an existing file");
openButton.addActionListener(e ->
openButton.setBackground(Color.ORANGE));
toolBar.add(openButton);

JButton saveButton = new JButton("Save");


saveButton.setToolTipText("Save the current file");
saveButton.addActionListener(e ->
saveButton.setBackground(Color.GREEN));
toolBar.add(saveButton);

frame.add(toolBar, "North");
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Program 3: Custom Dialogbox

import javax.swing.*;

public class CustomOptionDialogDemo {


public static void main(String[] args) {
for (int i = 5; i >= 0; i--) {
int result = JOptionPane.showConfirmDialog(null, "Countdown: "
+ i + ". Continue?", "Countdown Dialog",
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "You canceled at " + i);
break;
}
}
}
}

Output:
Program 4: File Choosers
import javax.swing.*;
import java.io.File;

public class CustomFileChooserDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom File Chooser Demo");
JTextArea textArea = new JTextArea();
frame.add(new JScrollPane(textArea), "Center");

JButton button = new JButton("Choose File");


button.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showOpenDialog(frame);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
textArea.setText("File: " + file.getName() + "\nPath: " +
file.getAbsolutePath());
}
});

frame.add(button, "South");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Program 5: Color Choosers

import javax.swing.*;
import java.awt.*;

public class CustomColorChooserDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom Color Chooser Demo");
JLabel label = new JLabel("Change my color!", JLabel.CENTER);
label.setOpaque(true);
frame.add(label, BorderLayout.CENTER);

JButton button = new JButton("Choose Color");


button.addActionListener(e -> {
Color color = JColorChooser.showDialog(frame, "Pick a Color",
Color.WHITE);
if (color != null) {
label.setBackground(color);
}
});

frame.add(button, BorderLayout.SOUTH);
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Program 6: Custom Spit Panes

import javax.swing.*;
import java.awt.*;

public class CustomSplitPaneDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom Split Pane Demo");

String[] items = {"Item 1", "Item 2", "Item 3"};


JList<String> list = new JList<>(items);

JLabel label = new JLabel("Select an item", JLabel.CENTER);

JSplitPane splitPane = new


JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(list),
label);
splitPane.setDividerLocation(150);

list.addListSelectionListener(e -> label.setText("Selected: " +


list.getSelectedValue()));

frame.add(splitPane);
frame.setSize(500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Program 7: Tabbed Panes

import javax.swing.*;

public class CustomTabbedPaneDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Custom Tabbed Pane Demo");
JTabbedPane tabbedPane = new JTabbedPane();

tabbedPane.addTab("Home", new JLabel("Welcome to Home Tab!"));

JButton addButton = new JButton("Add New Tab");


addButton.addActionListener(e -> {
int count = tabbedPane.getTabCount();
tabbedPane.addTab("Tab " + count, new JLabel("This is Tab " +
count));
});

frame.add(tabbedPane, "Center");
frame.add(addButton, "South");

frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
Program 8: List

import javax.swing.*;

public class ListDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("List Demo");

DefaultListModel<String> listModel = new DefaultListModel<>();


listModel.addElement("Item 1");
listModel.addElement("Item 2");
listModel.addElement("Item 3");

JList<String> list = new JList<>(listModel);


frame.add(new JScrollPane(list));

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
Program 9: Trees

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Tree Demo");

DefaultMutableTreeNode root = new


DefaultMutableTreeNode("Root");
DefaultMutableTreeNode node1 = new
DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new
DefaultMutableTreeNode("Node 2");
root.add(node1);
root.add(node2);

JTree tree = new JTree(root);


frame.add(new JScrollPane(tree));

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Program 10: Table

import javax.swing.*;

public class TableDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Table Demo");

String[] columns = {"ID", "Name", "Grade"};


String[][] data = {
{"1", "Aniruddha", "A"},
{"2", "Dhruv", "B"},
{"3", "Altheia", "C"}
};

JTable table = new JTable(data, columns);


frame.add(new JScrollPane(table));

frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Output:
Program 11: Progress Bars

import javax.swing.*;

public class ProgressBarDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Progress Bar Demo");

JProgressBar progressBar = new JProgressBar(0, 100);


progressBar.setValue(50);
progressBar.setStringPainted(true);

frame.add(progressBar);
frame.setSize(300, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

Output:
Final Program: All combined

Output:

You might also like