Monday, 26 March 2018

JTable Demo

Hi All,
See the JTable code


import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
public class JTableDemo extends JFrame
{
    public JTableDemo()
    {
        //headers for the table
        String[] columns = new String[] {
            "Id", "Name", "Hourly Rate", "Part Time"
        };
        
        //actual data for the table in a 2d array
        Object[][] data = new Object[][] {
            {1, "Raju", 40.0, false },
            {2, "Rechu", 70.0, false },
            {3, "Babu", 60.0, true },
        };
        //create table with data
        JTable table = new JTable(data, columns);
        
        //add the table to the frame
        this.add(new JScrollPane(table));
        
        this.setTitle("Table Example");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        this.pack();
        this.setVisible(true);
    }
    
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new JTableDemo();
            }
        });
    }  
}

Java Menu

Hi All,
See this Java Menu using AWT.

import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class MenuDemoMain extends Frame implements ActionListener
{
  TextArea ta;
  public MenuDemoMain()
  {                      // create menu bar
    MenuBar mBar = new MenuBar();
    setMenuBar(mBar);   // add menu bar to frame
                                // create menus
    Menu files = new Menu("Files");
    Menu date = new Menu("Date");
    Menu exit = new Menu("Exit");
    ta = new TextArea(10, 40);
    ta.setBackground(Color.cyan);
                              // add menus to menu bar
    mBar.add(files);
    mBar.add(date);
    mBar.add(exit);
                             // create menu items to menus
    MenuItem mi1 = new MenuItem("GLDemo.java");
    files.add(mi1);                 // using anonymous menu items
    files.add(new MenuItem("LabelTest.java"));
    files.addSeparator();
    files.add(new MenuItem("UsingFonts.class"));
    files.add(new MenuItem("FLDemo.class"));

    date.add(new MenuItem("Today"));
    exit.add(new MenuItem("Close"));
                               // linking listener
    files.addActionListener(this);
    date.addActionListener(this);
    exit.addActionListener(this);

    add(ta, "Center");
                                         
    setTitle("Menu Practice");
    setSize(400, 400);
    setVisible(true);
  }
  public void actionPerformed(ActionEvent e)
  {
    String str = e.getActionCommand();

    if(str.equals("Close"))
    {
      System.exit(0);
    }
    else if(str.equals("Today"))
    {
      ta.setText("Today: " + new java.util.Date());
    }
    else
    {
      try
      {
        FileReader fr = new FileReader(str);
        ta.setText("Folloiwing are file contents:\n\n");
        int temp;
        while( (temp = fr.read()) != -1)
        {
          char ch = (char) temp;
          String s1 = String.valueOf(ch);
          ta.append(s1);
        }
        fr.close();
      }
      catch(IOException e1) 
      {
        ta.setText("Exception: " + e1);
      }
    }
  }
  public static void main(String args[])
  {
    new MenuDemoMain();
  }
}

Friday, 23 March 2018

AWT Events

  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. class AEvent extends Frame implements ActionListener{  
  4. TextField tf;  
  5. AEvent(){  
  6.   
  7. //create components  
  8. tf=new TextField();  
  9. tf.setBounds(60,50,170,20);  
  10. Button b=new Button("click me");  
  11. b.setBounds(100,120,80,30);  
  12.   
  13. //register listener  
  14. b.addActionListener(this);//passing current instance  
  15.   
  16. //add components and set size, layout and visibility  
  17. add(b);add(tf);  
  18. setSize(300,300);  
  19. setLayout(null);  
  20. setVisible(true);  
  21. }  
  22. public void actionPerformed(ActionEvent e){  
  23. tf.setText("Welcome");  
  24. }  
  25. public static void main(String args[]){  
  26. new AEvent();  
  27. }  

Swing initialization

  1. import javax.swing.*;  
  2. public class Simple {  
  3. JFrame f;  
  4. Simple(){  
  5. f=new JFrame();//creating instance of JFrame  
  6.           
  7. JButton b=new JButton("click");//creating instance of JButton  
  8. b.setBounds(130,100,10040);  
  9.           
  10. f.add(b);//adding button in JFrame  
  11.           
  12. f.setSize(400,500);//400 width and 500 height  
  13. f.setLayout(null);//using no layout managers  
  14. f.setVisible(true);//making the frame visible  
  15. }  
  16.   
  17. public static void main(String[] args) {  
  18. new Simple();  
  19. }  

Simple Frame program

  1. import javax.swing.*;  
  2. public class FirstSwingExample {  
  3. public static void main(String[] args) {  
  4. JFrame f=new JFrame();//creating instance of JFrame  
  5.           
  6. JButton b=new JButton("click");//creating instance of JButton  
  7. b.setBounds(130,100,10040);//x axis, y axis, width, height  
  8.           
  9. f.add(b);//adding button in JFrame  
  10.           
  11. f.setSize(400,500);//400 width and 500 height  
  12. f.setLayout(null);//using no layout managers  
  13. f.setVisible(true);//making the frame visible  
  14. }  

Simple Java Form for DB

Hi All,

Use this code.


-------------------------------
import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

import java.sql.*;

public class sample1 extends JFrame
{
 public static Connection cons;
 public Statement s;
 ResultSet rs;
 public JTextField Name_text=new JTextField(20);
 public JTextField User_Name_text=new JTextField(20);
 JPasswordField Password_text=new JPasswordField(20);
 JTextField City_text=new JTextField(20);
 public sample1()
{
  super("Registration Page");
  setSize(500,500);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  Container c=getContentPane();
  c.setLayout(null);
 
  //creating the fields
  setIconImage(getToolkit().getImage("icon.jpeg"));
 
  JLabel name_label=new JLabel("Name");
  JLabel User_Name_label=new JLabel("User Name");
  JLabel Password_label=new JLabel("Password");
  JLabel DOB_label=new JLabel("DOB");
  JLabel Address_label=new JLabel("Address");
  JLabel City_label=new JLabel("city");
  JLabel Gender_label=new JLabel("Gender");
 
  JTextField DOB_text=new JTextField(20);
  JTextField Address_text=new JTextField(20);

  JTextField Gender_text=new JTextField(20);
 
  ButtonGroup buttongroup=new ButtonGroup();
  JRadioButton Male_rbutton=new JRadioButton("Male");
  JRadioButton Female_rbutton=new JRadioButton("Female");
  buttongroup.add(Male_rbutton);
  buttongroup.add(Female_rbutton);
  JButton Submit_button=new JButton("Submit");
 


   Submit_button.addActionListener(new ActionListener()
   {
   public void actionPerformed(ActionEvent e)
   {
   
    try
     {
    String password_text =Password_text.getText();
    String city_text=City_text.getText();
   
   
       s=cons.createStatement();
       rs=s.executeQuery("select * from table1");     
       rs.moveToInsertRow();
 
     
       rs.updateString(3,password_text);
       rs.updateString(6,city_text);
       
    rs.insertRow();

       
       
       
       /*Name_text.setText(rs.getString(1));
       User_Name_text.setText(rs.getString(2));*/
       
     }

    catch(SQLException sqlex)
    {
     System.out.println("error is "+sqlex);
    }
       
       
   }
 
   }
   );
   


 

  //Aligning the Fields
  name_label.setBounds(50,50,50,20);
  Name_text.setBounds(130,50,90,20);
  User_Name_label.setBounds(50,80,80,20);
  User_Name_text.setBounds(130,80,90,20);
  Password_label.setBounds(50,110,80,20);
  Password_text.setBounds(130,110,90,20);
  DOB_label.setBounds(50,140,50,20);
  DOB_text.setBounds(130,140,90,20);
  Gender_label.setBounds(50,170,50,20);
  Male_rbutton.setBounds(130,170,60,20);
  Female_rbutton.setBounds(190,170,90,20);
  Address_label.setBounds(50,200,50,20);
  Address_text.setBounds(130,200,90,20);
  City_label.setBounds(50,230,50,20);
  City_text.setBounds(130,230,90,20);
  Submit_button.setBounds(160,270,90,20);
  Male_rbutton.setSelected(true);

  //adding the fields
  c.add(name_label);
  c.add(User_Name_label);
  c.add(Password_label);
  c.add(DOB_label);
  c.add(Address_label);
  c.add(City_label);
  c.add(Gender_label);
  c.add(Name_text);
  c.add(User_Name_text);
  c.add(Password_text);
  c.add(DOB_text);
  c.add(Address_text);
  c.add(City_text);
  c.add(Male_rbutton);
  c.add(Female_rbutton);
  c.add(Submit_button); 
  setVisible(true);
  }




public static void main(String args[])
 {

  sample1 sobj=new sample1();

  try
  {
   
 
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
   con=DriverManager.getConnection("jdbc:odbc:sampletwo");
   
       
       
   
  }


  catch(ClassNotFoundException cnfex)
  {
    System.err.println("failed to load driver");
    cnfex.printStackTrace();
    System.exit(1);

  }
 
  catch(SQLException sqlex)
{
  System.err.println("unable to connect");
  sqlex.printStackTrace();
}
   
 



}


}

Java Database with MS Access

Hi All,

Use this code

private void addActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addActionPerformed
try
{
       String id1=id.getText();
String name1=name.getText();
     
        String password1=password.getText();
     
       
       
       
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con=null;
con=DriverManager.getConnection("jdbc:odbc:bugtrack");
Statement smt1=con.createStatement();
                Statement smt2=con.createStatement();
                 Statement smt3=con.createStatement();
                smt1.executeUpdate("insert into login values('"+name1+"','tester','"+password1+"')");
                smt1.close();
smt2.executeUpdate("insert into tester values('"+id1+"','"+name1+"','"+password1+"','not assigned' )");
               //JOptionPane.showMessageDialog(null, "registred","success",JOptionPane.PLAIN_MESSAGE);
               System.out.println("registred"); 
               // setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 
               ResultSet rs=null;

//Statement smt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
               int i=0;
                 
rs=smt3.executeQuery("select * from tester");
               
                while(rs.next())
{
                    jScrollPane1.setViewportView(jTable1);
                    jTable1.setValueAt(rs.getString("id"),i,0);
                    jTable1.setValueAt(rs.getString("tname"),i,1);
                     jTable1.setValueAt(rs.getString("status"),i,2);
                   
                 
                  i++;
                }
                smt1.close();
        smt2.close();
                smt3.close();
con.close();
                //setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
//response.sendRedirect("secon.jsp");
               // this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
                 //closewindow();
}
catch(Exception e)
{
System.out.println("Error="+e);
}
    // TODO add your handling code here:
}

Java is simple

Hi All,

As the title given, Java is so simple, because tons of materials are available in this domain.
In the coming sessions we will go deeper.
All the best.

Swing Menu

Hi All, Use this code. ------------------------------------------------------- package placement; import java.awt.*; import java.io.*...