Reputation: 500
So I've been making this GUI temperature converter for my college class and I'm almost done but I can't figure out why I can't get the button btnCalc to display the results from the different checkboxes. It'll display something if I do
if(ae.getSource()==btnCalc)
JOptionPane.showMessageDialog(this,"Button");
Anyway I would really appreciate any help if figuring this out. I just want the program to display the results after hitting the calculate button.
package TempConverter;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
/**
*
* @author 00220682
*/
public class TempConvertGUI extends javax.swing.JFrame implements ActionListener{
public TempConvertGUI() {
initComponents();
btnCalc.addActionListener(this);
degrees.addActionListener(this);
CtoF.addActionListener(this);
FtoC.addActionListener(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel1 = new javax.swing.JLabel();
degrees = new javax.swing.JTextField();
btnCalc = new javax.swing.JButton();
lblResults = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
CtoF = new javax.swing.JCheckBox();
FtoC = new javax.swing.JCheckBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Temperate Converter");
jLabel1.setText("Enter The Degrees:");
degrees.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
degreesActionPerformed(evt);
}
});
btnCalc.setText("Calculate");
btnCalc.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnCalcMouseClicked(evt);
}
});
btnCalc.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnCalcActionPerformed(evt);
}
});
jLabel2.setText("What would you like to convert?");
buttonGroup1.add(CtoF);
CtoF.setText("Celcius to Fahrenheit");
CtoF.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CtoFActionPerformed(evt);
}
});
buttonGroup1.add(FtoC);
FtoC.setText("Fahrenheit to Celcius");
FtoC.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
FtoCActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(degrees, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btnCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(FtoC)
.addGap(32, 32, 32)
.addComponent(CtoF))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 226, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)))))
.addGap(0, 0, Short.MAX_VALUE))
.addComponent(lblResults, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(degrees, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(FtoC)
.addComponent(CtoF))
.addGap(18, 18, 18)
.addComponent(btnCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(lblResults, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void degreesActionPerformed(java.awt.event.ActionEvent evt) {
CtoF.setEnabled(true);
FtoC.setEnabled(true);
}
private void FtoCActionPerformed(java.awt.event.ActionEvent evt) {
}
private void CtoFActionPerformed(java.awt.event.ActionEvent evt) {
}
private void btnCalcMouseClicked(java.awt.event.MouseEvent evt) {
}
private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TempConvertGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TempConvertGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TempConvertGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TempConvertGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TempConvertGUI().setVisible(true);
CtoF.setEnabled(false);
FtoC.setEnabled(false);
}
});
}
// Variables declaration - do not modify
public static javax.swing.JCheckBox CtoF;
public static javax.swing.JCheckBox FtoC;
static javax.swing.JButton btnCalc;
private javax.swing.ButtonGroup buttonGroup1;
public static javax.swing.JTextField degrees;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel lblResults;
// End of variables declaration
@Override
public void actionPerformed(ActionEvent ae) {
double display = 0;
double x = 0;
CtoF DegreesC = new CtoF(0);
FtoC DegreesF = new FtoC(0);
if(ae.getSource()==CtoF){
double DinC = Double.parseDouble(degrees.getText());
DegreesC.setDegrees(DinC);
display = DegreesC.CtoFconvert();
x = 1;
}
else if (ae.getSource()==FtoC){
double DinF = Double.parseDouble(degrees.getText());
DegreesF.setDegrees(DinF);
display = DegreesF.FtoCconvert();
x = 2;
}
if (ae.getSource() == btnCalc){
if (x == 1){
lblResults.setText("<html>Degrees in Celcius: " + degrees.getText() + "<br>Degrees in Fahrenheit: " + display);
}
else if (x == 2){
lblResults.setText("<html>Degrees in Fahrenheit: " + degrees.getText() + "<br>Degrees in Celcius: " + display);
}
}
}
}
Upvotes: 0
Views: 407
Reputation: 57192
I'm not sure I fully understand the issue, but see if this helps...
You are using the same ActionListener
on the checkboxes and the calculate button. Your constructor initializes these with
btnCalc.addActionListener(this);
degrees.addActionListener(this);
CtoF.addActionListener(this);
FtoC.addActionListener(this);
So when ANY of these are pressed, that action listener will fire. It may make more sense to add multiple smaller, more specific action listeners rather than one big one where you have to check the source. So btnCalc
's action listener would show the result and each of the other buttons would have their own action listeners that set the variables they need to set.
Upvotes: 1