0

I'm trying to find the color of an element with GWT, but am getting nothing (empty string) returned. What's my problem? The following code illustrates the problem:

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootLayoutPanel;

public class Test implements EntryPoint {
    Anchor a = new Anchor("Anchor");

    @Override
    public void onModuleLoad() {
            RootLayoutPanel.get().add(a);

            Scheduler.get().scheduleDeferred(new ScheduledCommand() {    
                @Override
                public void execute() {
                    log(a.getElement().getStyle().getColor());                    
                    log(a.getElement().getStyle().getVisibility());
                    log(a.getElement().getStyle().getPosition());                    
                }
            });
    }

    static native void log(String message) /*-{
    console.log(message);
  }-*/;
}

The console displays color and visibility as empty strings, whilst Position is shown as "absolute" (showing that the problem isn't with the logging). Result is the same in both Chrome and Firefox.

Oswulf
  • 41
  • 3
  • Try to `getComputedStyle`. Maybe this post will help you: http://stackoverflow.com/questions/21797258/getcomputedstyle-like-javascript-function-for-ie8 – Adam Oct 26 '16 at 22:36

1 Answers1

1

This means that the element has no color set directly in its style attribute. It can get color from its CSS class or browser defaults.

Andrei Volgin
  • 40,755
  • 6
  • 49
  • 58
  • So this approach doesn't look at the DOM and retrieve how the element has been rendered? I'm surprised. How can I get the rendering information? (My situation is that I have a canvas within an HTML page. The page styles are set dynamically via alternate CSS sheets. I need the canvas to match the CSS style.) – Oswulf Oct 26 '16 at 06:05