0

I have a method that accepts only a String.

public void setVerticalAlignment(String align) {
            ...
    gd.verticalAlignment = align;   // accepts only int.
            ...
}

But gd.verticalAlignment only accepts an int. Usually this is set by something like gd.verticalAlignment = SWT.TOP where SWT.TOP is a static int.

is it possible to call this method with something like setVerticalAlignment("SWT.TOP")?

6
  • Why don't you just convert the string? Integer.parseInt(align); Commented Aug 22, 2012 at 8:25
  • 2
    Why do you use a String for the alignment at all?? Commented Aug 22, 2012 at 8:28
  • 1
    Change setVerticalAlignment(String align) to setVerticalAlignment(int align) and call call obj.setVerticalAlignment(SWT.TOP). Commented Aug 22, 2012 at 8:32
  • You can use reflection. Commented Aug 22, 2012 at 8:32
  • im parsing from a text field. so the input is string. so I set the type as a String... Commented Aug 22, 2012 at 8:34

4 Answers 4

4

If you use Java 7 you can always use switch on Strings:

switch (align) {
    case "SWT.TOP":
        gd.verticalAlignment = SWT.TOP;
    /* etc */
}

Being honest I would avoid using strings like "STW.TOP". If I really had to store alignment state in the other way than just int I would use enums which might be used in switch in older versions of Java.

Sign up to request clarification or add additional context in comments.

Comments

2

Sounds like you want a map:

// Ideally use ImmutableMap from Guava
private static final Map<String, Integer> ALIGNMENTS = mapAlignments();

private static final Map<String, Integer> mapAlignments() {
    Map<String, Integer> ret = new HashMap<String, Integer>();
    ret.put ("SWT.TOP", SWT.TOP);
    // etc
    return ret;
}

Then you can just fetch from the map (and unbox) later.

Or, better, change your method declaration to avoid this in the first place :)

Comments

0
Integer.parseInt(String)

can throw a NumberFormatException if the string is not specified as integer value. Also in prev versions of java, you cant apply switch-case on Strings. So better you can use the following :

if(("SWT.TOP").equals(align))
{
     gd.verticalAlignment = SWT.TOP;
}

Comments

0

Why are you using a text field? There are only a few legal choices for alignments, so you should really use something like a JComboBox instead. You could store custom objects in the JComboBox so that they display the named constant but also store the integer constant:

public class SwingAlignOption {
  public final String name;
  public final int value;
  public SwingAlignOption(String name, int value) {
    this.name = name;
    this.value = value;
  }
  public String toString() { return name; }
}

Then you can add instances to the combo-box like comboBox.addItem(new SwingAlignOption("TOP", SWT.TOP)).

Note that JComboBox changed between Java 6 and 7. In the Java 7 libraries JComboBox is generic, which makes it easier to store custom objects like this inside and retrieve their values later. In Java 6 you'll have to use a cast when you access the selected value.

1 Comment

OK, that makes more sense. In that case, I'd suggest using the Map or switch option suggested above instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.