Total members 11894 |It is currently Sat Nov 23, 2024 8:55 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





application that displays two JRadioButtons
java code
/*
* Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for NON-COMMERCIAL purposes and without
* fee is hereby granted provided that this copyright notice
* appears in all copies. Please refer to the file "copyright.html"
* for further important copyright and licensing information.
*
* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.JRadioButton;
import com.sun.java.swing.ButtonGroup;
import com.sun.java.swing.JPanel;
import com.sun.java.swing.JFrame;
import com.sun.java.swing.event.ChangeListener;
import com.sun.java.swing.event.ChangeEvent;

/**
* An application that displays two JRadioButtons. The JRadioButtons
* determine the look and feel used by the application.
*/
public class RadioButtonDemo extends JPanel {
static JFrame frame;
static String first = new String("Button 1");
static String second = new String("Button 2");

public RadioButtonDemo() {
// Create the buttons.
JRadioButton firstButton = new JRadioButton(first);
firstButton.setKeyAccelerator('1');
firstButton.setActionCommand(first);
firstButton.setSelected(true);

JRadioButton secondButton = new JRadioButton(second);
secondButton.setKeyAccelerator('2');
secondButton.setActionCommand(second);

// Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(firstButton);
group.add(secondButton);

// Register a listener for the radio buttons.
RadioListener myListener = new RadioListener();
firstButton.addActionListener(myListener);
firstButton.addChangeListener(myListener);
firstButton.addItemListener(myListener);
secondButton.addActionListener(myListener);
secondButton.addChangeListener(myListener);
secondButton.addItemListener(myListener);

add(firstButton);
add(secondButton);
}


/** Listens to the radio buttons. */
class RadioListener implements ActionListener, //only one event type needed
ChangeListener, //for curiosity only
ItemListener { //for curiosity only
public void actionPerformed(ActionEvent e) {
String factoryName = null;

System.out.print("ActionEvent received: ");
if (e.getActionCommand() == first) {
System.out.println(first + " pressed.");
} else {
System.out.println(second + " pressed.");
}
}

public void itemStateChanged(ItemEvent e) {
System.out.println("ItemEvent received: "
+ e.getItem()
+ " is now "
+ ((e.getStateChange() == ItemEvent.SELECTED)?
"selected.":"unselected"));
}

public void stateChanged(ChangeEvent e) {
System.out.println("ChangeEvent received from: "
+ e.getSource());
}
}

public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};

frame = new JFrame("RadioButtonDemo");
frame.addWindowListener(l);
frame.getContentPane().add("Center", new RadioButtonDemo());
frame.pack();
frame.setVisible(true);
}
}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : JRadioButtons- Example- displays two JRadioButtons
 C program that displays shapes. Keep getting errors.     -  



Topic Tags

Java Swing
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com