-5

I am just trying to get a source of event by switch statement but I can't. I'm getting:

Constant expression required

What am I doing wrong? Is it even possible?

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener {

    JButton poteguj;
    JLabel podstawa, wykladnik, wynik;
    JTextField tPodstawa, tWykladnik;
    JCheckBox odwroc;
    int liczba1, liczba2;
    long wynikLiczb;

    public Main() {
        setTitle("Poteegpowanie");
        setSize(400, 250);
        setLayout(null);

        podstawa = new JLabel("Wprowadz podstawe:");
        podstawa.setBounds(20, 40, 150, 20);
        add(podstawa);

        tPodstawa = new JTextField("");
        tPodstawa.setBounds(170, 40, 150, 20);
        add(tPodstawa);

        wykladnik = new JLabel("Wprowadz wykladnik:");
        wykladnik.setBounds(20, 70, 150, 20);
        add(wykladnik);

        tWykladnik = new JTextField("");
        tWykladnik.setBounds(170, 70, 150, 20);
        add(tWykladnik);

        poteguj = new JButton("Poteguj!");
        poteguj.setBounds(130, 120, 100, 40);
        add(poteguj);
        poteguj.addActionListener(this);

        wynik = new JLabel("Wynik: ");
        wynik.setBounds(0, 160, 400, 20);
        add(wynik);

        odwroc = new JCheckBox("Odwroc dzialanie");
        odwroc.setBounds(130, 90, 150, 30);
        add(odwroc);
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        liczba2 = Integer.parseInt(tWykladnik.getText());
        liczba1 = Integer.parseInt(tPodstawa.getText());
        switch(source){
            case poteguj:
                if(!odwroc.isSelected()) {
                    wynikLiczb = (long) Math.pow(liczba1, liczba2);
                    wynik.setText(liczba1 + " do potegi " + liczba2 + " jest rowne: " + wynikLiczb);
                }
                else if(odwroc.isSelected()){
                    wynikLiczb = (int) Math.pow(liczba2, liczba1);
                    wynik.setText(liczba2 + " do potegi " + liczba1 + " jest rowne: " + wynikLiczb);
                }
            }
        }
    }
resueman
  • 10,572
  • 10
  • 31
  • 45

1 Answers1

1

The problem is that poteguj is not a constant. You can only use switch on constant values. You're stuck with the if, I'm afraid.

You could, perhaps, put your sources into a List and extract a constant value from that.

List<JButton> sourceList = new ArrayList<>();
// add your sources in a known order

int sourceIndex = sourceList.indexOf(source);
switch (sourceIndex) {
case 0: // poteguj
    // do something
    break;
case 1: // ???

But this sacrifices clarity for dogma. if-else if is not bad when that's what the problem looks like. Your code should ideally reflect your problem.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • There's no way to avoid it? If I add to the program other mathematical operations I will get a lot of "if". – Maciek Sienkiewicz Mar 15 '16 at 17:34
  • As @resueman has noted, the JLS is adamant on this point. But even if you could use `switch`, you'd end up with a lot of `case`, wouldn't you? The problem is equivalent. – Erick G. Hagstrom Mar 15 '16 at 17:36
  • So "if ladder" isn't a mistake in this case? I was taught to avoid it – Maciek Sienkiewicz Mar 15 '16 at 17:39
  • Avoiding it is preferable. But you can't avoid it here with a `switch`, or at least not by switching on `source`. Perhaps you could think of another way to avoid it? Hint: if you know your sources up front, you could perhaps assign them constants? Then a `switch` would be possible. Get creative with it. – Erick G. Hagstrom Mar 15 '16 at 17:42