Search code examples
javaswingjpanelextendjoptionpane

Error on .showMessageDialog in my JPanel?


I'm creating a Combination/permutation calculator. I'm in the process of making the GUI, and just tried making the

public Combination() 

and put the formulas inside.

Here's a snippet of code

 public long Combination() {
    String ncString = nchooseField.getText();
    String rcString = rchooseField.getText();
    int ncint = 0;
    int rcint = 0;

    try {
        ncint = Integer.parseInt(ncString);
        rcint = Integer.parseInt(rcString);
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(this,"ERROR! The values for 'n' and 'r' \n must be positive integers");
        return 0;
    }

I'm getting an error right on the showMessageDialog. I'm assuming I can't use that for some reason? Perhaps JOptionPane.showMessageDialog doesn't work in when extending a JPanel?

Thanks


Solution

  • You have to pass a GUI component (container) that's called this message dialog for the first parameter in the showMessageDialog method, but if you class wasn't a GUI component then you can pass null value.

    try this

    JOptionPane.showMessageDialog(null,"ERROR! The values for 'n' and 'r' \n must be positive integers");