Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
Bhagwan Mahavir College of Computer Application (BCA)
[Link](Integrated) SEM - 4
Advance java - Problem Sheet 1
1. write a java program to perform checkbox example.
Code:
import [Link].*;
import [Link].*;
public class CheckBoxExample extends JFrame implements
ActionListener
{
JLabel l;
JCheckBox cb1,cb2,cb3;
JButton b;
CheckBoxExample ()
{
l=new JLabel("food ordering System");
[Link](50,50,300,20);
cb1 = new JCheckBox("pizza @100");
[Link](100,100,150,20);
cb2 = new JCheckBox("burger @50");
[Link](100,150,150,20);
cb3 = new JCheckBox("tea @10");
[Link](100,200,150,20);
b=new JButton("order");
P a g e 1 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
[Link](100,250,80,30);
[Link](this);
add(l);
add(cb1);
add(cb2);
add(cb3);
add(b);
setSize(400,400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
public void actionPerformed(ActionEvent e)
{
float amount=0;
String msg="";
if([Link]())
{
amount+=100;
msg="pizza:100\n";
}
if([Link]())
P a g e 2 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
{
amount+=30;
msg="Burger:50\n";
}
if([Link]())
{
amount+=10;
msg="tea:10\n";
}
msg+="---------------\n";
[Link](this,msg+"Total:"+amount);
}
public static void main(String[]args)
{
new CheckBoxExample();
}
}
Output:
P a g e 3 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
[Link] a java program to perform ColorChooser example
Code:
package swing;
import [Link].*;
import [Link].*;
import [Link].*;
public class ColorChooserExample extends JFrame implements
ActionListener
{
JButton b;
Container c;
P a g e 4 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
ColorChooserExample()
{
c=getContentPane();
[Link](new FlowLayout());
b=new JButton("color");
[Link](this);
[Link](b);
}
public void actionPerformed(ActionEvent e)
{
Color initialcolor=[Link];
Color color=[Link](this,"Select a
color",initialcolor);
[Link](color);
}
public static void main(String[] args)
{
ColorChooserExample ch=new ColorChooserExample();
[Link](400,400);
[Link](true);
[Link](EXIT_ON_CLOSE);
}
}
P a g e 5 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
Output:
[Link] a java program to perform ComboBox example.
Code:
package swing;
import [Link].*;
import [Link].*;
public class ComboBoxExample {
P a g e 6 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
final JLabel label = new JLabel();
[Link]([Link]);
[Link](400,100);
JButton b=new JButton("Show");
[Link](200,100,75,20);
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
[Link](50, 100,90,20);
[Link](cb); [Link](label); [Link](b);
[Link](null);
[Link](350,350);
[Link](true);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "
+ [Link]([Link]());
[Link](data);
}
});
}
public static void main(String[] args) {
P a g e 7 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
new ComboBoxExample();
}
}
Output:
[Link] a program to perform file Chooser example.
Code:
import [Link].*;
import [Link].*;
import [Link].*;
public class FileChooserExample extends JFrame implements
ActionListener{
JMenuBar mb;
JMenu file;
JMenuItem open;
JTextArea ta;
FileChooserExample(){
P a g e 8 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
open=new JMenuItem("Open File");
[Link](this);
file=new JMenu("File");
[Link](open);
mb=new JMenuBar();
[Link](0,0,800,20);
[Link](file);
ta=new JTextArea(800,800);
[Link](0,20,800,800);
add(mb);
add(ta);
}
public void actionPerformed(ActionEvent e) {
if([Link]()==open){
JFileChooser fc=new JFileChooser();
int i=[Link](this);
if(i==JFileChooser.APPROVE_OPTION){
File f=[Link]();
String filepath=[Link]();
try{
BufferedReader br=new BufferedReader(new FileReader(filepath));
String s1="",s2="";
while((s1=[Link]())!=null){
s2+=s1+"\n";
P a g e 9 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
}
[Link](s2);
[Link]();
}catch (Exception ex) {[Link](); }
}
}
}
public static void main(String[] args) {
FileChooserExample om=new FileChooserExample();
[Link](500,500);
[Link](null);
[Link](true);
[Link](EXIT_ON_CLOSE);
}
}
Output:
5. write a java program that contain panel and has button in pane.
Code:
import [Link].*;
P a g e 10 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
import [Link].*;
public class PanelExample {
PanelExample()
{
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
[Link](40,80,200,200);
[Link]([Link]);
JButton b1=new JButton("Button 1");
[Link](50,100,80,30);
[Link]([Link]);
JButton b2=new JButton("Button 2");
[Link](100,100,80,30);
[Link]([Link]);
[Link](b1); [Link](b2);
[Link](panel);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new PanelExample();
}
P a g e 11 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
}
Output:
6. write a program to perform Menu Bar that has Option like
copy,cut,paste.
Code:
import [Link].*;
import [Link].*;
public class MenuExample implements ActionListener{
JFrame f;
JMenuBar mb;
JMenu file,edit,help;
JMenuItem cut,copy,paste,selectAll;
JTextArea ta;
MenuExample(){
f=new JFrame();
cut=new JMenuItem("cut");
copy=new JMenuItem("copy");
paste=new JMenuItem("paste");
P a g e 12 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
selectAll=new JMenuItem("selectAll");
[Link](this);
[Link](this);
[Link](this);
[Link](this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
[Link](cut);[Link](copy);[Link](paste);[Link](selectAll);
[Link](file);[Link](edit);[Link](help);
ta=new JTextArea();
[Link](5,5,360,320);
[Link](mb);[Link](ta);
[Link](mb);
[Link](null);
[Link](400,400);
[Link](true);
}
public void actionPerformed(ActionEvent e) {
if([Link]()==cut)
[Link]();
if([Link]()==paste)
[Link]();
P a g e 13 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
if([Link]()==copy)
[Link]();
if([Link]()==selectAll)
[Link]();
}
public static void main(String[] args) {
new MenuExample();
}
}
Output:
7. write a java program that take User name as input in option
panel.
Code:
import [Link].*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();
P a g e 14 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
[Link](f,"Hello, Welcome to BMCCA.");
}
public static void main(String[] args) {
new OptionPaneExample();
}
}
Output:
8. write a java program to perform file pop-up menu example
Code:
import [Link].*;
import [Link].*;
class PopupMenuExample
{
PopupMenuExample(){
final JFrame f= new JFrame("PopupMenu Example");
final JLabel label = new JLabel();
[Link]([Link]);
[Link](400,100);
final JPopupMenu popupmenu = new JPopupMenu("Edit");
JMenuItem cut = new JMenuItem("Cut");
P a g e 15 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
JMenuItem copy = new JMenuItem("Copy");
JMenuItem paste = new JMenuItem("Paste");
[Link](cut); [Link](copy);
[Link](paste);
[Link](new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
[Link](f , [Link](), [Link]());
}
});
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e) {
[Link]("cut MenuItem clicked.");
}
});
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e) {
[Link]("copy MenuItem clicked.");
}
});
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e) {
[Link]("paste MenuItem clicked.");
}
P a g e 16 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
});
[Link](label); [Link](popupmenu);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new PopupMenuExample();
}
}
Output:
9. write a program to perform layerd pane example.
Code:
import [Link].*;
import [Link].*;
public class LayeredPaneExample extends JFrame {
public LayeredPaneExample() {
super("LayeredPane Example");
setSize(400, 400);
P a g e 17 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
JLayeredPane pane = getLayeredPane();
//creating buttons
JButton top = new JButton();
[Link]([Link]);
[Link](20, 20, 100, 100);
JButton middle = new JButton();
[Link]([Link]);
[Link](40, 40, 100, 100);
JButton bottom = new JButton();
[Link]([Link]);
[Link](60, 60, 100, 100);
//adding buttons on pane
[Link](bottom, new Integer(1));
[Link](middle, new Integer(2));
[Link](top, new Integer(3));
}
public static void main(String[] args) {
LayeredPaneExample panel = new LayeredPaneExample();
[Link](true);
}
}
Output:
P a g e 18 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
[Link] a java program to make registration form that contain
textfield,check box,radio button,combobox,password field and on
submit enter data display in optional panel.
Code:
import [Link].*;
import [Link].*;
import [Link];
import [Link];
public class RegistrationForm extends JFrame implements
ActionListener {
// Components
private JTextField firstNameField, lastNameField;
private JCheckBox agreementCheckBox;
private JRadioButton maleRadioButton, femaleRadioButton;
private ButtonGroup genderGroup;
private JComboBox<String> courseComboBox;
private JPasswordField passwordField;
private JButton submitButton;
P a g e 19 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
private JTextArea displayArea;
// Constructor
public RegistrationForm() {
// Frame setup
setTitle("Registration Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// Panel for form elements
JPanel formPanel = new JPanel();
[Link](new GridLayout(8, 2));
// Components initialization
JLabel firstNameLabel = new JLabel("First Name:");
firstNameField = new JTextField();
JLabel lastNameLabel = new JLabel("Last Name:");
lastNameField = new JTextField();
JLabel agreementLabel = new JLabel("Agreement:");
agreementCheckBox = new JCheckBox("I agree to the terms and
conditions");
JLabel genderLabel = new JLabel("Gender:");
P a g e 20 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
maleRadioButton = new JRadioButton("Male");
femaleRadioButton = new JRadioButton("Female");
genderGroup = new ButtonGroup();
[Link](maleRadioButton);
[Link](femaleRadioButton);
JLabel courseLabel = new JLabel("Course:");
String[] courses = {"computer Engineering", "Medical",
"Commerce", "Arts"};
courseComboBox = new JComboBox<>(courses);
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
submitButton = new JButton("Submit");
[Link](this);
// Adding components to the form panel
[Link](firstNameLabel);
[Link](firstNameField);
[Link](lastNameLabel);
[Link](lastNameField);
[Link](agreementLabel);
[Link](agreementCheckBox);
[Link](genderLabel);
[Link](maleRadioButton);
[Link](new JLabel()); // Empty label for alignment
P a g e 21 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
[Link](femaleRadioButton);
[Link](courseLabel);
[Link](courseComboBox);
[Link](passwordLabel);
[Link](passwordField);
[Link](new JLabel()); // Empty label for alignment
[Link](submitButton);
// Optional panel to display entered data
JPanel displayPanel = new JPanel();
[Link](new BorderLayout());
displayArea = new JTextArea(10, 20);
[Link](false);
JScrollPane scrollPane = new JScrollPane(displayArea);
[Link](scrollPane, [Link]);
// Adding panels to the frame
add(formPanel, [Link]);
add(displayPanel, [Link]);
setVisible(true);
}
// Action performed when submit button is clicked
P a g e 22 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
@Override
public void actionPerformed(ActionEvent e) {
if ([Link]() == submitButton) {
// Get values from the form fields
String firstName = [Link]();
String lastName = [Link]();
String agreement = [Link]() ?
"Agreed" : "Not Agreed";
String gender = [Link]() ? "Male" :
"Female";
String course = (String) [Link]();
String password = new String([Link]());
// Display entered data in the text area
[Link]("First Name: " + firstName + "\n" +
"Last Name: " + lastName + "\n" +
"Agreement: " + agreement + "\n" +
"Gender: " + gender + "\n" +
"Course: " + course + "\n" +
"Password: " + password);
}
}
// Main method to start the application
P a g e 23 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java
public static void main(String[] args) {
[Link](RegistrationForm::new);
}
}
Output:
P a g e 24 | 24