Monday 15 October 2012

java Combo Box

 
//JComboBox Demo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
<applet code="JComboBoxDemo" width=300 height=100>
</applet>
*/

public class JComboBoxDemo extends JApplet
implements ItemListener {

JLabel jl;
ImageIcon Flowers, Koala, Desert, Penguins, sateesh;

public void init(){
// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

// Create a Combo box and add it
//to the pane
JComboBox jc = new JComboBox();
jc.addItem("Flowers");
jc.addItem("Koala");
jc.addItem("Desert");
jc.addItem("Penguins");
jc.addItem("sateesh");
jc.addItemListener(this);
contentPane.add(jc);

//Create label
jl = new JLabel(new ImageIcon("Flowers.gif"));
contentPane.add(jl);
}

public void itemStateChanged(ItemEvent ie) {
String s = (String)ie.getItem();
jl.setIcon(new ImageIcon(s +".gif"));
}
}

Sunday 14 October 2012

Java JRadioButton Demo program

// JAVA Program  for demonstration of JRadioButton

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*
<applet code="JRadioButtonDemo" width=500 height=50>
</applet>
*/

public class JRadioButtonDemo extends JApplet
implements ActionListener {
JTextField jtf;

public void init(){

// Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

// ADD radio buttons to content pane
JRadioButton b1 = new JRadioButton("B.Sc");
b1.addActionListener(this);
contentPane.add(b1);

JRadioButton b2 = new JRadioButton("M.Sc");
b2.addActionListener(this);
contentPane.add(b2);

JRadioButton b3 = new JRadioButton("B.Tech");
b3.addActionListener(this);
contentPane.add(b3);

JRadioButton b4 = new JRadioButton("M.Tech");
b4.addActionListener(this);
contentPane.add(b4);

JRadioButton b5 = new JRadioButton("M.C.A");
b5.addActionListener(this);
contentPane.add(b5);

// Define a button group
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
bg.add(b4);
bg.add(b5);

// Add text field to the content pane
jtf = new JTextField(15);
contentPane.add(jtf);
}
public void actionPerformed(ActionEvent ae){
jtf.setText(ae.getActionCommand());
}
}



Saturday 13 October 2012

Checkbox in Java

This java program is used for displaying a clicked item in a text box

compiling: javac JCheckBoxDemo.java
Run: appletviewer JCheckBoxDemo.java

--------------------------------------------------


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/*

<applet code="JCheckBoxDemo" width=500 height=60>
</applet>

*/

public class JCheckBoxDemo extends JApplet
implements ItemListener {
JTextField jtf;

public void init(){

//Get content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

// Create icons
 ImageIcon normal = new ImageIcon ("normal.gif");
 ImageIcon rollover = new ImageIcon ("rollover.gif");
 ImageIcon selected = new ImageIcon ("selected.gif");

// Add check boxes to the content pane
JCheckBox cb = new JCheckBox("C", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);

cb = new JCheckBox("C++", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);

cb = new JCheckBox("Java", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);

cb = new JCheckBox("Perl", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);

cb = new JCheckBox("VB", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
contentPane.add(cb);


// Add text field to the content pane
jtf = new JTextField(15);
contentPane.add(jtf);
}

public void itemStateChanged(ItemEvent ie) {
JCheckBox cb = (JCheckBox)ie.getItem();
jtf.setText(cb.getText());
}
}





Sunday 7 October 2012

Java text filed :JTextField

 This program is for creating java text filed
compilation: javac JTextFieldDemo.java
Run: appletviewer JTextFieldDemo.java
-----------------------------------------------------
CODE:
-----------------------------------------------------

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

/* <applet code="JTextFieldDemo" width=200 height=50>
</applet>
*/

public class JTextFieldDemo extends JApplet{

public  void init() {

// Get content pane
Container contentPane = getContentPane();

//Creating a Text filed
JTextField jtf = new JTextField(JTextField.CENTER);


// Add text field to content pane
contentPane.add(jtf);

}
}
--------------------------------------------------------------
OUTPUT:
--------------------------------------------------------------
 

Friday 5 October 2012

JLabel and imageIcon


This swing java program is used for displaying image, label in an applet.

place a required picture in a folder, copy and paste the following code in a note pad and
save as "JLabelDemo.java" as the file name. Note that picture and java file must be stored
in the same folder.

compilation: javac JLabelDemo.java
run: appletviewer JLabelDemo.java


program code:
-------------------------------------------------------------------------------------------


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

/* <applet code="JLabelDemo" width=355 height=200>
</applet>
*/

public class JLabelDemo extends JApplet{

public void init() {
// Get content pane
Container contentPane = getContentPane();

// Create an icon
ImageIcon ii = new ImageIcon("sateesh.jpg");

// Create a label
JLabel jl = new JLabel ("SATEESH.BAGADHI", ii, JLabel.CENTER);

// Add label to the content pane
contentPane.add(jl);
}
}
 
-----------------------------------------------------------------------------------------
output:

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();
    }
}