Monday, 12 November 2012

JTabbedPaneDemo

 PROGRAM

import javax.swing.*;
/*
<applet code="JTabbedPaneDemo" width=400 height=100>
</applet>
 */
public class JTabbedPaneDemo extends JApplet {
public void init() {
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Cities", new CitiesPanel());
jtp.addTab("Colors", new ColorsPanel());
jtp.addTab("Flavors", new FlavorsPanel());
getContentPane().add(jtp);
}
}

class CitiesPanel extends JPanel  {
public CitiesPanel()  {
JButton b1 = new JButton("New York");
add(b1);
JButton b2 = new JButton("London");
add(b2);
JButton b3 = new JButton("Hong Kong");
add(b3);
JButton b4 = new JButton("Tokyo");
add(b4);
}
}

class ColorsPanel extends JPanel  {
public ColorsPanel()  {
JCheckBox cb1 = new JCheckBox("RED");
add(cb1);
JCheckBox cb2 = new JCheckBox("Green");
add(cb2);
JCheckBox cb3 = new JCheckBox("Blue");
add(cb3);
}
}

class FlavorsPanel extends JPanel {
public FlavorsPanel() {
JComboBox jcb = new JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}
}

Execution



    

    
 


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: