1.
Write GUI programs for the following Swing Components
A. Java JButton
CODE:- OUTPUT:-
import [Link].*;
public class JButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JButton Example");
JButton button = new JButton("Click Me");
[Link](50, 50, 100, 40);
[Link](button);
[Link](200, 150);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
B. Java JLabel
CODE:-
import [Link].*; OUTPUT:-
public class JLabelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JLabel Example");
JLabel label = new JLabel("Hello, World!");
[Link](50, 50, 200, 30);
[Link](label);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
C. Java JTextField
CODE:- OUTPUT:-
import [Link].*;
public class JTextFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Example");
JTextField textField = new JTextField();
[Link](50, 50, 150, 30);
[Link](textField);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
D. Java TextArea
CODE:- OUTPUT:-
import [Link].*;
public class JTextAreaExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextArea Example");
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
[Link](50, 50, 300, 200);
[Link](scrollPane);
[Link](400, 300);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);}
}
E. JPasswordFieldExample
CODE:- OUTPUT:-
import [Link].*;
public class JPasswordFieldExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPasswordField Example");
JPasswordField passwordField = new JPasswordField();
[Link](50, 50, 150, 30);
[Link](passwordField);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
F. JcheckBox
CODE:- OUTPUT:-
import [Link].*;
public class JCheckboxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JCheckBox Example");
JCheckBox checkBox = new JCheckBox("Check me");
[Link](50, 50, 150, 30);
[Link](checkBox);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);}
}
G. Java JradioButton
CODE:-
import [Link].*;
import [Link];
import [Link].*;
public class JRadioButtonExample {
public static void main(String[] args) {
[Link](() -> {
JFrame frame = new JFrame("JRadioButton Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
JPanel contentPane = new JPanel();
[Link](new EmptyBorder(10, 10, 10, 10));
[Link](new GridLayout(1, 1));
[Link](contentPane);
JRadioButton radioButton1 = new JRadioButton("Option 1");
JRadioButton radioButton2 = new JRadioButton("Option 2");
ButtonGroup group = new ButtonGroup();
[Link](radioButton1);
[Link](radioButton2);
[Link](radioButton1);
[Link](radioButton2);
[Link](true);
});
}
OUTPUT:-
H. Java JComboBox
CODE:- OUTPUT:-
import [Link].*;
public class JComboBoxExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
String[] options = {"Option 1", "Option 2", "Option 3"};
JComboBox<String> comboBox = new JComboBox<>(options);
[Link](50, 50, 150, 30);
[Link](comboBox);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
I. Java JList
CODE:- OUTPUT:-
import [Link].*;
public class JListExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JList Example");
String[] data = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
JList<String> list = new JList<>(data);
[Link](50, 50, 150, 100);
[Link](list);
[Link](250, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);}
}
J. Java MenuItem & JMenuJava JPopupMenu
CODE:- OUTPUT:-
import [Link].*;
public class JMenuItemJMenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JMenuItem and JMenu Example");
JMenu menu = new JMenu("File");
JMenuItem menuItem1 = new JMenuItem("Open");
JMenuItem menuItem2 = new JMenuItem("Save");
JMenuItem menuItem3 = new JMenuItem("Exit");
[Link](menuItem1);
[Link](menuItem2);
[Link](menuItem3);
JMenuBar menuBar = new JMenuBar();
[Link](menu);
[Link](menuBar);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
Java PopMenuExample
CODE:-
import [Link].*;
public class JPopupMenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JPopupMenu Example");
JPopupMenu popupMenu = new JPopupMenu();
JMenuItem menuItem1 = new JMenuItem("Copy");
JMenuItem menuItem2 = new JMenuItem("Paste");
[Link](menuItem1);
[Link](menuItem2);
[Link](new [Link]() {
public void mouseClicked([Link] evt) {
[Link](frame, [Link](), [Link]());
}
});
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
OUTPUT:-
K. Java JcolorChooser
CODE:- OUTPUT:-
import [Link].*;
import [Link].*;
import [Link].*;
public class JColorChooserDemo extends JFrame {
JPanel panel;
Color bgColor = Color.LIGHT_GRAY;
public JColorChooserDemo() {
panel = new JPanel(new BorderLayout());
JButton btnColor = new JButton("Change Color");
[Link](btnColor,[Link]);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Color color = [Link]([Link],
"Choose a color", bgColor);
if (color != null) { // new color selected
bgColor = color;
}
[Link](bgColor);
}
});
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JColorChooser Demo");
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new JColorChooserDemo();
}
});
}
}
L. Java JtabbedPane
CODE:-
import [Link];
import [Link].*;
public class JTabbedPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTabbedPane Example");
[Link](JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
[Link](new JLabel("This is Panel 1"));
[Link](new JLabel("This is Panel 2"));
[Link]("Tab 1", panel1);
[Link]("Tab 2", panel2);
[Link](new BorderLayout()); // Set the layout manager
[Link](tabbedPane, [Link]); // Add the tabbed pane to the center
of the frame
[Link](300, 200);
[Link](true);
}
}
OUTPUT:-
[Link] JSlider
CODE:- OUTPUT:-
import [Link].*;
public class JSliderExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JSlider Example");
JSlider slider = new JSlider([Link], 0, 100, 50);
[Link](5);
[Link](20);
[Link](true);
[Link](true);
JPanel panel = new JPanel();
[Link](slider);
[Link](panel);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}
N. JFileChooser
CODE:- OUTPUT:-
import [Link].*;
import [Link].*;
public class JFileChooserExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JFileChooser Example");
JButton button = new JButton("Open File Chooser");
[Link](50, 50, 200, 30);
[Link](button);
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = [Link](null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
[Link](null, "Selected file: " +
[Link]().getName());
} } });
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}}
O. JavaToggleButton
CODE:- OUTPUT:-
import [Link].*;
public class JToggleButtonExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JToggleButton Example");
JToggleButton toggleButton = new JToggleButton("Toggle Me");
[Link](50, 50, 150, 30);
[Link](toggleButton);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}
}:
P. Java JToolBar
CODE:- OUTPUT:-
import [Link].*;
public class JToolBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JToolBar Example");
JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Button 2");
JToolBar toolBar = new JToolBar();
[Link](button1);
[Link](button2);
[Link](toolBar);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);
}}
Q. Java JprogressBar
CODE:- OUTPUT:-
import [Link].*;
public class JProgressBarExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JProgressBar Example");
JProgressBar progressBar = new JProgressBar();
[Link](50);
[Link](true);
[Link](progressBar);
[Link](300, 200);
[Link](null);
[Link](true);
[Link](JFrame.EXIT_ON_CLOSE);}}
2. Write a JAVA Swing program using event Handling A
Frame with three buttons Red blue and green and change
the background color according to button pressed by user.
CODE:-
import [Link].*;
import [Link].*;
import [Link].*;
public class mycolor implements ActionListener
{
Button b1,b2,b3,b4;
Container cn;
JFrame f;
mycolor()
{
f = new JFrame("Event Handling");
cn = [Link]();
[Link](true);
[Link](500,500);
b1=new Button("RED");
b2=new Button("BLUE");
b3=new Button("GREEN");
b4=new Button("YELLOW");
[Link](b1);[Link](b2);[Link](b3);[Link](b4);
[Link](null);
[Link](100,100,100,50);
[Link](100,200,100,50);
[Link](100,300,100,50);
[Link](100,400,100,50);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()=="RED")
{
[Link]([Link]);
}
else if([Link]()=="BLUE")
{
[Link]([Link]);
}
else if([Link]()=="GREEN")
{
[Link]([Link]);
}
else
{ [Link]([Link]);
}
}
public static void main(String[] args) {
mycolor mm = new mycolor();
}
}
OUTPUT:-
3. Design A Login Window as shown below and make it Active.
If user id and password are correct then design of question
4 should open. If password are incorrect then invalid
password message should be displayed.
CODE:- OUTPUT:-
import [Link].*;
import [Link].*;
import [Link].*;
public class login implements ActionListener
{
JButton b1,b2;
TextField t1,t2;
Label l1,l2;
JFrame f;
Container cn;
login()
{
f = new JFrame ("LoGiN PaGe");
[Link](true);
[Link](350,350);
cn = [Link]();
[Link](new Color(153,0,153));
b1 = new JButton("OK");
b2 = new JButton("Cancel");
[Link](b1); [Link](b2);
[Link](null);
[Link](50,190,100,50);
[Link](200,190,100,50);
[Link]([Link]);
[Link]([Link]);
l1 = new Label("UserName");
[Link](l1);
[Link](30,65,150,50);
t1 = new TextField();
[Link](t1);
[Link](180,70,130,30);
l2 = new Label("Password");
[Link](l2);
[Link](30,115,150,50);
t2 = new TextField();
[Link](t2);
[Link](180,120,130,30);
[Link]([Link]);
[Link]([Link]);
Font ft = new Font("ARIAL",[Link]+[Link],18);
[Link](ft);
[Link](ft);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()== "OK")
{
if([Link]().equals("sumit") && [Link]().equals("sumit"))
{factinfo info = new factinfo();
[Link](true);}
else{
[Link]();
}
public static void main(String args[])
{
new login();
}}
4. Design Following Design Frame Using Swing Components
CODE:-
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
import [Link];
public class factinfo extends Frame implements ActionListener {
Label L, L1, l1, l2, l3, l4, l5, l6, l7, l8;
Button b1, b2, b3, b4, b5, b6, b8, b9, b0;
static TextField t1, t2, t3, t4, t5, t6, t7, t8;
Toolkit tk;
Connection cn;
factinfo() {
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
} });
tk = [Link]();
setSize([Link]());
setBackground(new Color(255, 255, 220));
setLayout(null);
L = new Label("ITM UNIVERSITY GWALIOR 2024 FACULTY Information System");
add(L);
[Link](60, 50, 650, 25);
l1 = new Label("Faculty ID");
add(l1);
[Link](50, 150, 100, 25);
t1 = new TextField();
add(t1);
[Link](150, 150, 100, 25);
l2 = new Label("Faculty Name");
add(l2);
[Link](300, 150, 100, 25);
t2 = new TextField();
add(t2);
[Link](400, 150, 100, 25);
l3 = new Label("Address");
add(l3);
[Link](50, 200, 100, 25);
t3 = new TextField();
add(t3);
[Link](150, 200, 100, 25);
l4 = new Label("City");
add(l4);
[Link](300, 200, 100, 25);
t4 = new TextField();
add(t4);
[Link](400, 200, 100, 25);
l5 = new Label("Phone NO.");
add(l5);
[Link](50, 250, 100, 25);
t5 = new TextField();
add(t5);
[Link](150, 250, 100, 25);
l6 = new Label("Mobile No.");
add(l6);
[Link](300, 250, 100, 25);
t6 = new TextField();
add(t6);
[Link](400, 250, 100, 25);
l7 = new Label("E_mail");
add(l7);
[Link](50, 300, 100, 25);
t7 = new TextField();
add(t7);
[Link](150, 300, 100, 25);
l8 = new Label("Designation");
add(l8);
[Link](300, 300, 100, 25);
t8 = new TextField();
add(t8);
[Link](400, 300, 100, 25);
L1 = new Label("Operation");
add(L1);
[Link](610, 130, 90, 20);
[Link]([Link]);
b1 = new Button("Add");
[Link](new Color(255, 168, 81));
[Link](600, 150, 100, 25);
add(b1);
b2 = new Button("Delete");
[Link](new Color(255, 168, 81));
[Link](600, 180, 100, 25);
add(b2);
b3 = new Button("Display");
[Link](new Color(255, 168, 81));
[Link](600, 210, 100, 25);
add(b3);
b4 = new Button("Refresh");
[Link](new Color(255, 168, 81));
[Link](600, 240, 100, 25);
add(b4);
b5 = new Button("Edit");
[Link](new Color(255, 168, 81));
[Link](600, 270, 100, 25);
add(b5);
b6 = new Button("Report");
[Link](new Color(255, 168, 81));
[Link](600, 300, 100, 25);
add(b6);
b8 = new Button("Datagrid");
[Link](new Color(255, 168, 81));
[Link](600, 330, 100, 25);
add(b8);
b9 = new Button("Next");
[Link](new Color(255, 168, 81));
[Link](600, 450, 100, 25);
add(b9);
b0 = new Button("Previous");
[Link](new Color(255, 168, 81));
[Link](250, 450, 150, 25);
add(b0);
Font ft = new Font("ARIAL", [Link] + [Link], 18);
[Link](ft);
Font ft1 = new Font("ARIAL", [Link] + [Link], 18);
[Link](ft1);
setTitle("Faculty Information");
setVisible(false);
[Link]();
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link](this);
[Link](this);
[Link](this);
try {
[Link]("[Link]");
[Link]("Driver Loaded successfully");
cn = [Link]("jdbc:mysql://[Link]:3306/JAVA", "root",
"sumit1021");
[Link]("Connection established successfully");
}
catch (ClassNotFoundException e) {
[Link]("Driver Not Avilable" + [Link]());
} catch (SQLException e) {
[Link]("Connection error" + [Link]());
} }
public void actionPerformed(ActionEvent e) {
try {
if ([Link]() == "Add")
{
String sql = "insert into factinfo values(?,?,?,?,?,?,?,?)";
PreparedStatement pst = [Link](sql);
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
[Link](5, [Link]());
[Link](6, [Link]());
[Link](7, [Link]());
[Link](8, [Link]());
int x = [Link]();
if (x > 0) {
[Link]("record inserted successfully");
[Link](null,"Record Inserted
Successfully");
}}
else if ([Link]() == "Delete")
{
String sql =" delete from factinfo where Faculty_ID ='"+[Link]()+"'";
PreparedStatement pst = [Link](sql);
//[Link](4,[Link]());
int x = [Link](sql);
if( x > 0)
{
[Link](null,"Record Delete Successfully");
}
else{
[Link](null,"Record is not Available");
}}
else if([Link]() == "Display")
{
String sql = "select * from factinfo where Faculty_ID = '"+[Link]()+"'";
PreparedStatement pst = [Link](sql);
ResultSet re= [Link]();
if([Link]()){
[Link]([Link](2));
[Link]([Link](3));
[Link]([Link](4));
[Link]([Link](5));
[Link]([Link](6));
[Link]([Link](7));
[Link]([Link](8));
}
else{
[Link](null,"Record is not Available");
}
}
else if([Link]()=="Refresh")
{} }
catch (SQLException f)
{ }}
public static void main(String s[]) {
factinfo mp = new factinfo();
[Link](true);
}
public void paint(Graphics g) {
[Link](new Color(128, 128, 0));
[Link](30, 110, 510, 330, 15, 15);
[Link](30, 110, 510, 330, 15, 15);
[Link](570, 110, 165, 310);
[Link](570, 110, 165, 310);
}
}
OUTPUT:-
5. Consider the Design in Question -2: and Write a
Swing+JDBC program to establish connection with oracle
and perform following operation on emp table. And active
all mentioned JBUttons
a. Display the table data
b. Insert a new record into emp table
c. Delete record by emp no. From emp table.
d. Update emp data using emp number.
6. Implement Following GUI Design and onclick of Button A
color palate to be shown and change the background color
according color selected by user.
CODE:-
import [Link].*;
import [Link].*;
import [Link].*;
public class JColorChooserDemo extends JFrame {
JPanel panel;
Color bgColor = Color.LIGHT_GRAY;
public JColorChooserDemo() {
panel = new JPanel(new BorderLayout());
JButton btnColor = new JButton("Change Color");
[Link](btnColor,[Link]);
[Link](new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
Color color = [Link]([Link],
"Choose a color", bgColor);
if (color != null) { // new color selected
bgColor = color;
}
[Link](bgColor);
}
});
setContentPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JColorChooser Demo");
setSize(300, 200);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
[Link](new Runnable() {
@Override
public void run() {
new JColorChooserDemo();
}
});
}
}
OUTPUT:-