Validating 2 radio buttons on event
I've been working for a while on this. Here is my code (it doesn't work and I know why):
publicvoid actionPerformed(ActionEvent e){
if( e.getSource() == CelInButton && e.getSource() == CelOutButton){
output.setText("From Celcius to Celcius");
}
if( e.getSource() == CelInButton && e.getSource() == FahOutButton){
output.setText("From Celcius to Fahrenheit");
}
if( e.getSource() == CelInButton && e.getSource() == KelOutButton){
output.setText("From Celcius to Kelvin");
}
if( e.getSource() == FahInButton && e.getSource() == CelOutButton){
output.setText("From Fahrenheit to Celcius");
}
if( e.getSource() == FahInButton && e.getSource() == FahOutButton){
output.setText("From Fahrenheit to Fahrenheit");
}
if( e.getSource() == FahInButton && e.getSource() == KelOutButton){
output.setText("From Fahrenheit to Kelvin");
}
if( e.getSource() == KelInButton && e.getSource() == CelOutButton){
output.setText("From Kelvin to Celcius");
}
if( e.getSource() == KelInButton && e.getSource() == FahOutButton){
output.setText("From Kelvin to Fahrenheit");
}
if( e.getSource() == KelInButton && e.getSource() == KelOutButton){
output.setText("From Kelvin to Kelvin");
}
}
The reason it doesn't work because it would require both radio buttons to be selected at the exact same time as 1 event which isn't possible (at least in my mind it isn't).
Here is my application: [url]http://www.plastikhosting.net/uploads/tristanlee85/converter_box.png[/url]
I need the output to change ONLY when 2 of the buttons are selected. getSource() isn't what I need, but I can't find any other object that will work. What do I need for this to work right?
Yeh, but buttons are in 2 different groups. Here is my entire code:
/*
* Main.java
*
* Created on April 4, 2007, 4:19 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author tristan
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.*;
import java.applet.Applet;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Converter extends JPanel implements ActionListener {
private JTextField value;
static String CelInString = "癈";
static String FahInString = "癋";
static String KelInString = " K";
static String CelOutString = "癈";
static String FahOutString = "癋";
static String KelOutString = " K";
private JRadioButton CelInButton = new JRadioButton(CelInString);
private JRadioButton FahInButton = new JRadioButton(FahInString);
private JRadioButton KelInButton = new JRadioButton(KelInString);
private JRadioButton CelOutButton = new JRadioButton(CelOutString);
private JRadioButton FahOutButton = new JRadioButton(FahOutString);
private JRadioButton KelOutButton = new JRadioButton(KelOutString);
private JLabel Title = new JLabel("Enter in a value:");
private JLabel output = new JLabel("--Output here");
public Converter() {
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.setLayout(new BorderLayout());
p.setPreferredSize(new Dimension(200, 100));
p.setBackground(Color.gray);
CelInButton.setActionCommand(CelInString);
FahInButton.setActionCommand(FahInString);
KelInButton.setActionCommand(KelInString);
CelOutButton.setActionCommand(CelOutString);
FahOutButton.setActionCommand(FahOutString);
KelOutButton.setActionCommand(KelOutString);
value = new JTextField();
ButtonGroup radioIn = new ButtonGroup();
radioIn.add(CelInButton);
radioIn.add(FahInButton);
radioIn.add(KelInButton);
ButtonGroup radioOut = new ButtonGroup();
radioOut.add(CelOutButton);
radioOut.add(FahOutButton);
radioOut.add(KelOutButton);
//Register the actions
CelInButton.addActionListener(this);
FahInButton.addActionListener(this);
KelInButton.addActionListener(this);
CelOutButton.addActionListener(this);
FahOutButton.addActionListener(this);
KelOutButton.addActionListener(this);
// Put everything in boxes.
Box inputBox = Box.createVerticalBox();
Box valueBox = Box.createVerticalBox();
valueBox.add(Title);
valueBox.add(value);
Box radioBox = Box.createHorizontalBox(); // for the 2 radio button boxes
//Put the radioIn buttons in a column in a box.
Box radioInBox = Box.createVerticalBox();
JLabel from = new JLabel("From");
radioInBox.add(from);
radioInBox.add(CelInButton);
radioInBox.add(FahInButton);
radioInBox.add(KelInButton);
//Put the radioOut buttons in a column in a box.
Box radioOutBox = Box.createVerticalBox();
JLabel to = new JLabel("To");
radioOutBox.add(to);
radioOutBox.add(CelOutButton);
radioOutBox.add(FahOutButton);
radioOutBox.add(KelOutButton);
radioBox.add(radioInBox);
radioBox.add(Box.createHorizontalGlue()); // absorbs extra space
radioBox.add(radioOutBox);
Box outputBox = Box.createHorizontalBox();
outputBox.add(Box.createHorizontalGlue());
outputBox.add(output);
outputBox.add(Box.createHorizontalGlue());
inputBox.add(valueBox);
inputBox.add(radioBox);
inputBox.add(outputBox);
add(inputBox);
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Temperature Conversion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Converter();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
frame.setSize(200,200);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public void actionPerformed(ActionEvent e) {
if( e.getSource() == CelInButton && e.getSource() == CelOutButton) {
output.setText("From Celcius to Celcius");
}
if( e.getSource() == CelInButton && e.getSource() == FahOutButton) {
output.setText("From Celcius to Fahrenheit");
}
if( e.getSource() == CelInButton && e.getSource() == KelOutButton) {
output.setText("From Celcius to Kelvin");
}
if( e.getSource() == FahInButton && e.getSource() == CelOutButton) {
output.setText("From Fahrenheit to Celcius");
}
if( e.getSource() == FahInButton && e.getSource() == FahOutButton) {
output.setText("From Fahrenheit to Fahrenheit");
}
if( e.getSource() == FahInButton && e.getSource() == KelOutButton) {
output.setText("From Fahrenheit to Kelvin");
}
if( e.getSource() == KelInButton && e.getSource() == CelOutButton) {
output.setText("From Kelvin to Celcius");
}
if( e.getSource() == KelInButton && e.getSource() == FahOutButton) {
output.setText("From Kelvin to Fahrenheit");
}
if( e.getSource() == KelInButton && e.getSource() == KelOutButton) {
output.setText("From Kelvin to Kelvin");
}
}
}
Basically if "From *C" and "To *F" is selected, it'll change the output text (temporary; it will eventually a calculation). Now assuming "From *C" is still the same but the user selects "To K", then it will change the output as well to "From Celcius to Kelvin".