Login    New User    Forgot Password    Help      

Radio Button

Java Tutorials

Example

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

public class swing3 extends JApplet implements ActionListener
{
JTextField tf;

public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

JRadioButton b1 = new JRadioButton("A");
b1.addActionListener(this);
contentPane.add(b1);

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

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

 

ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);

tf = new JTextField(5);
contentPane.add(tf);
}
public void actionPerformed(ActionEvent ae)
{
tf.setText(ae.getActionCommand());
}
}

Output

Output in applet

TOP

Combo Boxes

A JComboBox, which lets the user choose one of several choices, can have two very different forms. The default form is the uneditable combo box, which features a button and a drop-down list of values. 

Example

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

 

public class swing4 extends JApplet
implements ItemListener
{
JLabel jl;

ImageIcon France, Germany, Italy, Japan;

public void init()
{

Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());

JComboBox jc = new JComboBox();

jc.addItem("France");
jc.addItem("Germany");
jc.addItem("Italy");
jc.addItem("Japan");
jc.addItemListener(this);
contentPane.add(jc);

jl = new JLabel(new ImageIcon("image1.jpg"));
contentPane.add(jl);
}

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

Output

Java Tutorials