Thursday, 4 October 2012

java tool tip



This java program is used to display a tool-tip.
 
//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;

public class JavaApplication2 extends JFrame {
   
    //Initializing JButton
    private JButton button;

    //Setting up GUI
    public JavaApplication2() {
       
        //Setting up the Title of the Window
        super("Add ToolTipText on JButton");

        //Set Size of the Window (WIDTH, HEIGHT)
        setSize(250,100);

        //Exit Property of the Window
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Constructing JButton
        button = new JButton("Place The Cursor Here...");
       
        //Setting or Adding ToolTipText on JButton
        button.setToolTipText("My First ToolTipText on JButton");

        //Setting up the container ready for the components to be added.
        Container pane = getContentPane();
        setContentPane(pane);
       
        //Setting up the container layout
        FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
        pane.setLayout(flow);

        //Adding the JButton component to the container
        pane.add(button);

        /**Set all the Components Visible.
         * If it is set to "false", the components in the container will not be visible.
         */
        setVisible(true);
    }
   
    //Main Method
    public static void main (String[] args) {
        JavaApplication2 ttt = new JavaApplication2();
    }
}


Password demo in java

Password demo in java

This Java program is used for password authentication.

package javaapplication1;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;

public class JavaApplication1 extends JPanel implements ActionListener{

    private static String OK = "ok";
    private static String HELP = "help";

    private JFrame controllingFrame; //needed for dialogs
    private JPasswordField passwordField;

    public JavaApplication1(JFrame f) {
        //Use the default FlowLayout.
        controllingFrame = f;

        //Create everything.
        passwordField = new JPasswordField(10);
        passwordField.setActionCommand(OK);
        passwordField.addActionListener(this);

        JLabel label = new JLabel("Enter the password: ");
        label.setLabelFor(passwordField);

        JComponent buttonPane = createButtonPanel();

        //Lay out everything.
        JPanel textPane = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        textPane.add(label);
        textPane.add(passwordField);

        add(textPane);
        add(buttonPane);
    }

    protected JComponent createButtonPanel() {
        JPanel p = new JPanel(new GridLayout(0,1));
        JButton okButton = new JButton("OK");
        JButton helpButton = new JButton("Help");

        okButton.setActionCommand(OK);
        helpButton.setActionCommand(HELP);
        okButton.addActionListener(this);
        helpButton.addActionListener(this);

        p.add(okButton);
        p.add(helpButton);

        return p;
    }

    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (OK.equals(cmd)) { //Process the password.
            char[] input = passwordField.getPassword();
            if (isPasswordCorrect(input)) {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Success! You typed the right password.");
            } else {
                JOptionPane.showMessageDialog(controllingFrame,
                    "Invalid password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            }

            //Zero out the possible password, for security.
            Arrays.fill(input, '0');

            passwordField.selectAll();
            resetFocus();
        } else { //The user has asked for help.
            JOptionPane.showMessageDialog(controllingFrame,
                "You can get the password by searching this example's\n"
              + "source code for the string \"correctPassword\".\n"
              + "Or look at the section How to Use Password Fields in\n"
              + "the components section of The Java Tutorial.");
        }
    }

    /**
     * Checks the passed-in array against the correct password.
     * After this method returns, you should invoke eraseArray
     * on the passed-in array.
     */
    private static boolean isPasswordCorrect(char[] input) {
        boolean isCorrect = true;
        char[] correctPassword = { 'b', 'u', 'g', 'a', 'b', 'o', 'o' };

        if (input.length != correctPassword.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals (input, correctPassword);
        }

        //Zero out the password.
        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }

    //Must be called from the event dispatch thread.
    protected void resetFocus() {
        passwordField.requestFocusInWindow();
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("PasswordDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        final JavaApplication1 newContentPane = new JavaApplication1(frame);
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Make sure the focus goes to the right component
        //whenever the frame is initially given the focus.
        frame.addWindowListener(new WindowAdapter() {
            public void windowActivated(WindowEvent e) {
                newContentPane.resetFocus();
            }
        });

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        createAndShowGUI();
            }
        });
    }
}

OUT PUT:
TYPE THE PASSWORD AS "bugaboo" AND PRESS OK BUTTON

Student details form using java


This Java program is used for accessing student information in a Graphical User Interface format and caliculates total marks and average of the marks and it is an applet program. 

     import java.awt.*;
     import java.applet.*;
     import java.awt.event.*;
 /* <applet code="Stupanel" width=400 height=400>
    </applet> */
   
    public class Stupanel extends Applet implements    ActionListener,ItemListener
     {
             String s1,s2,s3;
             TextField t3,t4,t5,t6,t7;
             Button tot,avg;
             Checkbox c1,c2,c3,c4,m,f;
             CheckboxGroup cbg;
              Panel p1,p2,p3,p4;

              public void init()
             {
                  s3=" ";
                  tot=new Button("Total");
                  avg=new Button("Average");
                 c1=new Checkbox("MCA",true);
                 c2=new Checkbox("Msc Comp");
                 c3=new Checkbox("MSIT");
                 c4=new Checkbox("MSIS");

                 cbg=new CheckboxGroup();

                 m=new Checkbox("Male",cbg,false);
                 f=new Checkbox("Female",cbg,true);
                 p1=new Panel();
                 p1.setLayout(new GridLayout(2,2));

                 p1.add(new Label("Student Number "));
                 p1.add(new TextField(5));

                 p1.add(new Label("Student Name "));
                 p1.add(new TextField(15));
             




      add(p1);
      p2=new Panel(); p2.setLayout(new GridLayout(1,3));
      p2.add(new Label("Gender"));
      p2.add(m);
      p2.add(f);
      add(p2);  

       p3=new Panel(); p3.setLayout(new GridLayout(1,5));
       p3.add(new Label("Degree"));
       p3.add(c1);   p3.add(c2);     p3.add(c3);      p3.add(c4);

       add(p3);

        p4=new Panel(); p4.setLayout(new GridLayout(6,2));
        p4.add(new Label("Marks in JAVA"));
       t3=new TextField(3);    p4.add(t3);

       p4.add(new Label("Marks in VB .Net"));
       t4=new TextField(3); p4.add(t4);

       p4.add(new Label("Marks In C"));
       t5=new TextField(3); p4.add(t5);     

       p4.add(new Label("Total "));
       t6=new TextField(3); p4.add(t6);     

       p4.add(new Label(" Average"));
       t7=new TextField(3); p4.add(t7);     

        p4.add(tot);   p4.add(avg);

        tot.addActionListener(this);
        avg.addActionListener(this);

        c1.addItemListener(this);
        c2.addItemListener(this);
      


        c3.addItemListener(this);
        c4.addItemListener(this);
        m.addItemListener(this);
        f.addItemListener(this);

       
             add(p4);
      }

 
               public void paint(Graphics g)
      {
         int no,m1,m2,m3,tot;
         float avg=0.0f;
         no=m1=m2=m3=tot=0;
         try
         {
            m1=Integer.parseInt(t3.getText());
            m2=Integer.parseInt(t4.getText());
            m3=Integer.parseInt(t5.getText());
         }
         catch(Exception e)
         {
         }
         tot=m1+m2+m3;
         avg= tot/3;
         s1=String.valueOf(tot);
         s2=String.valueOf(avg);
     }

      public boolean action(Event e,Object o)
      {
        repaint();
        return true;
      }

      public void actionPerformed(ActionEvent e)
      {
         s3=e.getActionCommand();
         if(s3.equals("Total"))
           t6.setText(s1);
         if(s3.equals("Average"))
           t7.setText(s2);
           repaint();
      }
        public void itemStateChanged(ItemEvent e)
      {
         repaint();
       }
 }


creating a java desktop pane

This Java program is used for creating a Desktop pane. Copy the following code and paste on a note pad, save it as "createJDesktoPane.java" and run the program.

//Java Core Package
import javax.swing.*;
//Java Extension Package
import java.awt.*;
import java.awt.event.*;

public class createJDesktoPane extends JFrame {

 //Initializing program components
 private JDesktopPane desktopTest;
 private JLabel labels[];
 private JTextField inputs[];
 private JButton buttons[];
 private String labelName[]={"Enter Name: ","Enter Age: ","Enter Address: ","Enter Mobile#: "};
 private String buttonName[] = {"Open","Save","Exit"};
 private JPanel panel1, panel2;

 //Setting up GUI
    public createJDesktoPane() {
    
     //Setting up the Title of the Window
     super("Creating a JDesktopPane");

     //Set Size of the Window (WIDTH, HEIGHT)
     setSize(600,500);

     //Exit Property of the Window
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     JMenuBar bar = new JMenuBar(); //Constructing JMenuBar
     JMenu menu = new JMenu("File"); //Constructing JMenu name "File"
     JMenuItem newFile = new JMenuItem("Add New Data"); //Constructing JMenuItem with "Add New Data" label
    
     menu.add(newFile); //Adding JMenuItem in the JMenu
     bar.add(menu); //Adding JMenu in the JMenuBar
    
  setJMenuBar(bar); //Adding JMenuBar in the container
 
  desktopTest = new JDesktopPane(); //Creating a JDesktopPane
  desktopTest.setBackground(Color.BLACK); //Setting JDesktopPane background color
 
     //Setting up the container ready for the components to be added.
     Container pane = getContentPane();
     setContentPane(pane);
    
     pane.add(desktopTest); //Adding JDesktopPane in the container
    
     //Implemeting Even-Listener on newFile JMenuItem
     newFile.addActionListener(
  new ActionListener() {
  
   //Handle JMenuItem "newFile" event if it is clicked
   public void actionPerformed(ActionEvent e) {
  
   //Constructing an Internal Frame inside JDesktopPane
   JInternalFrame frame = new JInternalFrame(null,true,true,true,true);
  
   Container container = frame.getContentPane(); //Creating a container inside the JInternalFrame
  
   //Constructing JLabel, JButton, and JTextField inside JInternalFrame
   labels = new JLabel[4];
   inputs = new JTextField[4];
   buttons = new JButton[3];
  
   //Creating a JPanel 1 with GridLayout of 4 rows and 2 columns inside JInternalFrame
   panel1 = new JPanel();
   panel1.setLayout(new GridLayout(4,2));
  
   //Constructing JLabel and JTextField using "for loop" and add to JPanel 1
   for(int count=0; count<labels.length && count<inputs.length; count++) {
    labels[count] = new JLabel(labelName[count]);
    inputs[count] = new JTextField(10);
    panel1.add(labels[count]);
    panel1.add(inputs[count]);
   }
  
   //Creating a JPanel 2 with GridLayout of 1 row and 3 columns inside JInternalFrame
   panel2 = new JPanel();
   panel2.setLayout(new GridLayout(1,3));
  
   //Constructing JButton using "for loop" and add to JPanel 2
   for(int count=0; count<buttons.length; count++) {
    buttons[count] = new JButton(buttonName[count]);
    panel2.add(buttons[count]);
   }
  
   //Adding JPanel 1 and 2 to the JInternalFrame container
   container.add(panel1,BorderLayout.NORTH);
   container.add(panel2,BorderLayout.CENTER);
  
   frame.setTitle("Add New Data"); //Set the Title of the JInternalFrame
   frame.setResizable(false); //Lock the size of the JInternalFrame
   frame.setMaximizable(false); //Disable the Maximize function of JInternalFrame
  
   //Set the size of JInternalFrame to the size of its content
   frame.pack();
  
   //Attached the JInternalFrame to JDesktopPane and show it by setting the visible in to "true"
   desktopTest.add(frame);
   frame.setVisible(true);
   }
  }
  );

     /**Set all the Components Visible.
      * If it is set to "false", the components in the container will not be visible.
      */
     setVisible(true);
    }
   
 //Main Method
    public static void main (String[] args) {
     createJDesktoPane dp = new createJDesktoPane();
 }
}