-3

In the following code, when I execute it, only the first if block will work (when I press the button for the other options, no pop-ups happen). What am I doing wrong? This is a program designed to make an app that can convert numbers between binary, decimal, and hexadecimal.

public class Menu implements ActionListener {
    public static void main(String[] args) {
        new Menu().createMenu();
    }

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    JButton binToHex = new JButton("Bin to Hex");
    JButton binToDec = new JButton("Bin to Dec");
    JButton hexToBin = new JButton("Hex to Bin");
    JButton hexToDec = new JButton("Hex to Dec");
    JButton decToHex = new JButton("Dec to Hex");
    JButton decToBin = new JButton("Dec to Bin");

    private void createMenu() {
        frame.setVisible(true);
        frame.add(panel);
        panel.add(binToHex);
        panel.add(binToDec);
        panel.add(hexToBin);
        panel.add(hexToDec);
        panel.add(decToHex);
        panel.add(decToBin);
        frame.pack();
        binToHex.addActionListener(this);
            
    }

public void actionPerformed(ActionEvent e) {
    String input = new String();
    if (e.getSource() == binToHex) {
        input = JOptionPane.showInputDialog(null, "Enter a binary number");
        Binary.convertToHexadecimal();
    } 
    
    if (e.getSource() == binToDec) {
        input = JOptionPane.showInputDialog(null, "Enter a binary number");
        Binary.convertToDecimal();
    } 
    
    if (e.getSource() == hexToBin) {
        input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
        Hexadecimal.convertToBinary();
    }
    
    if (e.getSource() == hexToDec) {
        input = JOptionPane.showInputDialog(null, "Enter a hexadecimal number");
        Hexadecimal.convertToDecimal();
    }
    
    if (e.getSource() == decToHex) {
        input = JOptionPane.showInputDialog(null, "Enter a decimal number");
        Decimal.convertToHexadecimal(input);
    }
    
    if (e.getSource() == decToBin) {
        input = JOptionPane.showInputDialog(null, "Enter a decimal number");
        Decimal.convertToBinary(input);
    }
    
}

}

Mikaela
  • 1
  • 1
  • If you print out `e.getSource()` to see its value, does that help explain the behavior? – jarmod Oct 05 '20 at 22:54
  • What is the return type of `getSource()`? What type is `binToHex` and the others? If they are `String`, see: [How do I compare strings in Java?](https://stackoverflow.com/q/513832/5221149) – Andreas Oct 05 '20 at 22:58
  • @Andreas `binToHex` etc are `JButton`s - the source of the click event. – Bohemian Oct 05 '20 at 23:27
  • They're not strings. binToHex is a JButton. And for the e.getSource(), that call works for the first call so I don't know why it wouldn't for the second. – Mikaela Oct 06 '20 at 00:13
  • Comparing strings is done with String#equals() and not with ==. – NomadMaker Oct 06 '20 at 01:36

1 Answers1

3

You forgot to call addActionListener on all your JButton instances:

binToHex.addActionListener(this); // you have this one
binToDec.addActionListener(this);
hexToBin.addActionListener(this);
hexToDec.addActionListener(this);
decToHex.addActionListener(this);
decToBin.addActionListener(this);
zelus
  • 143
  • 8