-2

I keep getting the above error, I'm sure it's something simple but I can't figure it out even after looking it up. I'm also having some problems with the menubar, but I dont care about that right now. From what I understand, in order to return all those numbers in the actionPerformed method it cant be void. But when I change it to int I get the error stated in the title. When it was void and not int, the error changed to "cannot return when void."

From others who posted this problem it seemed like they werent using actionlistener when they declared it, but I think I am.

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.util.*;

     public class Widget extends JFrame implements ActionListener
  {
DataOutputStream output;

String[] styleStrings = { "Economy", "Standard", "Advanced", "Exceptional" };
String[] colorStrings = { "Red", "Green", "Blue", "Yellow" };

int economyCount, standardCount, advancedCount, exceptionalCount, redCount, greenCount, blueCount, yellowCount, entryValue;


JPanel economyPanel = new JPanel();
JPanel standardPanel = new JPanel();
JPanel advancedPanel = new JPanel();
JPanel exceptionalPanel = new JPanel();
JPanel redPanel = new JPanel();
JPanel greenPanel = new JPanel();
JPanel bluePanel = new JPanel();
JPanel yellowPanel = new JPanel();

JLabel selectStyle = new JLabel("Select a style: ");
    JComboBox styleList = new JComboBox(styleStrings);

JLabel selectColor = new JLabel("Select a color: ");
    JComboBox colorList = new JComboBox(colorStrings);

JLabel enterAmount = new JLabel("Enter amount: ");
    JTextField enterField = new JTextField(5);

JButton submitButton = new JButton("Submit");

JLabel blankLabel = new JLabel("");

JLabel totalLabel = new JLabel("Total Widgets:");

JLabel economyLabel = new JLabel("Economy: " + economyCount);
JLabel standardLabel = new JLabel("Standard: " + standardCount);
JLabel advancedLabel = new JLabel("Advanced: " + advancedCount);
JLabel exceptionalLabel = new JLabel("Exceptional: " + exceptionalCount);

JLabel redLabel = new JLabel("Red: " + redCount);
JLabel greenLabel = new JLabel("Green: " + greenCount);
JLabel blueLabel = new JLabel("Blue: " + blueCount);
JLabel yellowLabel = new JLabel("Yellow: " + yellowCount);



public static void main(String[] args)
{
    Widget w = new Widget();
    w.setSize(500, 500);
    w.setTitle("ABC Computers Inventory Management System");
    w.setResizable(true);
    w.setLocation(300,300);
    w.setVisible(true);
    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public Widget()
{
    Container c = getContentPane();
    c.setLayout((new GridLayout(9,2)));

    FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT);
        economyPanel.setLayout(rowSetup);
        standardPanel.setLayout(rowSetup);
        advancedPanel.setLayout(rowSetup);
        exceptionalPanel.setLayout(rowSetup);
        redPanel.setLayout(rowSetup);
        greenPanel.setLayout(rowSetup);
        bluePanel.setLayout(rowSetup);
        yellowPanel.setLayout(rowSetup);

    economyPanel.add(economyLabel);

    standardPanel.add(standardLabel);

    advancedPanel.add(advancedLabel);

    exceptionalPanel.add(exceptionalLabel);

    redPanel.add(redLabel);

    greenPanel.add(greenLabel);

    bluePanel.add(blueLabel);

    yellowPanel.add(yellowLabel);

    c.add(selectStyle);
    c.add(selectColor);
    c.add(styleList);
    c.add(colorList);
    c.add(enterAmount);
    c.add(enterField);
    c.add(submitButton);
    c.add(blankLabel);
    c.add(totalLabel);
    c.add(blankLabel);
    c.add(economyPanel);
    c.add(redPanel);
    c.add(standardPanel);
    c.add(greenPanel);
    c.add(advancedPanel);
    c.add(bluePanel);
    c.add(exceptionalPanel);
    c.add(yellowPanel);

    styleList.setSelectedIndex(1);
    styleList.setEditable(false);
    styleList.addActionListener(this);

    colorList.setSelectedIndex(1);
    colorList.setEditable(false);
    colorList.addActionListener(this);

    submitButton.addActionListener(this);

    String filename = "widgettotals";
}

public JMenuBar createMenuBar()
{
    JMenuBar mnuBar = new JMenuBar();
    setJMenuBar(mnuBar);

    JMenu mnuFile = new JMenu("File", true);
        mnuFile.setMnemonic(KeyEvent.VK_F);
        mnuFile.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuFile);

    JMenuItem mnuFileExit = new JMenuItem("Exit");
        mnuFileExit.setMnemonic(KeyEvent.VK_X);
        mnuFileExit.setDisplayedMnemonicIndex(1);
        mnuFile.add(mnuFileExit);
        mnuFileExit.setActionCommand("Exit");
        mnuFileExit.addActionListener(this);

    JMenu mnuHelp = new JMenu("Help", true);
        mnuHelp.setMnemonic(KeyEvent.VK_E);
        mnuHelp.setDisplayedMnemonicIndex(0);
        mnuBar.add(mnuHelp);

    return mnuBar;
}

public int actionPerformed(ActionEvent e)
{
    economyCount = 0;
    standardCount = 0;
    advancedCount = 0;
    exceptionalCount = 0;
    redCount = 0;
    greenCount = 0;
    blueCount = 0;
    yellowCount = 0;

    String chosenStyle = (String)styleList.getSelectedItem();
    String chosenColor = (String)colorList.getSelectedItem();
    String entryNumbers = enterField.getText();
    int entryValue = Integer.parseInt(entryNumbers);

    if ("Submit".equals(e.getActionCommand()))
    {
        if (styleList.getSelectedItem() == "Economy")
            {
                economyCount = economyCount + entryValue;
            }

        if (styleList.getSelectedItem() == "Standard")
            {
                standardCount = standardCount + entryValue;
            }

        if (styleList.getSelectedItem() == "Advanced")
            {
                advancedCount = advancedCount + entryValue;
            }

        if (styleList.getSelectedItem() == "Exceptional")
            {
                exceptionalCount = exceptionalCount + entryValue;
            }

        if (colorList.getSelectedItem() == "Red")
            {
                redCount = redCount + entryValue;
            }

        if (colorList.getSelectedItem() == "Green")
            {
                greenCount = greenCount + entryValue;
            }

        if (colorList.getSelectedItem() == "Blue")
            {
                blueCount = blueCount + entryValue;
            }

        if (colorList.getSelectedItem() == "Yellow")
            {
                yellowCount = yellowCount + entryValue;
            }
    }

    return economyCount; return standardCount; return advancedCount; return exceptionalCount; 
    return redCount; return greenCount; return blueCount; return yellowCount;
}   
   }
SoupBones
  • 25
  • 4

1 Answers1

2
public int actionPerformed(ActionEvent e)

This method should not return int, or anything for that matter. It should return void. When you override a method, you must keep the same signature (with a few exceptions - the return type not being one of them)

You are getting this "cannot return when void." error when changing to void because a void method should not return anything, and you are trying to return int values. You need to get rid of the returning values.

On top of that, you are trying to return a million things. Only one item/object/value is allowed to be returned.

If those values you are trying to return are supposed to have affect on the rest of the ui, then make those changes in the actionPerformed method


Side notes:

  • See How do I compare Strings in Java to help you out with this

    if (styleList.getSelectedItem() == "Economy")
    
  • Make use of the @Override annotation, so you know if you are properly overriding a method. If you aren't and you are implementing an interface (i.e. `ActionListener), and your class is not abstract, you will get the error you are currently receiving

    @Override
    public void actionPerformed(ActionEvent e) {}
    
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Thank you very much for your help so far, I've implemented the change to .equals() instead of == and removed the all the returns. I have a question though, when you say "make those changes in the actionPerformed method," do you mean move the code from the Widget() method into the actionPerformed method? Thank you again for your help. – SoupBones May 19 '14 at 01:06
  • I cant really answer than, unless I know what you were trying to accomplish by return those values. What were you expecting to happen? – Paul Samsotha May 19 '14 at 02:29
  • OK I see what you are trying to do. Use `label.setText()`. After you increase the count, just use `setText` on the corresonding label. Like `economyCount = economyCount + entryValue; economyLabel.setText("Economy: " + economyCount);` And do that for all the other labels. No need to return anything – Paul Samsotha May 19 '14 at 02:35
  • Perfect! Thats exactly what I needed, I thought it was going to be way more complicated than that. You're a lifesaver! – SoupBones May 19 '14 at 02:40