Java Pract 233

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

Practical no-4

Aim—Write a JDBC program that display the data of given table in a GUI
table.
Code--
select * from JAVA.PRODUCT;

insert into JAVA.PRODUCT values(1, 'dove' , 20);

insert into JAVA.PRODUCT values(2, 'sunsilk' , 60);

insert into JAVA.PRODUCT values(3, 'himalya' , 90);


Code--
package jdbc;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

public class Jdbc {

public static void main(String[] args) {

try

Class.forName("org.apache.derby.jdbc.ClientDriver");

System.out.println("driver rigisted successfuly");

Connection con=DriverManager.getConnection("jdbc:derby://localhost:1527/java [java on


JAVA]");

System.out.println("database connection successfulyy");

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery("select * from JAVA.PRODUCT;");

while(rs.next())

System.out.println(rs.getInt(1) + "\t" + rs.getString(2) + "\t" + rs.getInt(3));

catch(Exception e)

System.out.println(e);

}
Output--
Practical no-5
Aim—Create a swing Application to demonstrate use of scrollpane to change its
color selected using color chooser.
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);
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
A)Aim :- Write a program for the Border Layout.
Code--

package borderex;

import java.awt.*;

import javax.swing.*;

public class FlowLayoutex {

FlowLayoutex()

JFrame f=new JFrame();

JButton b1=new JButton("1");

JButton b2=new JButton("2");

JButton b3=new JButton("3");

JButton b4=new JButton("4");

f.add(b1);

f.add(b2);

f.add(b3);

f.add(b4);

f.setLayout(new FlowLayout());

f.setSize(500,600);

f.setVisible(true);

public static void main(String[] args)

new FlowLayoutex();

} }

Output—
B)Aim :- Write a program for the Grid Layout.
Code-
package border1;

import java.awt.*;

import javax.swing.*;

public class GridLayoutex {

JFrame f = new JFrame();

GridLayoutex()

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");

JButton btn6 = new JButton("6");

f.add (btn1);

f.add (btn2);

f.add (btn3);

f.add (btn4);

f.add (btn5);

f.add (btn6);

f.setLayout(new GridLayout());

f.setSize(300, 300);

f.setVisible(true);

public static void main(String[] args)

{
new GridLayoutex ();

Output--
C)Aim :- Write a program for the Border Layout.
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();
}
}
Output--
Practical no-7
A)Aim :- Write a program to demonstrate the Action Event.
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--

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


Code--
package borderex;

import java.awt.*;

import java.awt.event.*;

public class MouseListenerex extends Frame implements MouseListener {

Label i;

MouseListenerex(){

addMouseListener(this);

i=new Label();

PopupMenu l;

i.setBounds(20,25,100,20);

add(i);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e) {

i.setText("Mouse Clicked");

public void mouseEntered(MouseEvent e) {

i.setText("Mouse Entered");

public void mouseExited(MouseEvent e) {

i.setText("Mouse Exited");

public void mousePressed(MouseEvent e) {

i.setText("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

i.setText("Mouse Released");
}

public static void main(String[] args) {

new MouseListenerex();

Output--

Practical no-8
Aim :- Demonstrate the use of Adapter Class in Event Handling.
Code--
package border12;

import java.awt.*;

import java.awt.event.*;

public class AdapterExample {

Frame f = new Frame("Hema Thodji");

AdapterExample(){

f.addWindowListener(new WindowAdapter(){

public void windowClosing (WindowEvent e){

});

f.setSize(400,400);

f.setLayout (null);

f.setVisible(true);

public static void main(String[] args) {

new AdapterExample();

Output—
Practical No- 9
Aim :- Demonstrate the use of Anonymous Inner Class in Event
Handling.
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);
}
}
Output--

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


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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy