Best way to add SyntaxConstants into ComboBox?

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Best way to add SyntaxConstants into ComboBox?

Postby Endogen » Sat Jan 04, 2014 10:35 am

First of all, this is a great library! I found this thing a week ago and really enjoy using it. Thanks for all the great work!

What i try to do is, add all the available constants in "SyntaxConstants" into a JComboBox so that i have the possibility to choose the highlighting that i want to use for my text. Also i don't necessarily want to see the exact same values as in "SyntaxConstants". Something like "Java" or "Delphi" would be great!
Could someone give me some ideas how to achieve this?
Posts: 5
Joined: Sat Jan 04, 2014 10:18 am

Re: Best way to add SyntaxConstants into ComboBox?

Postby robert » Wed Jan 08, 2014 1:36 pm

You'd have to roll your own to do that. One way is perhaps a HashMap mapping user-friendly Strings for language names to the SyntaxConstants property names.

You might be able to programmatically get "user-friendly" names from the constant values. It's up to you if they're "friendly" enough. Something like this:

java code:

Field[] fields = SyntaxConstants.class.getFields();
for (Field field : fields) {
String name = field.getName();
String userFriendlyName = name.substring("SYNTAX_STYLE_".length());
// Sentence-case it
boolean lowerCaseNext = true;
StringBuilder sb = new StringBuilder();
for (int i=1; i<userFriendlyName.length(); i++) {
char ch = userFriendlyName.charAt(i);
if (ch=='_') {
sb.append(' ');
lowerCaseNext = false;
else {
if (lowerCaseNext) {
ch = Character.toLowerCase(ch);
else {
lowerCaseNext = true;
userFriendlyName = sb.toString();
User avatar
Posts: 844
Joined: Sat May 10, 2008 5:16 pm

Return to Help

Who is online

Users browsing this forum: No registered users and 1 guest