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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With