Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Error: constant string expression required [duplicate]

i have 2 Java classes:

public abstract class IconNames {
/**
 *
 */
public static final String ButtonFett = java.util.ResourceBundle.getBundle("recources/buttonproperties").getString("fett");
}

and

public class EditorPanelActionListener implements ActionListener{
.
.
.
String buttonText = e.getActionCommand();
switch(buttonText)
    {
        case IconNames.ButtonFett: //Error: constant string expression required
            replace(XmlTags.BOLD);
            break;
    }
 .
 .
 .
 }

The EditorPanelActionListener fire the error "constant string expression required", whats the problem?

Thanks!

like image 413
rainer zufall Avatar asked Sep 05 '13 09:09

rainer zufall


1 Answers

You should not mix up program logic and user interface texts. The action command is a property distinct from the text shown and only defaults to the shown text if not set explicitly.

public abstract class IconNames {
  public static final String ButtonFett_CMD = "DO-BOLD";
  public static final String ButtonFett_TXT = java.util.ResourceBundle.getBundle("recources/buttonproperties").getString("fett");
}

JButton b=new JButton(IconNames.ButtonFett_TXT);
b.setActionCommand(IconNames.ButtonFett_CMD);

String buttonText = e.getActionCommand();
switch(buttonText)
{
    case IconNames.ButtonFett_CMD: // user language independent
        replace(XmlTags.BOLD);
        break;
}

This works for subclasses of AbstractButton which includes menu items as well. If you are dealing with Action implementations directly (which I doubt seeing your switch statement) you should differentiate between the Action.NAME and Action.ACTION_COMMAND_KEY property.

like image 195
Holger Avatar answered Oct 29 '22 10:10

Holger