Sunday 11 September 2011

ADVANCED JAVA CODE FOR GTU 7 TH SEM

,
YOU CAN FIND ALL THE REQUIRED CODE OF ADVANCED JAVA FROM THIS .
THIS IS VERY GOOD CD  FOR ADVANCED JAVA.
CLICK HERE TO DOWNLOAD:

Online shopping form in java

,
//Online shopping form in java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyForm extends JApplet implements ActionListener
{
    //vars
    String str="", str1="", str2="";
    Object x[];
    JLabel n,a,i,lbl;
    JTextField name;
    JTextArea addr;
    JList lst;
    JButton b1,b2;
    Container c;
    public void init()
    {
        //create JFrame and container
        JFrame jf = new JFrame();
        c = jf.getContentPane();
        //display yellow background color in container
        c.setBackground(Color.yellow);
        //do not set any layout to c
        c.setLayout(null);
        //set the size and title for frame
        jf.setSize(500,400);
        jf.setTitle("My Form");
        //display the frame
        jf.setVisible(true);
        //Display heading in the frame using a label
        Font f = new Font("Dialog",Font.BOLD,26);
        lbl = new JLabel();
        lbl.setFont(f);
        lbl.setForeground(Color.red);
        lbl.setText("Z-ELECTRONICS ONLINE SHOP");
        lbl.setBounds(200,10,500,50);
        c.add(lbl);
        //TextField and a label for entering name
        n = new JLabel("Name: ", JLabel.LEFT);
        name = new JTextField(30);
        n.setBounds(50,100,100,30);
        name.setBounds(200,100,200,30);
        c.add(n);
        c.add(name);
        //TextArea and a label for entering address
        a = new JLabel("Address: ", JLabel.LEFT);
        addr = new JTextArea(5,50);
        a.setBounds(50,150,100,30);
        addr.setBounds(200,150,200,100);
        c.add(a);
        c.add(addr);
//List box for multiple selection
        i = new JLabel("Select items: ", JLabel.LEFT);
String[] data = {"TVs", "Washing machines", "DVD players",
        "Refrigerators"};
        lst = new JList(data);
        i.setBounds(50,270,100,30);
        lst.setBounds(200,270,200,100);
        c.add(i);   
        c.add(lst);
//add Two push buttons: OK and Cancel
        b1 = new JButton("OK");
        b2 = new JButton("Cancel");
        b1.setBounds(200,400,100,30);
        b2.setBounds(350,400,100,30);
        c.add(b1);
        c.add(b2);
        //add listeners to buttons
        b1.addActionListener(this);
        b2.addActionListener(this);
    }
    //this method is executed when the buttons are clicked
    public void actionPerformed(ActionEvent ae)
    {
        //know which button is clicked
        str = ae.getActionCommand();
        //if the button label is OK then
        if(str.equals("OK"))
        {
            //retrieve data from text field, text area and list boxes
            str1= name.getText()+"\n";
            str1+= addr.getText()+"\n";
            x = lst.getSelectedValues();   
            for(int i=0;i<x.length; i++)
str2 +=(String)x[i]+"\n";    
            //display the data in text area
            addr.setText(str1+str2);

            //make the strings empty
            str1="";

   str2="";
        }
        else{

            //if Cancel button is clicked, clear the data in the form
            name.setText("");
            addr.setText("");
            lst.clearSelection();
        }
    }
}

CLICK HERE TO DOWNLOAD MORE CODE:

http://www.ziddu.com/download/16351028/advancedjavabookcd.rar.html

Changing the look and feel of components in java

,
//Changing the look and feel of components in java swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.plaf.*;
class LookFeel extends JFrame implements ItemListener
{
    //vars
    JButton b;
    JCheckBox cb;
    JTextField t;
    JRadioButton r1, r2, r3;
    ButtonGroup bg;
    Container c;

    LookFeel()
    {
        //create content pane
        c = this.getContentPane();

        //set flow layout to c
        c.setLayout(null);

        //create components
        b = new JButton("Button");
        cb = new JCheckBox("CheckBox");
        t = new JTextField("TextField", 15);
        r1 = new JRadioButton("Metal");
        r2 = new JRadioButton("Motif");
        r3 = new JRadioButton("Windows");

        //create ButtonGroup object and add radio buttons to specify
        //that they belong to same group
        bg = new ButtonGroup();
        bg.add(r1);
        bg.add(r2);
        bg.add(r3);

        //set the location of components in content pane
        b.setBounds(100,50,75,40);
        cb.setBounds(100,100,100,40);
        t.setBounds(100,150,100,40);
        r1.setBounds(50,250,100,30);
        r2.setBounds(150,250,100,30);
        r3.setBounds(250,250,100,30);

        //add the components to content pane
        c.add(b);
        c.add(cb);
        c.add(t);
        c.add(r1);
        c.add(r2);
        c.add(r3);
        //add item listeners to radio buttons
        r1.addItemListener(this);
        r2.addItemListener(this);
        r3.addItemListener(this);

        //close the frame
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void itemStateChanged(ItemEvent ie)
    {
       
        try{
         //know which radio button is selected and accordingly change
         //the look and feel

        if(r1.getModel().isSelected())
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        if(r2.getModel().isSelected())
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
        if(r3.getModel().isSelected())
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

        //change the look and feel in the content pane
        SwingUtilities.updateComponentTreeUI(c);
        }catch(Exception e){}
     }

    public static void main(String args[])
    {
        //create the frame
        LookFeel lf = new LookFeel();
        lf.setSize(400,400);
        lf.setTitle("Look and Feel");
        lf.setVisible(true);
    }
}
CLICK HERE TO DOWNLOAD MORE CODE:

http://www.ziddu.com/download/16351028/advancedjavabookcd.rar.html

A toggle button with start and stop images

,
//A toggle button with start and stop images in java swing
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class JTButton extends JFrame implements ActionListener
{
    //vars
    JToggleButton but;
    ImageIcon img1;
 
    JTButton()
    {
        //create content pane with flow layout
        Container c = getContentPane();
        c.setLayout(new FlowLayout());
 
        //image with start signal
        img1 = new ImageIcon("start.jpg");

        //create toggle button with start image
        but = new JToggleButton("Start/Stop", img1);

        //add button to content pane
        c.add(but);

        //add action listener to button
        but.addActionListener(this);
    }

    public void actionPerformed(ActionEvent ae)
    {
        //image with stop signal
        ImageIcon img2 = new ImageIcon("stop.jpg");

        //if toggle button is selected display stop signal image
        //else display start signal image
        if(but.isSelected())
        but.setIcon(img2);
        else but.setIcon(img1);
  
    }

    public static void main(String args[])
    {
        //create the frame
        JTButton demo = new JTButton();
        demo.setSize(400,400);
        demo.setVisible(true);
        demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
CLICK HERE TO DOWNLOAD MORE CODE:

http://www.ziddu.com/download/16351028/advancedjavabookcd.rar.html

Check boxes, radio buttons and Text area

,
//Check boxes, radio buttons and Text area in java swing
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class CheckRadio extends JFrame implements ActionListener
{
    //vars
    JCheckBox cb1, cb2;
    JRadioButton rb1, rb2;
    JTextArea ta;
    ButtonGroup bg;
    String msg="";

    CheckRadio()
    {
        //create the content pane
        Container c = getContentPane();

        //set flow layout to content pane
        c.setLayout(new FlowLayout());

        //create a text area with 10 rows and 20 chars per row
        ta = new JTextArea(10,20);

        //create two check boxes
        cb1 = new JCheckBox("Java", true);
        cb2 = new JCheckBox("J2EE");

        //create two radio buttons
        rb1 = new JRadioButton("Male", true);
        rb2 = new JRadioButton("Female");

        //create a button group and add the radio buttons to it
        bg = new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);

        //add the checkboxes, radio buttons, textarea to the container
        c.add(cb1);
        c.add(cb2);
        c.add(rb1);
        c.add(rb2);
        c.add(ta);

        //add action listeners. We need not add listener to text area
        //since the user clicks on the checkboxes or radio buttons only
        cb1.addActionListener(this);
        cb2.addActionListener(this);
        rb1.addActionListener(this);
        rb2.addActionListener(this);
   
        //close the frame upon clicking
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent ae)
    {
        //know which components are selected by user
        if(cb1.getModel().isSelected()) msg+="\nJava";
        if(cb2.getModel().isSelected()) msg+="\nJ2EE";    
        if(rb1.getModel().isSelected()) msg+="\nMale";
        else msg+="\nFemale";
        //display the selected message in text area
        ta.setText(msg);

        //reset the message to empty string
        msg="";
     }

    public static void main(String args[])
    {
        //create frame
        CheckRadio cr = new CheckRadio();
        cr.setTitle("My checkboxes and Radio buttons");
        cr.setSize(500,400);
        cr.setVisible(true);
    }
}
CLICK HERE TO DOWNLOAD MORE CODE:

http://www.ziddu.com/download/16351028/advancedjavabookcd.rar.html

BUTTON DEMO IN JAVA SWING

,
//Button with an image, colors, border, tool tip text and shortcut key IN JAVA SWING
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
class ButtonDemo extends JFrame
{
    JButton b;
    ButtonDemo()
    {
        //create container
        Container c = getContentPane();
        //set a layout for container
        c.setLayout(new FlowLayout());

        //store the image into ImageIcon object
        ImageIcon ii = new ImageIcon("car.jpg");

        //create the button with the image
        b = new JButton("Click Me", ii);

        //set background and foreground colors for button
        b.setBackground(Color.yellow);
        b.setForeground(Color.red);

        //set font for the label of button
        b.setFont(new Font("Arial", Font.BOLD, 30));

        //set bevel border for button
        Border bd = BorderFactory.createBevelBorder(BevelBorder.RAISED);
        b.setBorder(bd);
        //set tool tip text for button
        b.setToolTipText("This is a button");
        //set a short cut key for button. Alt+C from keyboard will invoke
         //the button
        b.setMnemonic('C');
        //add the button to the container
        c.add(b);
        //close the frame upon clicking
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public static void main(String args[])
    {
        //create a frame
        ButtonDemo obj = new ButtonDemo();
        obj.setTitle("My Button");;
        obj.setSize(500,400);
        obj.setVisible(true);
    }
}

BORDER DEMO IN JAVA SWING

,
 THE CODE IS IN JAVA.

//Understanding the borders
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

class BorderDemo extends JFrame
{
    //vars
    JButton b1,b2,b3,b4,b5,b6,b7,b8;

    BorderDemo()
    {
        //create content pane c
        Container c = getContentPane();

        //set a layout for content pane
        c.setLayout(new FlowLayout());

        //Create push buttons
        b1 = new JButton("Raised Bevel Border");
        b2 = new JButton("Lowered Bevel Border");
        b3 = new JButton("Raised Etched Border");
        b4 = new JButton("Lowered Etched Border");
        b5 = new JButton("Line Border");
        b6 = new JButton("Matte Border");
        b7 = new JButton("Compound Border");
        b8 = new JButton("Empty Border");

        //set raised bevel border for b1 with high light color:
          //red and shadow color: green
        Border bd = BorderFactory.createBevelBorder(BevelBorder.RAISED,
         Color.red, Color.green);
        b1.setBorder(bd);

        //set lowered bevel border for b2 with its current background
         //color for high light and shadow
        bd = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
        b2.setBorder(bd);

        //set raised etched border for b3 with high light color: red
         //and shadow color: green


        bd = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
         Color.red, Color.green);
        b3.setBorder(bd);

        //set lowered etched border for b4 with its current background
         //color for highlight and shadow
        bd = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        b4.setBorder(bd);

        //set line border for b5 with red color and width 5 px
        bd = BorderFactory.createLineBorder(Color.red, 5);
        b5.setBorder(bd);

        //set matte border for b6 with top, left, bottom, right widths as
         //5,10,15,20 px and in red color
        bd = BorderFactory.createMatteBorder(5,10,15,20, Color.red);
        b6.setBorder(bd);

        //set compound border for b7 without any borders inside or outside
         //edges
        bd = BorderFactory.createCompoundBorder();
        b7.setBorder(bd);

        //set empty border for b8 without any space for border
        bd = BorderFactory.createEmptyBorder();
        b8.setBorder(bd);
        //add the buttons to the container
        c.add(b1);
        c.add(b2);
        c.add(b3);
        c.add(b4);
        c.add(b5);
        c.add(b6);
        c.add(b7);
        c.add(b8);
        //close the frame upon clicking
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[])
    {
        //create a frame
        BorderDemo obj = new BorderDemo();
        //set the title and size for frame
        obj.setTitle("Borders");;
        obj.setSize(500,400);
        //display the frame
        obj.setVisible(true);
    }

}