tophe97
tophe97

Reputation: 1

If and else GUI. having only one output in all of else if

this is my problem.... BUILDING GUI

i have 2 other class named DistanceConverter and VolumeConverter. i set their action command as String named chkDistance and chkVolume, now i can use if and else...

but the problem is in if and else the output is only one.. the only thing that outputs is the chkVolume. and whenever i select the chkDistance the output is still the chkVolume..

this is my code.... import java.awt.Checkbox; import javax.swing.JOptionPane; import javax.swing.JRadioButton;

public class Converter extends javax.swing.JFrame {
String chkDistance;
String chkVolume;

public Converter() {
    initComponents();
}

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    buttonGroup = new javax.swing.ButtonGroup();
    jLabel1 = new javax.swing.JLabel();
    myButton1 = new javax.swing.JRadioButton();
    MyButton2 = new javax.swing.JRadioButton();
    mainButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 0, 204));
    setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

    jLabel1.setBackground(new java.awt.Color(153, 153, 255));
    jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
    jLabel1.setForeground(new java.awt.Color(0, 0, 102));
    jLabel1.setText("CONVERTER");

    buttonGroup.add(myButton1);
    myButton1.setText("Distance");
    myButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            myButton1ActionPerformed(evt);
        }
    });

    MyButton2.setBackground(new java.awt.Color(240, 240, 241));
    buttonGroup.add(MyButton2);
    MyButton2.setText("Volume");
    MyButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            MyButton2ActionPerformed(evt);
        }
    });

    mainButton.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
    mainButton.setText("OK");
    mainButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            mainButtonActionPerformed(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()
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addGroup(layout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(myButton1)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(12, 12, 12)
                            .addComponent(mainButton))
                        .addComponent(MyButton2))))
            .addContainerGap(26, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addGap(26, 26, 26)
            .addComponent(MyButton2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(myButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
            .addComponent(mainButton)
            .addGap(35, 35, 35))
    );

    pack();
}// </editor-fold>                        

private void myButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        myButton1= new JRadioButton();
    myButton1.setActionCommand(chkDistance);
}                                         

private void MyButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                          

    MyButton2= new JRadioButton();
    MyButton2.setActionCommand(chkVolume);

}                                         

private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

    String command = buttonGroup.getSelection().getActionCommand();

here is my if and else.......... only the variable that is in if statements output and whenever i select other option like chkDistance the output is also in the volume..

    if (command == chkVolume){
        VolumeConverter volume = new VolumeConverter();
        volume.setVisible(true);
    }
     else  if(command == chkDistance){
       DistanceConverter distance = new DistanceConverter();
       distance.setVisible(true);
    }
     else{
         return;
     }
}                                          

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(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Converter.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Converter().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JRadioButton MyButton2;
private javax.swing.ButtonGroup buttonGroup;
private javax.swing.JLabel jLabel1;
private javax.swing.JButton mainButton;
private javax.swing.JRadioButton myButton1;
// End of variables declaration                   

}

but when i change their position example.. the if statement is having the value of chkDistance.. the output that will be open is all about the distance even i select the volume

     if(command == chkDistance){
       DistanceConverter distance = new DistanceConverter();
       distance.setVisible(true);
    }
else if (command == chkVolume){
        VolumeConverter volume = new VolumeConverter();
        volume.setVisible(true);
    }
     else{
         return;
     }

Upvotes: 0

Views: 33

Answers (1)

David Lavender
David Lavender

Reputation: 8311

You haven't initialised chkDistance and chkVolume. So they are both null. Which means that both your actionCommands are actually bound to null. Which then means that if (command == chkVolume){ always returns true.

Initialise them to different values:

String chkDistance = "checkDistance";
String chkVolume = "checkVolume";

Upvotes: 1

Related Questions