Java error: constant string expression required

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 PanelActionListener editor fires a "persistent string expression" error, what is the problem?

Thanks!

+6
source share
1 answer

Do not mix program logic and user interface texts. An action command is a property other than the displayed text, and only the displayed text is displayed by default if it is not explicitly specified.

 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 that also contain menu items. If you are dealing with Action implementations directly (which I doubt you see the switch statement), you must distinguish between the properties Action.NAME and Action.ACTION_COMMAND_KEY .

+2
source

Source: https://habr.com/ru/post/953226/


All Articles