Java PR Final

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

ROLL NO.

Rajiv Gandhi College of Arts, Commerce & Science


(Permanently Affiliated to University of Mumbai)

DEPARTMENT OF INFORMATION TECHNOLOGY


&
COMPUTER SCIENCE

CERTIFICATE

This is certify that Mr./Miss______________________________ of the


S.Y.B.Sc(IT)class with seat no._____________, has satisfactorily
completed the required number of Practical’s in the
subject_____________ as per syllabus laid down by the
University of Mumbai
Academic year 2023-2024

Subject in-charge Co-Ordinator


_____________ ________________
External Examiner
_____________

INDEX
ROLL NO.

Sr.No Name of the Practical Sign

Practical no 4
ROLL NO.

Aim :- write a JDBC program that display the data of given table in
Gui table.

Source code—

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;

public class JDBCTableDisplay {


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}

private static void createAndShowGUI() {


JFrame frame = new JFrame("Table Data Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

DefaultTableModel tableModel = new DefaultTableModel();

tableModel.addColumn("ID");
tableModel.addColumn("Name");
tableModel.addColumn("Age");

try {
ROLL NO.

Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable");

while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
tableModel.addRow(new Object[]{id, name, age});
}

resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}

JTable table = new JTable(tableModel);

JScrollPane scrollPane = new JScrollPane(table);

frame.add(scrollPane);

frame.setVisible(true);
}
}

Output—
ROLL NO.

Practical no 5
ROLL NO.

Aim :- create a swing application to demonstrate use of scroll Pane to


change its color selected using color chooser.

Source code—
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScrollPaneColorDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGUI();
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ScrollPane Color Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);

JTextArea textArea = new JTextArea(10, 30);


JScrollPane scrollPane = new JScrollPane(textArea);

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


colorButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color selectedColor = JColorChooser.showDialog(frame, "Choose Background Color",
Color.WHITE);
ROLL NO.

if (selectedColor != null) {
scrollPane.getViewport().setBackground(selectedColor);
}
}
});

JPanel panel = new JPanel();


panel.add(scrollPane);
panel.add(colorButton);

frame.add(panel);
frame.setVisible(true);
}
}

Output—

Practical no 6
ROLL NO.

a) Aim :- Write a program for the Flow Layout.

Source code—

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

public class FlowLayoutExample {

FlowLayoutExample()
{
JFrame f = new JFrame();
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
JButton b4 = new JButton("4");
JButton b5 = new JButton("5");
f.add(b1);

f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(new FlowLayout());
f.setSize(300,300);
f.setVisible(true);
ROLL NO.

public static void main(String[] args) {


new FlowLayoutExample();
}

Output—

b) Aim :- Write a program for the Grid Layout.


ROLL NO.

Source code—

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

public class GridLayoutExample {


JFrame f = new JFrame();
GridLayoutExample()
{
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
f.add(btn1);

f.add(btn2);
f.add(btn3);
f.add(btn4);
f.add(btn5);
f.setLayout(new GridLayout());
f.setSize(300,300);
f.setVisible(true);
}

public static void main(String[] args) {


new GridLayoutExample();
ROLL NO.

Output—

c) Aim :- Write a program for the Border Layout.


ROLL NO.

Source code—

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

public class Border {


JFrame f = new JFrame();
Border()
{
JButton b1 = new JButton("NORTH");
JButton b2 = new JButton("SOUTH");
JButton b3 = new JButton("EAST");
JButton b4 = new JButton("WEST");
JButton b5 = new JButton("CENTER");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}

public static void main(String[] args) {


new Border();
}
ROLL NO.

Output--

Practical no 7

a) Aim :- Write a program to demonstrate the Action Event.


ROLL NO.

Source code—

import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
tf.setBounds(50,50, 150,20);
Button b=new Button("Click Here");
b.setBounds(50,100,60,30);

b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}

Output--
ROLL NO.

b) Aim :- Write a program to demonstrate the Mouse Event.

Source code—
ROLL NO.

import java.awt.*;
import java.awt.event.*;
public class MouseListenerExample extends Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);

l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
ROLL NO.

l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}

Output--

Practical no 8

Aim :- Demonstrate the use of Adapter Class in Event Handling.


ROLL NO.

Source code—

import java.awt.*;
import java.awt.event.*;

public class AdapterExample {

Frame f;
AdapterExample() {
f = new Frame ("Window Adapter");
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});

f.setSize (400, 400);


f.setLayout (null);
f.setVisible (true);
}

public static void main(String[] args) {


new AdapterExample();
}
}

Output--
ROLL NO.

Practical no 9

Aim :- Demonstrate the use of Anonymous Inner Class in Event


Handling
ROLL NO.

Source code—

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

public class ButtonDemo {


public static void main(String[] args) {
JFrame frame = new JFrame("Button Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();


JButton blueButton = new JButton("Blue");
panel.add(blueButton);

blueButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.setBackground(Color.BLUE);
}
});

frame.add(panel);
frame.pack();
frame.setVisible(true);
}
ROLL NO.

Output--

You might also like