Java PR Final
Java PR Final
Java PR Final
CERTIFICATE
INDEX
ROLL NO.
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.*;
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();
}
frame.add(scrollPane);
frame.setVisible(true);
}
}
Output—
ROLL NO.
Practical no 5
ROLL NO.
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);
if (selectedColor != null) {
scrollPane.getViewport().setBackground(selectedColor);
}
}
});
frame.add(panel);
frame.setVisible(true);
}
}
Output—
Practical no 6
ROLL NO.
Source code—
package border1;
import java.awt.*;
import javax.swing.*;
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.
Output—
Source code—
package border1;
import java.awt.*;
import javax.swing.*;
f.add(btn2);
f.add(btn3);
f.add(btn4);
f.add(btn5);
f.setLayout(new GridLayout());
f.setSize(300,300);
f.setVisible(true);
}
Output—
Source code—
package border;
import java.awt.*;
import javax.swing.*;
Output--
Practical no 7
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.
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
Source code—
import java.awt.*;
import java.awt.event.*;
Frame f;
AdapterExample() {
f = new Frame ("Window Adapter");
f.addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent e) {
f.dispose();
}
});
Output--
ROLL NO.
Practical no 9
Source code—
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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--