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?

[3025 byte] By [tristanlee85a] at [2007-11-27 0:13:37]
# 1

It looks like you have two sets of JRadioButtons. Are the two sets of buttons added

to different ButtonGroups? If not, they should be.

Then your actionPerformed() method ignores e.getSource(). Instead it finds out

which button is selected in each of the button groups and sets the text accordingly.

pbrockway2a at 2007-7-11 21:57:36 > top of Java-index,Java Essentials,Java Programming...
# 2

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".

tristanlee85a at 2007-7-11 21:57:36 > top of Java-index,Java Essentials,Java Programming...
# 3

So make the button groups instance variables - ie declare them up where you

declare the buttons, not in the constructor. Then, in actionPerformed() use these

button groups and not e.getSource() to determine the text.

The ButtonGroup API documentation is here:

http://java.sun.com/javase/6/docs/api/javax/swing/ButtonGroup.html

You are looking for some way to get the selection from each group.

Alternatively you could just use the JButton API to see how to figure out which

buttons are selected. Either way once you have the two buttons you can figure out

what the text should be.

pbrockway2a at 2007-7-11 21:57:36 > top of Java-index,Java Essentials,Java Programming...