Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Adv Java-problemsheet-1

The document contains a series of Java programming exercises focused on GUI components using Swing. It includes code examples for checkboxes, color chooser, combo boxes, file chooser, panels, menus, option panes, pop-up menus, layered panes, and a registration form. Each section provides the code and a brief description of the functionality implemented.

Uploaded by

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

Adv Java-problemsheet-1

The document contains a series of Java programming exercises focused on GUI components using Swing. It includes code examples for checkboxes, color chooser, combo boxes, file chooser, panels, menus, option panes, pop-up menus, layered panes, and a registration form. Each section provides the code and a brief description of the functionality implemented.

Uploaded by

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

Name: Krupali Usadadiya

Enrollment No: 2202040601098 Subject: Advance Java

Bhagwan Mahavir College of Computer Application (BCA)


M.Sc.IT(Integrated) SEM - 4
Advance java - Problem Sheet 1
1. write a java program to perform checkbox example.
Code:
import javax.swing.*;
import java.awt.event.*;
public class CheckBoxExample extends JFrame implements
ActionListener
{
JLabel l;
JCheckBox cb1,cb2,cb3;
JButton b;
CheckBoxExample ()
{
l=new JLabel("food ordering System");
l.setBounds(50,50,300,20);
cb1 = new JCheckBox("pizza @100");
cb1.setBounds(100,100,150,20);
cb2 = new JCheckBox("burger @50");
cb2.setBounds(100,150,150,20);
cb3 = new JCheckBox("tea @10");
cb3.setBounds(100,200,150,20);
b=new JButton("order");

P a g e 1 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

b.setBounds(100,250,80,30);
b.addActionListener(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(cb1.isSelected())
{
amount+=100;
msg="pizza:100\n";
}
if(cb2.isSelected())

P a g e 2 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

{
amount+=30;
msg="Burger:50\n";
}
if(cb3.isSelected())
{
amount+=10;
msg="tea:10\n";
}
msg+="---------------\n";
JOptionPane.showMessageDialog(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

2.write a java program to perform ColorChooser example


Code:
package swing;

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

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();
c.setLayout(new FlowLayout());
b=new JButton("color");
b.addActionListener(this);
c.add(b);
}
public void actionPerformed(ActionEvent e)
{
Color initialcolor=Color.RED;
Color color=JColorChooser.showDialog(this,"Select a
color",initialcolor);
c.setBackground(color);
}

public static void main(String[] args)


{
ColorChooserExample ch=new ColorChooserExample();
ch.setSize(400,400);
ch.setVisible(true);
ch.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}

P a g e 5 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

Output:

3.write a java program to perform ComboBox example.


Code:
package swing;

import javax.swing.*;
import java.awt.event.*;
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();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(400,100);
JButton b=new JButton("Show");
b.setBounds(200,100,75,20);
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
cb.setBounds(50, 100,90,20);
f.add(cb); f.add(label); f.add(b);
f.setLayout(null);
f.setSize(350,350);
f.setVisible(true);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = "Programming language Selected: "
+ cb.getItemAt(cb.getSelectedIndex());
label.setText(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:

4.write a program to perform file Chooser example.


Code:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
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");


open.addActionListener(this);
file=new JMenu("File");
file.add(open);
mb=new JMenuBar();
mb.setBounds(0,0,800,20);
mb.add(file);
ta=new JTextArea(800,800);
ta.setBounds(0,20,800,800);
add(mb);
add(ta);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==open){
JFileChooser fc=new JFileChooser();
int i=fc.showOpenDialog(this);
if(i==JFileChooser.APPROVE_OPTION){
File f=fc.getSelectedFile();
String filepath=f.getPath();
try{
BufferedReader br=new BufferedReader(new FileReader(filepath));
String s1="",s2="";
while((s1=br.readLine())!=null){
s2+=s1+"\n";
P a g e 9 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

}
ta.setText(s2);
br.close();
}catch (Exception ex) {ex.printStackTrace(); }
}
}
}
public static void main(String[] args) {
FileChooserExample om=new FileChooserExample();
om.setSize(500,500);
om.setLayout(null);
om.setVisible(true);
om.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Output:

5. write a java program that contain panel and has button in pane.
Code:
import java.awt.*;
P a g e 10 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

import javax.swing.*;
public class PanelExample {
PanelExample()
{
JFrame f= new JFrame("Panel Example");
JPanel panel=new JPanel();
panel.setBounds(40,80,200,200);
panel.setBackground(Color.gray);
JButton b1=new JButton("Button 1");
b1.setBounds(50,100,80,30);
b1.setBackground(Color.yellow);
JButton b2=new JButton("Button 2");
b2.setBounds(100,100,80,30);
b2.setBackground(Color.green);
panel.add(b1); panel.add(b2);
f.add(panel);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(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 javax.swing.*;
import java.awt.event.*;
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");
cut.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
selectAll.addActionListener(this);
mb=new JMenuBar();
file=new JMenu("File");
edit=new JMenu("Edit");
help=new JMenu("Help");
edit.add(cut);edit.add(copy);edit.add(paste);edit.add(selectAll);
mb.add(file);mb.add(edit);mb.add(help);
ta=new JTextArea();
ta.setBounds(5,5,360,320);
f.add(mb);f.add(ta);
f.setJMenuBar(mb);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==cut)
ta.cut();
if(e.getSource()==paste)
ta.paste();
P a g e 13 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

if(e.getSource()==copy)
ta.copy();
if(e.getSource()==selectAll)
ta.selectAll();
}
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 javax.swing.*;
public class OptionPaneExample {
JFrame f;
OptionPaneExample(){
f=new JFrame();

P a g e 14 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

JOptionPane.showMessageDialog(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 javax.swing.*;
import java.awt.event.*;
class PopupMenuExample
{
PopupMenuExample(){
final JFrame f= new JFrame("PopupMenu Example");
final JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setSize(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");
popupmenu.add(cut); popupmenu.add(copy);
popupmenu.add(paste);

f.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
popupmenu.show(f , e.getX(), e.getY());
}
});
cut.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
label.setText("cut MenuItem clicked.");
}
});
copy.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
label.setText("copy MenuItem clicked.");
}
});
paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
label.setText("paste MenuItem clicked.");
}

P a g e 16 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

});
f.add(label); f.add(popupmenu);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new PopupMenuExample();
}
}
Output:

9. write a program to perform layerd pane example.


Code:
import javax.swing.*;
import java.awt.*;
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();
top.setBackground(Color.white);
top.setBounds(20, 20, 100, 100);
JButton middle = new JButton();
middle.setBackground(Color.red);
middle.setBounds(40, 40, 100, 100);
JButton bottom = new JButton();
bottom.setBackground(Color.cyan);
bottom.setBounds(60, 60, 100, 100);
//adding buttons on pane
pane.add(bottom, new Integer(1));
pane.add(middle, new Integer(2));
pane.add(top, new Integer(3));
}
public static void main(String[] args) {
LayeredPaneExample panel = new LayeredPaneExample();
panel.setVisible(true);
}
}
Output:

P a g e 18 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

10.write 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 javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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();
formPanel.setLayout(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();
genderGroup.add(maleRadioButton);
genderGroup.add(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");
submitButton.addActionListener(this);

// Adding components to the form panel


formPanel.add(firstNameLabel);
formPanel.add(firstNameField);
formPanel.add(lastNameLabel);
formPanel.add(lastNameField);
formPanel.add(agreementLabel);
formPanel.add(agreementCheckBox);
formPanel.add(genderLabel);
formPanel.add(maleRadioButton);
formPanel.add(new JLabel()); // Empty label for alignment

P a g e 21 | 24
Name: Krupali Usadadiya
Enrollment No: 2202040601098 Subject: Advance Java

formPanel.add(femaleRadioButton);
formPanel.add(courseLabel);
formPanel.add(courseComboBox);
formPanel.add(passwordLabel);
formPanel.add(passwordField);
formPanel.add(new JLabel()); // Empty label for alignment
formPanel.add(submitButton);

// Optional panel to display entered data


JPanel displayPanel = new JPanel();
displayPanel.setLayout(new BorderLayout());
displayArea = new JTextArea(10, 20);
displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(displayArea);
displayPanel.add(scrollPane, BorderLayout.CENTER);

// Adding panels to the frame


add(formPanel, BorderLayout.NORTH);
add(displayPanel, BorderLayout.CENTER);

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 (e.getSource() == submitButton) {
// Get values from the form fields
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
String agreement = agreementCheckBox.isSelected() ?
"Agreed" : "Not Agreed";
String gender = maleRadioButton.isSelected() ? "Male" :
"Female";
String course = (String) courseComboBox.getSelectedItem();
String password = new String(passwordField.getPassword());

// Display entered data in the text area


displayArea.setText("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) {


SwingUtilities.invokeLater(RegistrationForm::new);
}
}
Output:

P a g e 24 | 24

You might also like