0% found this document useful (0 votes)
6 views15 pages

Java Swing Menu and Dialog Examples

Uploaded by

Harsh Burman
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)
6 views15 pages

Java Swing Menu and Dialog Examples

Uploaded by

Harsh Burman
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

Name: Harsh Burman

Registration No.: 22BCE10439

Class Work-2

Program 1: Swing Menu


import [Link].*;
import [Link].*;

public class SwingMenu {


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Menu Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);

JPanel panel = new JPanel();

JMenuBar menuBar = new JMenuBar();

JMenu fileMenu = new JMenu("File");


[Link](KeyEvent.VK_F);

ImageIcon newIcon = new ImageIcon("./icons/[Link]");


ImageIcon openIcon = new ImageIcon("./icons/[Link]");
ImageIcon exitIcon = new ImageIcon("./icons/[Link]");

JMenuItem newItem = new JMenuItem("New", newIcon);


[Link](KeyEvent.VK_N);
[Link]([Link](KeyEvent.VK_N,
ActionEvent.CTRL_MASK));
[Link](e -> [Link](frame, "New
File Selected"));

JMenuItem openItem = new JMenuItem("Open", openIcon);


[Link](KeyEvent.VK_O);
[Link]([Link](KeyEvent.VK_O,
ActionEvent.CTRL_MASK));
[Link](e -> [Link](frame, "Open
File Selected"));

JMenuItem exitItem = new JMenuItem("Exit", exitIcon);


[Link](KeyEvent.VK_X);
[Link]([Link](KeyEvent.VK_X,
ActionEvent.CTRL_MASK));
[Link](e -> [Link](0));

JCheckBoxMenuItem autoSaveItem = new JCheckBoxMenuItem("Auto Save");


[Link](true);
[Link](e -> {
if ([Link]()) {
[Link](frame, "Auto Save Enabled");
} else {
[Link](frame, "Auto Save Disabled");
}
});

ButtonGroup group = new ButtonGroup();


JRadioButtonMenuItem lightModeItem = new JRadioButtonMenuItem("Light
Mode");
JRadioButtonMenuItem darkModeItem = new JRadioButtonMenuItem("Dark
Mode");
[Link](lightModeItem);
[Link](darkModeItem);
[Link](true);

[Link](newItem);
[Link](openItem);
[Link]();
[Link](exitItem);
[Link]();
[Link](autoSaveItem);
[Link]();
[Link](lightModeItem);
[Link](darkModeItem);
[Link](fileMenu);

JMenu editMenu = new JMenu("Edit");


JMenuItem copyItem = new JMenuItem("Copy");
[Link]([Link](KeyEvent.VK_C,
ActionEvent.CTRL_MASK));
[Link](e -> [Link](frame, "Copy
selected"));

JMenuItem pasteItem = new JMenuItem("Paste");


[Link]([Link](KeyEvent.VK_V,
ActionEvent.CTRL_MASK));
[Link](e -> [Link](frame, "Paste
selected"));

[Link](copyItem);
[Link](pasteItem);
[Link](editMenu);

JToolBar toolBar = new JToolBar();


JButton newButton = new JButton(new ImageIcon("./icons/[Link]"));
[Link]("New");
[Link](e -> [Link](frame, "New
File from Toolbar"));

JButton openButton = new JButton(new ImageIcon("./icons/[Link]"));


[Link]("Open");
[Link](e -> [Link](frame, "Open
File from Toolbar"));

JButton exitButton = new JButton(new ImageIcon("./icons/[Link]"));


[Link]("Exit");
[Link](e -> [Link](0));

[Link](newButton);
[Link](openButton);
[Link]();
[Link](exitButton);

[Link](menuBar);
[Link](toolBar, "North");

[Link](true);
}
}
Output:
Program 2: Swing Dialog Boxes
import [Link].*;
import [Link].*;
import [Link];

public class SwingDialog {


public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("Swing Dialog Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](500, 400);

JButton optionDialogButton = new JButton("Show Option Dialog");


[Link](e -> {
int result = [Link](frame, "Do you want to
proceed?",
"Option Dialog", JOptionPane.YES_NO_CANCEL_OPTION);
[Link](frame, "You selected: " +
(result == JOptionPane.YES_OPTION ? "Yes" : result ==
JOptionPane.NO_OPTION ? "No" : "Cancel"));
});

JButton customDialogButton = new JButton("Show Custom Dialog");


[Link](e -> {
JDialog customDialog = new JDialog(frame, "Custom Dialog", true);
[Link](300, 150);
[Link](new FlowLayout());
[Link](new JLabel("This is a custom dialog box."));
JButton closeButton = new JButton("Close");
[Link](ev -> [Link]());
[Link](closeButton);
[Link](frame);
[Link](true);
});

JButton fileChooserButton = new JButton("Show File Chooser");


[Link](e -> {
JFileChooser fileChooser = new JFileChooser();
int returnValue = [Link](frame);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = [Link]();
[Link](frame, "File selected: " +
[Link]());
}
});

JButton colorChooserButton = new JButton("Show Color Chooser");


[Link](e -> {
Color selectedColor = [Link](frame, "Choose a Color",
[Link]);
if (selectedColor != null) {
[Link]().setBackground(selectedColor);
[Link](frame, "Selected color: " +
selectedColor);
}
});

JPanel panel = new JPanel();


[Link](new GridLayout(4, 1, 10, 10));
[Link](optionDialogButton);
[Link](customDialogButton);
[Link](fileChooserButton);
[Link](colorChooserButton);

[Link](panel);
[Link](true);
});
}
}
Output:
Program 3: Swing Component Organizer
import [Link].*;
import [Link].*;

public class SwingComponentOrganizer {


public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("Swing Component Organizer Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](1000, 600);

JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

JTabbedPane tabbedPane = new JTabbedPane();


JTextArea textArea1 = new JTextArea("Content of Tab 1");
[Link](true);
[Link]("Tab 1", new JScrollPane(textArea1));
JTextArea textArea2 = new JTextArea("Content of Tab 2");
[Link](true);
[Link]("Tab 2", new JScrollPane(textArea2));

JTextArea textArea3 = new JTextArea("Content of Tab 3");


[Link](true);
[Link]("Tab 3", new JScrollPane(textArea3));

JDesktopPane desktopPane = new JDesktopPane();


for (int i = 0; i < 3; i++) {
JInternalFrame internalFrame = new JInternalFrame("Internal Frame " + (i +
1), true, true, true, true);
[Link](200, 150);
[Link](i * 50, i * 50);
[Link](true);
[Link](internalFrame);
}

JPanel controlsPanel = new JPanel(new FlowLayout([Link]));


JButton cascadeButton = new JButton("Cascade");
JButton tileButton = new JButton("Tile");
[Link](cascadeButton);
[Link](tileButton);

[Link](e -> cascadeWindows(desktopPane));


[Link](e -> tileWindows(desktopPane));

JPanel desktopWithControlsPanel = new JPanel(new BorderLayout());


[Link](desktopPane, [Link]);
[Link](controlsPanel, [Link]);

[Link](tabbedPane);
[Link](desktopWithControlsPanel);

[Link](250);

[Link](new BorderLayout());
[Link](splitPane, [Link]);
[Link](true);
});
}

private static void cascadeWindows(JDesktopPane desktopPane) {


int offset = 30;
int x = 0, y = 0;
for (JInternalFrame frame : [Link]()) {
[Link](x, y);
x += offset;
y += offset;
try {
[Link](true);
} catch (Exception ignored) {
}
}
}

private static void tileWindows(JDesktopPane desktopPane) {


JInternalFrame[] frames = [Link]();
int count = [Link];
if (count == 0)
return;

int rows = (int) [Link](count);


int cols = (int) [Link]((double) count / rows);

Dimension size = [Link]();


int w = [Link] / cols;
int h = [Link] / rows;

int x = 0, y = 0;
for (int i = 0; i < [Link]; i++) {
frames[i].setBounds(x, y, w, h);
x += w;
if ((i + 1) % cols == 0) {
x = 0;
y += h;
}
try {
frames[i].setSelected(true);
} catch (Exception ignored) {
}
}
}
}

Output:
Program 4: Advanced Swing Components
import [Link].*;
import [Link];
import [Link];
import [Link].*;

public class AdvancedSwingComponents {


public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("Advanced Swing Components");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](800, 600);

JPanel mainPanel = new JPanel(new GridLayout(2, 2, 10, 10));

JList<String> list = new JList<>(new String[] { "Item 1", "Item 2", "Item 3",
"Item 4" });
JScrollPane listScrollPane = new JScrollPane(list);
[Link](listScrollPane);

DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");


DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("Node 1");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Node 2");
[Link](new DefaultMutableTreeNode("Child 1"));
[Link](new DefaultMutableTreeNode("Child 2"));
[Link](new DefaultMutableTreeNode("Child 3"));
[Link](node1);
[Link](node2);
JTree tree = new JTree(root);
JScrollPane treeScrollPane = new JScrollPane(tree);
[Link](treeScrollPane);

String[] columnNames = { "Column 1", "Column 2", "Column 3" };


Object[][] data = {
{ "Row 1", "Value 1", "Value 2" },
{ "Row 2", "Value 3", "Value 4" },
{ "Row 3", "Value 5", "Value 6" }
};
JTable table = new JTable(new DefaultTableModel(data, columnNames));
JScrollPane tableScrollPane = new JScrollPane(table);
[Link](tableScrollPane);

JProgressBar progressBar = new JProgressBar(0, 100);


[Link](75);
[Link](true);
[Link](progressBar);

[Link](mainPanel);
[Link](true);
});
}
}
Output:

You might also like