Featured Post

Showing posts with label Web Technologies. Show all posts
Showing posts with label Web Technologies. Show all posts

Friday, February 28, 2020

Swing programs to implement event handling using JFrame

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class eventdemo
{
   JButton b1;
   JLabel l1;
   eventdemo()
   {
      JFrame j=new JFrame("Swingdemo");
      j.setSize(300,300);
      j.setLayout(new FlowLayout());
      j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      b1=new JButton("click me");
      JButton b2=new JButton("MCA");
      b1.addActionListener(new ActionListener()
      {
         public void actionPerformed(ActionEvent e)
         {
           l1.setText("button clicked");
         }
      });
      j.add(b1);
      l1=new JLabel("pls click");
      j.add(l1);
      j.setVisible(true);
     }
    public static void main(String args[])
    {
      SwingUtilities.invokeLater(new Runnable(){
      public void run()
      {
        new eventdemo(); 
       }
     });
    }
}
  
Output:

Swing program using JTree



import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;
public class JTreedemo extends JApplet
{
  public void init()
  {
   try{
   SwingUtilities.invokeAndWait(new Runnable(){
   public void run()
   {
     makeGUI();
   }
   });
   }
   catch(Exception e)
   {
     System.out.println("cannot create"+e);
   }
 }
 private void makeGUI()
 {
   DefaultMutableTreeNode top=new DefaultMutableTreeNode("options");
   DefaultMutableTreeNode a=new DefaultMutableTreeNode("A");
   top.add(a);
   DefaultMutableTreeNode a1=new DefaultMutableTreeNode("A1");
   a.add(a1);
   DefaultMutableTreeNode a2=new DefaultMutableTreeNode("A2");
   a.add(a2);
   DefaultMutableTreeNode b=new DefaultMutableTreeNode("B");
   top.add(b);
   DefaultMutableTreeNode b1=new DefaultMutableTreeNode("B1");
   b.add(b1);
   DefaultMutableTreeNode b2=new DefaultMutableTreeNode("B2");
   b.add(b2);
   JTree tree=new JTree(top);
   add(tree);
 }
}
  
   
*****************************************************************
<html>
<body>
<applet code="JTreedemo.class" width=200 height=300>
</applet>
</body>

</html>

Output:



   

Swing programs using JTable

import javax.swing.*;
import java.awt.*;
public class JTabledemo extends JApplet
{
  public void init()  
  {
    try
    {
       SwingUtilities.invokeAndWait(new Runnable()
       {
        public void run()
        {
         makeGUI();
        }
        });
      }
     catch(Exception e)
     {
         System.out.println("Cant create becuase "+e);
     }
  }

private void makeGUI()
{
  //Intilize columns headings
  String[] colheads={"Name","roll no","group"};
  //Intilize data
  Object[][] data={{"kumar","121","MCA"},
                  {"nani","122","MCA"},
                  {"ashok","123","M.Tech"},
                  {"suresh","124","M.Tech"}};
      JTable jt=new JTable(data,colheads);
      add(jt);

     /*if table size is big then we use scrollpane to display table*/
     //JScrollPane jsp=new JScrollPane(jt);
     //add(jsp);

}
}

******************************************************************
<html>
<body>
<applet code="JTabledemo.class" width=200 height=300>
</applet>
</body>

</html>

*******************************************
Output:

Swing program using JTabbedPane


/* JTabbedPanedemo */
import javax.swing.*;
import java.awt.*;

public class JTabbedPanedemo extends JApplet
{
  JButton b1,b2,b3,b4,b5,b6,b7,b8;
  public void init()
  {
   try
   {
     SwingUtilities.invokeAndWait(new Runnable(){
     public void run()
     {
           makeGUI();
     }
    });
   }
   catch(Exception e)
   {
     System.out.println("Cant create becuase"+e);
   }
}
private void makeGUI()
{
  JTabbedPane jtp=new JTabbedPane();
   jtp.addTab("B.Tech",new BTechPanel());
   jtp.addTab("M.Tech",new MTechPanel());
   jtp.addTab("MCA",new MCAPanel());
  add(jtp);
}



public class BTechPanel extends JPanel
{
  public BTechPanel()
  {
   b1=new JButton("CSE");
   b2=new JButton("IT");
   add(b1);
   add(b2);
   }
}  
public class MTechPanel extends JPanel
{
  public MTechPanel()
  {
    b3=new JButton("CSE");
   b4=new JButton("IT");
   b5=new JButton("Cyber");
   add(b3);
   add(b4);
   add(b5);
  }
} 
public class MCAPanel extends JPanel
{
 public MCAPanel()
  {
   b6=new JButton("MCA");
   b7=new JButton("MPCc");
   add(b6);
   add(b7);
   }
}
}

******************************************************************
<html>
<body>
<applet code="JTabbedPanedemo.class" width=200 height=300>
</applet>
</body>

</html>

Output:

Swing program using JScrollPane



/*  JScrollPane */
import javax.swing.*;
import java.awt.*;
public class JScrollPanedemo extends JApplet
{
  public void init()
  {
    try
    {
     SwingUtilities.invokeAndWait(new Runnable(){
     public void run()
     {
        makeGUI();
     }
    });
    }
    catch(Exception e)
    {
      System.out.println("Cant create because"+e);
    }
}
private void makeGUI()
{
   JPanel jp=new JPanel();
   jp.setLayout(new GridLayout(5,5));
    int b=0;
    for(int i=0;i<5;i++)
    {
      for(int j=0;j<5;j++)
      {
        jp.add(new JButton("Button"+b));
        b++;
       }
    }
   JScrollPane jsp=new JScrollPane(jp);
   add(jsp);
}
}

********************************************************************

<html>

<body>
<applet code="JScrollPanedemo.class" width=200 height=300>
</applet>
</body>
</html>

*****************************************************************


Design home page using awt and swings


 /* home page using awt and swings */
 /* home page using JApplets */

import javax.swing.*;
import java.awt.event.*;
public class homepage extends JApplet implements ActionListener
{
   JLabel l1,l2,l3,l4,l5,l6;
   JTextField t1,t2,t3;
   JRadioButton rb1,rb2;
   ButtonGroup gender;
   JCheckBox c1,c2,c3,c4;
   JComboBox<String> j1;
    public void init()          
    {
     setLayout(null);
     l1=new JLabel("Enter your First name");
     l1.setSize(300,20);
     l1.setLocation(100,100);  add(l1);
     t1=new JTextField();
     t1.setSize(100,20);
     t1.setLocation(300,100);  add(t1);
     l2=new JLabel("Enter your Email id");
     l2.setSize(300,20);
     l2.setLocation(100,130);
     add(l2); 
     t2=new JTextField();
     t2.setSize(100,20);
     t2.setLocation(300,130);
     add(t2);
     JLabel l3=new JLabel("Select gender");
     l3.setSize(100,20);
     l3.setLocation(100,160);
     add(l3);
     rb1=new JRadioButton("male");
     rb1.setSize(100,20);
     rb1.setLocation(300,160); 
     rb2=new JRadioButton("female");
     rb2.setSize(100,20);
     rb2.setLocation(400,160);
     add(rb1);
     add(rb2);
     gender=new ButtonGroup();
     gender.add(rb1);
     gender.add(rb2);
     l4=new JLabel("Select Your Born Year");
     l4.setSize(150,20);
     l4.setLocation(100,190);
     add(l4);
     j1=new JComboBox<String>();
     j1.addItem("Select");
     j1.addItem("1990");
     j1.addItem("1991");
     j1.setSize(100,20);
     j1.setLocation(300,190);
     add(j1);
     JLabel l4=new JLabel("Select your qualification");
     l4.setSize(150,20);
     l4.setLocation(100,220);
     add(l4);
     c1=new JCheckBox("ssc");
     c1.setSize(100,20);
     c1.setLocation(250,220);
     add(c1);
     c2=new JCheckBox("Inter");
     c2.setSize(100,20);
     c2.setLocation(350,220);
     add(c2);
     c3=new JCheckBox("Degree");
     c3.setSize(100,20);
     c3.setLocation(450,220);
     add(c3);
     c4=new JCheckBox("PG");
     c4.setSize(100,20);
     c4.setLocation(550,220);
     add(c4);
     JButton b1=new JButton("submit");
     b1.setSize(100,20);
     b1.setLocation(100,250);
     add(b1);
     JButton b2=new JButton("reset");
     b2.setSize(100,20);
     b2.setLocation(300,250);
     add(b2);
     l6=new JLabel("");
     l6.setSize(300,20);
     l6.setLocation(100,300);
     add(l6);
     b1.addActionListener(this);
     b2.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e)
    {
      String str=e.getActionCommand();
     if(str.equals("submit"))
     {
       cleardata();
       l6.setText("Your Form is submitted");  
     };    
     if(str.equals("reset"))
     {
       cleardata();
       l6.setText("Please enter the details again");
    }
  }
    public void cleardata()
    {
      String cl="";
      t1.setText(cl);
     t2.setText(cl);
     c1.setSelected(false);   
     c2.setSelected(false);
     c3.setSelected(false);
     c4.setSelected(false);   
     j1.setSelectedIndex(0);
     gender.clearSelection();
   }     
}

*****************************************************************

<html>
<body>
<applet code="homepage.class" width=150 height=50>
</applet>
</body>

</html>

********************************************************************
Output:




Tags:
Design home page using awt and swings, design home page using swings and awt,
Design home page using awt and swing, design home page using swing and awt, design home page using JApplets, Design a web page using awt and swings, design a form using awt and swings,
Form validation using swings, form validation using awt and swings, connect home page to oracle using swings. connecting swing programs to back end, connecting swings programs to oracle, swings and mysql, swings and oracle, jdbc-odbc program, develop swing programs using JButton , JLabel, JRadioButton ,JCheckBox, JComboBox, Swing programs using JComboBox