Advanced Java Swing Components Guide
Advanced Java Swing Components Guide
1 Use of JTextArea()
import [Link].*;
public class JTextAreaExample
{
JTextAreaExample()
{
JFrame f= new JFrame();
JTextArea area=new JTextArea("Welcome to javatpoint");
[Link](10,30, 200,200);
[Link](area);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new JTextAreaExample();
}
}
3 Swing Program to Display use of JButton(). When pressed OK button it will display “You
pressed OK button” or when pressed cancel button display “You pressed Cancel Button”
message in the JLabel.
import [Link].*;
import [Link].*;
import [Link].*;
class JButtonDemo extends JFrame implements ActionListener
{ JButton ok, cancel;
JLabel jlbl;
JButtonDemo()
{ setLayout(new FlowLayout());
ok=new JButton("OK"); [Link](KeyEvent.VK_O);
cancel=new JButton("CANCEL"); [Link](KeyEvent.VK_C);
jlbl=new JLabel();
add(ok); add(cancel); add(jlbl);
[Link](this);
[Link](this);
setSize(500,500); setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{ String s=[Link]();
String msg="";
if([Link]("OK"))
msg="You pressed OK button";
else if([Link]("CANCEL"))
msg="You pressed Cancel Button";
[Link](msg);
}
public static void main(String args[])
{
new JButtonDemo();
}
}
4 Use of JCheckbox()
import [Link].*;
public class JCheckBoxEx
{
JCheckBoxEx(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
[Link](100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
[Link](100,150, 80,50);
[Link](checkBox1);
[Link](checkBox2);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{
new JCheckBoxEx();
}}
total=pen+pencil+scale;
[Link]("Cost Rs:"+total);
}
public static void main(String args[])
{
new JCheckBoxDemo();
}
}
[Link](true);
[Link]("Japan");
[Link]("India");
[Link](50, 50,90,20);
[Link](cb);
[Link](null);
[Link](400,500);
[Link](true);
}
public static void main(String[] args) {
new JComboBoxExample();
}
}
8 Use of JList()
import [Link].*;
class JListDemo
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p=new JPanel();
String s[]={"Core Java","Advanced Java", "Enterprise Java", "Web Services"};
JList jL=new JList(s);
[Link](jL);
[Link](p);
[Link](300,300);
[Link](true);
}
}
9 Use of JMenu()
import [Link].*;
class JMenuExample
{
JMenu menu1,menu2, submenu1,submenu2;
JMenuItem i1,i2,i3,i4,i5;
JMenuExample()
{
JFrame f=new JFrame();
JMenuBar mb=new JMenuBar();
menu1 = new JMenu("SYCS-IV");
menu2=new JMenu("SYCS -III");
submenu1 =new JMenu("Programming :");
submenu2=new JMenu("MAths :");
i1=new JMenuItem("Advanced Java");
i2=new JMenuItem("FOA");
i3=new JMenuItem("CN");
i4=new JMenuItem("SE");
i5=new JMenuItem("LAUP");
[Link](i1); [Link](i2); [Link](i3); [Link](i4);
[Link](i5);
[Link](submenu1); [Link](submenu2);
[Link](menu1); [Link](menu2);
[Link](mb);
[Link](400,400);
[Link](null);
[Link](true);
}
public static void main(String args[])
{ new JMenuExample();
}
10 Use of JPopupMenu()
import [Link].*;
import [Link].*;
class JPopupExample
{
JPopupExample()
{
JFrame f=new JFrame();
JPopupMenu popupmenu=new JPopupMenu("EDIT");
JMenuItem cut = new JMenuItem("CUT");
JMenuItem copy = new JMenuItem("COPY");
JMenuItem paste = new JMenuItem("PASTE");
JLabel label=new JLabel();
[Link](400,100);
[Link](cut); [Link](copy); [Link](paste);
[Link](new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
[Link](f,[Link](),[Link]());
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Cut is slected");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("Copy is slected");
}
});
[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("PAste is slected");
}
});
[Link](label);
[Link](popupmenu);
[Link](500,500);
[Link](null);
[Link](true);
11 Use of JRadioButton()- stationary app where user can select only one item due to
ButtonGroup() control
import [Link].*;
import [Link].*;
import [Link].*;
class JRadioButtonDemo extends JFrame implements ActionListener
{
JRadioButton ch1, ch2, ch3;
JLabel jl;
JTextField jtf;
JRadioButtonDemo()
{
setLayout(new FlowLayout());
ch1=new JRadioButton("Pen Rs 20");
ch2=new JRadioButton("Pencil Rs 10");
ch3=new JRadioButton("Scale Rs 50");
ButtonGroup bg=new ButtonGroup();
[Link](ch1); [Link](ch2); [Link](ch3);
jl=new JLabel("Total Rupees :");
jtf=new JTextField(20);
add(ch1); add(ch2); add(ch3); add(jl); add(jtf);
[Link](this); [Link](this);
[Link](this);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int pen=0 , pencil=0,scale=0, total=0;
if([Link]())
pen=20;
else
pen=0;
if([Link]())
pencil=10;
else
pencil=0;
if([Link]())
scale=50;
else
scale=0;
total=pen+pencil+scale;
[Link]("Cost Rs:"+total);
}
public static void main(String args[])
{
new JRadioButtonDemo();
}
}
12 Use of JRadioButton()
import [Link].*;
public class JRadiobuttonEx
{
JFrame f;
JRadiobuttonEx()
{
f=new JFrame("Radio Button Example");
JRadioButton r1=new JRadioButton("Male");
JRadioButton r2=new JRadioButton("Female");
[Link](75,50,100,30);
[Link](75,100,100,30);
ButtonGroup bg=new ButtonGroup();
[Link](r1);[Link](r2);
[Link](r1);[Link](r2);
[Link](300,300);
[Link](null);
[Link](true);
}
public static void main(String[] args)
{
new JRadiobuttonEx();
}
}
13 Use of JTabbedPane()
import [Link].*;
class JTabbedPaneExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JTabbedPane jTab=new JTabbedPane();
JLabel jL1=new JLabel("This is tab onq");
JLabel jL2=new JLabel("This is tab two");
JTextField jT1=new JTextField("welcome");
[Link](jL1); [Link](jT1);
final Object[][] info={ {"1","jeet"} , {"2","hari"}, {"3","kavi"}};
final String[] cHead={"Roll no", "Name"};
JTable jT=new JTable(info, cHead);
[Link](jT);
[Link](jL2);
[Link]("Tab1", p1);
[Link]("Tab2", p2);
[Link](jTab);
[Link](500,400);
[Link](true);
}
}
14 Use of JTable()
import [Link].*;
class JTableExample
{
public static void main(String args[])
{
JFrame f = new JFrame("JList Example");
JPanel p=new JPanel();
final Object[][] info={ {"1","jeet"} , {"2","hari"}, {"3","kavi"}};
final String[] cHead={"Roll no", "Name"};
JTable jT=new JTable(info, cHead);
[Link](jT);
[Link](p);
[Link](400,400);
[Link](true);
}
}
public static void main(String args[])
{
new LoginDemo();
}
}
[Link](lblname); [Link](jtfname);
[Link](lblm1); [Link](jtfm1);
[Link](lblm2); [Link](jtfm2);
[Link](lblm3); [Link](jtfm3);
[Link](lbltotal); [Link](lbldisptotal);
[Link](lblremark); [Link](lbldispremark);
[Link](btnCalculate); [Link](btnReset);
[Link](this);
[Link](this);
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
if([Link]()==btnCalculate)
{ double s1,s2,s3,total;
s1=[Link]([Link]());
s2=[Link]([Link]());
s3=[Link]([Link]());
total=s1+s2+s3;
[Link](total+"");
if(s1>=40 && s2>=40 && s3>=40)
[Link]("PASS");
else
[Link]("FAIL");
}
if([Link]()==btnReset)
{ [Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
[Link](null);
}
}
public static void main(String args[])
{
new MarksheetDemo();
}
}
setSize(500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
double p, i, t,si;
p=[Link]([Link]());
i=[Link]([Link]());
t=[Link]([Link]());
if([Link]()==calculateBtn)
{
si=(p*i*t)/100;
[Link]("Simple Interest "+si);
}
if([Link]()==resetBtn)
{
[Link](null); [Link](null); [Link](null);
[Link](null);
}
}
public static void main(String args[])
{
new SimpleInterestDemo();
}
}
JDBC
1 Steps :
File New Project
Press Next
[Link]();
[Link]();
[Link]();
}
catch(Exception e)
{
}
}
}
catch(Exception e)
{
}
}
}
Servlet
1 Design a servlet program to do the arithmetic operations
<form action="AritServlet" method="get">
Enter First Number :
<Input type="text" name="n1">
<br>
Enter Second number:
<Input type="text" name="n2">
<br>
<select name="op">
<option value="add">Additon</option>
<option value="sub">subtraction</option>
<option value="mul">multiplication</option>
<option value="div">division</option>
</select>
<br>
<input type="submit" value="Click">
</form>
2 Write a servlet to accept Employee details and insert the data into database
<body>
<form action="EmpServlet" method="post">
Enter Employee name:
<Input type="text" name="ename"> <br>
Enter Employee salary:
<Input type="text" name="esal"> <br>
<input type="submit" value="save"> <br>
<input type="reset" value="clear"> <br>
</form>
</body>
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/EmpServlet"})
public class EmpServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
[Link]("Connection okay");
[Link]("[Link]"); //step2
Connection con=[Link]("jdbc:derby://localhost:1527/EmpDB" ,
"EmpDB","EmpDB"); // step 3
[Link]("Connection okay");
String empname=[Link]("ename");
int salary = [Link]([Link]("esal"));
[Link]("RECORD INSERTED");
[Link]();
[Link]();
}
catch(Exception e)
{ [Link]("Error");
}
</table>
</form>
</body>
</html>
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@WebServlet(urlPatterns = {"/regServlet"})
public class regServlet extends HttpServlet {
String st=[Link]("stream");
String wn=[Link]("wno");
[Link]("Welcome :" +nm);
if([Link]("BScCS"))
{ [Link]("Stream: BSc Computer Science");
}
if([Link]("BScIT"))
{ [Link]("BSc Inforamtion Technology");
}
if([Link]("BCOM"))
{ [Link]("B Com");
}
}
4 Create a servlet for a login page. If the username and password are correct then it says
message “Hello <username>” else a message “login failed”
<html>
<head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value="<< Click to Login >>" >
</form>
</html>
package mypack;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Servlet LoginServlet</title></head>");
String uname = [Link]("txtId");
String upass = [Link]("txtPass");
if([Link]("admin") && [Link]("12345")){
[Link]("<body bgcolor=blue >");
[Link]("<h1> Welcome !!! "+uname+"</h1>");
}
else{
[Link]("<body bgcolor=red >");
[Link]("<h1> Login Fail !!! </h1>");
}
[Link]("</body>");
[Link]("</html>");
}
}
5 Using Request Dispatcher Interface create a Servlet which will validate the password
entered by the user, if the user has entered "Servlet" as password, then he will be
forwarded to Welcome Servlet else the user will stay on the [Link] page and an error
message will be displayed.
<html>
<head><title>Login Form</title></head>
<form action="LoginServlet" >
Enter User ID<input type="text" name="txtId"><br>
Enter Password<input type="password" name="txtPass"><br>
<input type="reset">
<input type="submit" value="<< Click to Login >>" >
</form>
</html>
package mypack;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Servlet LoginServlet</title></head>");
String uname = [Link]("txtId");
String upass = [Link]("txtPass");
if([Link]("admin") && [Link]("servlet")){
RequestDispatcher rd = [Link]("WelcomeServlet");
[Link](request, response);
}
else{
[Link]();
[Link]("<h1> Login Fail !!! </h1>");
RequestDispatcher rd = [Link]("[Link]");
[Link](request, response);
}
[Link]("</body>");
[Link]("</html>");
}
}
package mypack;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
[Link]("<html><head>");
[Link]("<title>Welcome Servlet</title></head>");
[Link]("");
[Link]("<h1>Welcome to the world of Servlet !!!!</h1>");
[Link]("</body>");
[Link]("</html>");
}
}
JSP
1 Page directive – include page
[Link]
<%--
Document : B
Created on : Mar 20, 2021, 8:55:18 AM
Author : ADMIN
--%>
[Link]
<%--
Document : A
Created on : Mar 20, 2021, 8:54:16 AM
Author : ADMIN
--%>
</body>
</html>
3 Jsp Login
[Link] or [Link]
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="[Link]" method="post">
Ente your name:
<input type="text" name="t1">
Enter Password:
<input type="password" name="p1">
<br>
<input type="submit">
</form>
</body>
</html>
[Link]
%>
</form>
</body>
</html>
4 Develop a simple JSP application to pass values from one page to another with validations.
(Name-txt, age-txt, hobbies-checkbox, email-txt, gender-radio button).
[Link]
package mypack;
public class CheckerBean {
private String name, age, hob, email, gender, error;
public CheckerBean(){error="";}
<%--
Document : successful
Created on : Apr 5, 2021, 2:02:06 AM
Author : ADMIN
--%>
JavaBean
1. Write a simple javabean code to access employee id and name
[Link]
package person;
}
[Link]
package person;
2 Write a simple java bean code to access student details <jsp:useBean> tag
[Link]
package Bean;
[Link]