0% found this document useful (0 votes)
89 views22 pages

Java Swing GUI: ScrollPane & Layouts

The document contains code snippets for several Java GUI programming practical examples using Swing and AWT. The examples demonstrate using different layout managers like FlowLayout, GridLayout, and BorderLayout. They also show event handling using ActionListener, MouseListener, WindowAdapter, and anonymous inner classes. The code snippets are accompanied by short descriptions of the program aims and expected outputs.

Uploaded by

commerce
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)
89 views22 pages

Java Swing GUI: ScrollPane & Layouts

The document contains code snippets for several Java GUI programming practical examples using Swing and AWT. The examples demonstrate using different layout managers like FlowLayout, GridLayout, and BorderLayout. They also show event handling using ActionListener, MouseListener, WindowAdapter, and anonymous inner classes. The code snippets are accompanied by short descriptions of the program aims and expected outputs.

Uploaded by

commerce
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

Practical no-4

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

insert into [Link] values(1, 'dove' , 20);

insert into [Link] values(2, 'sunsilk' , 60);

insert into [Link] values(3, 'himalya' , 90);


Code--
package jdbc;

import [Link];

import [Link];

import [Link];

import [Link];

public class Jdbc {

public static void main(String[] args) {

try

[Link]("[Link]");

[Link]("driver rigisted successfuly");

Connection con=[Link]("jdbc:derby://localhost:1527/java [java on


JAVA]");

[Link]("database connection successfulyy");

Statement stmt=[Link]();

ResultSet rs=[Link]("select * from [Link];");

while([Link]())

[Link]([Link](1) + "\t" + [Link](2) + "\t" + [Link](3));

catch(Exception e)

[Link](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 [Link].*;
import [Link].*;
import [Link];
import [Link];
public class ScrollPaneColorDemo {
public static void main(String[] args) {
[Link](() -> {
createAndShowGUI();
});
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ScrollPane Color Demo");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](400, 300);

JTextArea textArea = new JTextArea(10, 30);


JScrollPane scrollPane = new JScrollPane(textArea);

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


[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Color selectedColor = [Link](frame, "Choose Background Color",
[Link]);
if (selectedColor != null) {
[Link]().setBackground(selectedColor);
}
}
});

JPanel panel = new JPanel();


[Link](scrollPane);
[Link](colorButton);

[Link](panel);
[Link](true);
}
}

Output—
Practical no-6
A)Aim :- Write a program for the Border Layout.
Code--

package borderex;

import [Link].*;

import [Link].*;

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

[Link](b1);

[Link](b2);

[Link](b3);

[Link](b4);

[Link](new FlowLayout());

[Link](500,600);

[Link](true);

public static void main(String[] args)

new FlowLayoutex();

} }

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

import [Link].*;

import [Link].*;

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

[Link] (btn1);

[Link] (btn2);

[Link] (btn3);

[Link] (btn4);

[Link] (btn5);

[Link] (btn6);

[Link](new GridLayout());

[Link](300, 300);

[Link](true);

public static void main(String[] args)

{
new GridLayoutex ();

Output--
C)Aim :- Write a program for the Border Layout.
Code—
package border;
import [Link].*;
import [Link].*;
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");
[Link](b1,[Link]);
[Link](b2,[Link]);
[Link](b3,[Link]);
[Link](b4,[Link]);
[Link](b5,[Link]);
[Link](300,300);
[Link](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 [Link].*;
import [Link].*;
public class Main {
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
[Link](50,50, 150,20);
Button b=new Button("Click Here");
[Link](50,100,60,30);
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
[Link]("Welcome to Javatpoint.");
}
});
[Link](b);[Link](tf);
[Link](400,400);
[Link](null);
[Link](true);
}
}
Output--

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


Code--
package borderex;

import [Link].*;

import [Link].*;

public class MouseListenerex extends Frame implements MouseListener {

Label i;

MouseListenerex(){

addMouseListener(this);

i=new Label();

PopupMenu l;

[Link](20,25,100,20);

add(i);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e) {

[Link]("Mouse Clicked");

public void mouseEntered(MouseEvent e) {

[Link]("Mouse Entered");

public void mouseExited(MouseEvent e) {

[Link]("Mouse Exited");

public void mousePressed(MouseEvent e) {

[Link]("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

[Link]("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 [Link].*;

import [Link].*;

public class AdapterExample {

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

AdapterExample(){

[Link](new WindowAdapter(){

public void windowClosing (WindowEvent e){

});

[Link](400,400);

[Link] (null);

[Link](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 [Link].*;
import [Link].*;
import [Link].*;

public class ButtonDemo {


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

JPanel panel = new JPanel();


JButton blueButton = new JButton("Blue");
[Link](blueButton);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
[Link]([Link]);
}
});

[Link](panel);
[Link]();
[Link](true);
}
}
Output--

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


Source code—
import [Link].*;
import [Link].*;
public class Main {
public static void main(String[] args) {
Frame f=new Frame("ActionListener Example");
final TextField tf=new TextField();
[Link](50,50, 150,20);
Button b=new Button("Click Here");
[Link](50,100,60,30);
[Link](new ActionListener(){
public void actionPerformed(ActionEvent e){
[Link]("Welcome to Javatpoint.");
}
});
[Link](b);[Link](tf);
[Link](400,400);
[Link](null);
[Link](true);
}
}

Output--

You might also like