import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
class Demo extends JFrame{
JTree jt;
Demo(){
DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
DefaultMutableTreeNode c=new DefaultMutableTreeNode("C");
DefaultMutableTreeNode d=new DefaultMutableTreeNode("D");
Container c1=getContentPane();
c1.setLayout(new FlowLayout());
setSize(500,500);
a.add(c);
a.add(b);
b.add(d);
jt =new JTree(a);
c1.add(jt);
}
}
public class Tree {
public static void main(String arg[]){
Demo d=new Demo();
d.setVisible(true);
}
}
A blog provide anexample with sample code for swing , servlet, jdbc and odbc connection to database, jsp, servlets, taglib, sql server, cassandra and many more. we also provide Apache camel sample code for integration.
Wednesday, November 7, 2012
Jtree
Create Login form using swing
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener{
JButton jb1;
JLabel jl1,jl2,jl3;
JTextField jt1,jt2;
public Login(){
setSize(500,500);
jb1=new JButton("LogIn");
jl1=new JLabel("");
jl2=new JLabel("\n User Name\n");
jl3=new JLabel("\n Password\n");
jt1=new JTextField(10);
jt2=new JTextField(10);
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(jl1);
c.add(jl2);
c.add(jt1);
c.add(jl3);
c.add(jt2);
c.add(jb1);
jb1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(jt1.getText().equals("uttam") && jt2.getText().equals("123")){
jl1.setText("LogIN successfuly");
jt2.setText("");
jb1.setText("LogOut");
}
else if(jb1.getText().equals("LogOut")){
jl1.setText("LogOut successfuly");
jb1.setText("LogIn");
}
}
public static void main(String[] args) {
// TODO code application logic here
Login l=new Login();
l.setVisible(true);
}
}
Monday, November 5, 2012
Calculator useing jsp
Cal.jsp
<%--
Document : Cal
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculator</title>
</head>
<body>
<form metod="get" action="Cal1.jsp">
Value1:<input type="text" name="v1"/><br/>
Value2:<input type="text" name="v2"/><br/>
Opration:<select name="op">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select><br/>
<input type="submit" name="b" value="ok"/>
</form>
</body>
</html>
Cal1.jsp
<%--
Document : Cal1
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%! int ans; %>
<% int s=Integer.parseInt(request.getParameter("v1"));
int r=Integer.parseInt(request.getParameter("v2"));
String op1=request.getParameter("op");
if(op1.equals("+")){
ans=s+r;
}
if(op1.equals("-")){
ans=s-r;
}
if(op1.equals("*")){
ans=s*r;
}
if(op1.equals("/")){
ans=s/r;
}%>
<h1> answer is : <%=ans%> </h1>
</body>
</html>
How to create Jmenu
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
class Demo extends JFrame /*implements ActionListener*/
{
JMenu jm1,jm2;
JMenuBar jb;
JMenuItem jl1,jl2,jl3,jl4,jl5,jl6;
Demo()
{
jb= new JMenuBar();
jm1=new JMenu("Home");
jm2=new JMenu("Product");
jl1=new JMenuItem("File1");
jl2=new JMenuItem("File2");
jl3=new JMenuItem("File3");
jl4=new JMenuItem("Product1");
jl5=new JMenuItem("Product2");
jl6=new JMenuItem("Product3");
Container c= getContentPane();
c.setLayout(new FlowLayout());
setSize(500,500);
c.add(jb);
jb.add(jm1);
jb.add(jm2);
jm1.add(jl1);
jm1.add(jl2);
jm1.add(jl3);
jm2.add(jl4);
jm2.add(jl5);
jm2.add(jl6);
}
}
public class Jmenu {
public static void main(String arg[]){
Demo d=new Demo();
d.setVisible(true);
}
}
Sunday, November 4, 2012
Textbox with only number
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class Demo1 extends JFrame implements KeyListener{
JTextField jt;
JLabel jl;
Demo1(){
jt=new JTextField(15);
jl=new JLabel("Enter String ");
Container c=getContentPane();
c.setLayout(new FlowLayout());
setSize(500,500);
c.add(jl);
c.add(jt);
jt.addKeyListener(this);
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()>=65 && e.getKeyCode()<=91){
//jt.setText(e.getKeyCode());
jt.setText("");
String s=jt.getText();
int l=s.length();
s=s.substring(0,l-1);
jt.setText(s);
}else
{
}
}
}
public class Textbox {
public static void main(String arg[]){
Demo1 d=new Demo1();
d.setVisible(true);
}
}
Moving ball across the boundry
import java.applet.*;
import java.awt.Color;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Moveball extends Applet implements Runnable{
public int x=1,y=0;
public void init(){
Thread t=new Thread(this);
t.start();
}
public void run(){
while(true){
try {
if(x<300 && y==0)x=x+1;
if(x==300 && y<150)y++;
if(y==150 && x>0)x=x-1;
if(x==0 && y<=150)y=y-1;
Thread.sleep(3);
repaint();
} catch (InterruptedException ex) {
Logger.getLogger(Moveball.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void paint(Graphics g){
setBackground(Color.BLUE);
setForeground(Color.GREEN);
g.drawOval(x, y, 50, 50);
}
}
Using applet to demonstrate the wind
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
public class Wind extends Applet implements Runnable{
int Theta;
int x11=0,y11=0,x12=0,y12=0,x21=0,y21=0,x22=0,y22=0;
@Override
public void run() {
while(true){
Dimension d=getSize();
x11=150+(int)(100*Math.cos(Math.toRadians(Theta)));
y11=150+(int)(100*Math.sin(Math.toRadians(Theta)));
x12=150+(int)(100*Math.cos(Math.toRadians(Theta+180)));
y12=150+(int)(100*Math.sin(Math.toRadians(Theta+180)));
x21=150+(int)(100*Math.cos(Math.toRadians(Theta+90)));
y21=150+(int)(100*Math.sin(Math.toRadians(Theta+90)));
x22=150+(int)(100*Math.cos(Math.toRadians(Theta+270)));
y22=150+(int)(100*Math.sin(Math.toRadians(Theta+270)));
Theta++;
try{
Thread.sleep(3);
repaint();
}
catch(Exception e){
}
}
}
@Override
public void init(){
Thread t1=new Thread(this);
t1.start();
}
@Override
public void paint(Graphics g){
g.drawLine(x11,y11,x12,y12);
g.drawLine(x21,y21,x22,y22);
}
}
OUTPUT
Subscribe to:
Posts (Atom)