GWT custom widgets height - gwt

I need a custom widget height. I tried using this
Integer.toString(yourWidget.getElement().getOffsetHeight())
but,
If I use it when I create it or add it to the container panel, it returns 0
If I use it in the contrainer panel's onLoad method, it returns the widget height before the style is applied
So, when should I use it to get the widget height after the style is applied?
Thanks a lot!

I don't known if you've solved your problem already, but an option to use JSNI would be making sure that the widget creation is finished.
A way of achieving that is with the Deferred command:
DeferredCommand.addCommand(new Command() {
public void execute() {
// Ask here for the height
}
});

Seems to be the same kind of problem that the one exposed here: GWT - Retrieve size of a widget that is not displayed
They used a workaround using JSNI to do what they wanted, maybe a similiar trick will work for you as well.

I think onAttach or onLoad would be a good catch..
#Override
protected void onLoad() {
super.onLoad();
//do sth
}

Related

GWT Nothing shows up after changes

I am learning GWT Widget. I am having a simple EntryPoint class using which I am adding a Widget(ToggleButton). Initially it was working. Then I have just added the changes to change the attributes of my button. Then nothing came up in the specific HTML. I have done a clean-build too.
Below is my onModuleLoad()
#Override
public void onModuleLoad()
{
// Create Instance for your Widget
ToggleButton aToggleButton = new ToggleButton("Normal State", "Clicked State");
// Apply required style as per your wish
aToggleButton.setPixelSize(50, 10);
aToggleButton.setTitle("My first Simple Widget");
// Add it to the panel of your wish
RootPanel.get().add(aToggleButton);
}
Am I missing something. But I am sure that I haven't done anything apart from adding the lines that setting the size and title.
There's nothing wrong with this code. Compile to javascript, clean browser cache and try again.

Is there a way to tell when a Widget is shown with GWT?

I'd like to respond to an even whenever my widget is made visible on a page done with GWT and UI Binder.
Is there anything similar to the onAttach() event handler (which fires when the widget is added to the DOM), pertaining to when the widget is actually made visible?
I'd like to be able to handle the even when the widget is shown because there are a few different ways of making it visible, and I'd like a single place on the widget itself that can handle this event.
Thanks
I know this is an old question, I have faced the same problem before. What I did was override the setVisible(boolean visible) method in the widget, then perform whatever I needed to do:
#Override
public void setVisible(boolean isVisible) {
super.setVisible(isVisible);
if(isVisible) {
// Do whatever you need to do with your widget
}
}
The widget should be visible once added to the DOM unless you've intentionally hidden it (i.e. with CSS or hid it behind another widget). Normally, onAttached() means its on the page. If you're using CSS classes to make it visible, write a setVisible(boolean isVisible) method to your widget and set the visibility class this way. If you have it behind another widget (i.e. in layers) then you'll need to write your only logic to determine when it's visible.
There is no browser event for this, but you could try this:
With your widget you could check the elements getLeftOffset (or similar method), if you get a positive value, you could fire your method, and set a flag to indicate that your onVisible() method had fired.
Then once the getLeftOffset returns a 0 you could reset your flag, ready to fire your event again.

Can I use GWT frame widget inside a PopupPanel?

I'd like to use a Frame widget inside a PopupPanel widget, so that in this popup I'm able to access some information from a different URL.
public class MyPopup extends PopupPanel {
public MyPopup() {
super(true);
Frame frame = new Frame();
frame.setUrl("http://google.com");
frame.setWidth("200px");
frame.setHeight("100px");
setWidget(frame);
}
}
When I debugged the code, I noticed that the page inside the frame was actually loaded, but I didn't see the content shown. The popup was transparent.
Anyone has experience on this? And any help would be appreciated.
Thanks.
I have used another way to implement my feature, maybe frame+popup is not good way. I will investigate further and see what is the problem.

Get the GWT ScrollPanel to start its vertical scrollbar at the bottom

I know there are some questions out there about the GWT ScrollPanel and how it works, but allow me to explain the situation.
I'm working on a project to implement QoS on routers. I'm now at the developping stage of the project and I need to make a webinterface to add protocols such as ssh and http and give them their bandwidth.
To save memory usage and network traffic, I do not use GWT-EXT or Smart GWT. So to set the bandwidths I use a ScrollPanel with an empty SimplePanel in it (which is way too big), leaving only the scrollbar.
Now here's the problem:
I want each scrollbar for each added protocol to start at the bottom, not the top. I can get it working through the code if I manually move the scrollbar first, then any function works, like a scrollToBottom(), or a setScrollPosition(). If I want to move scrollbars through code before moving the scrollbar manually, however, I can't call a function on it.
(I would post a picture but I can't yet - new user)
Summary:
So if I add a protocol (using a button called btnAjouter), the two slidebars (One for guaranteed bandwidth and one for the maximum bandwidth) for each protocol start at the top. I want them to start at the bottom on the load of the widget.
Is there a way to do this?
Thanks in advance!
Glenn
Okay, my colleage found the solution. It's a rather dirty one, though.
The thing is, the functions only work when the element in question is attached to the DOM. I did do a check with a Window.alert() to see if it was attached, and it was. But the prolem was that the functions were called to early, for example on a buttonclick it would've worked. The creation and attachment of the elements all happens very fast, so the Javascript can't keep up, this is the solution:
Timer t1 = new Timer()
{
#Override
public void run()
{
s1.getScroll().scrollToBottom();
s2.getScroll().scrollToBottom();
}
};
t1.schedule(20);
Using a timer isn't the most clean solution around, but it works. s1 and s2 are my custom slidebars, getScroll() gets the ScrollPanel attached to it.
You can extend ScrollPanel and override the onLoad method. This method is called immediately after a widget becomes attached to the browser's document.
#Override
protected void onLoad() {
scrollToBottom();
}
Could you attach a handler to listen to the add event and inside that handler do something like this:
panel.getElement().setScrollTop(panel.getElement().getScrollHeight());
"panel" is the panel that you add your protocol to. It doesn't have to be a ScrollPanel. An HTMLPanel will work.
You can wrap this method in a command and pass it to Schedule.scheduleDeferred if it needs to be called after the browser event loop returns:
Schedule.scheduleDeferred(new Scheduler.ScheduledCommand(
public void execute() {
panel.getElement().setScrollTop(panel.getElement().getScrollHeight());
}
));

Centering PopupPanel doesn't work on first call

I'd like PopupPanel centred in the screen calling the center() method. It is placed incorrectly the first time I load it. All subsequent times, it centers just fine.
It seems like the styles contained in < ui:style> aren't being injected. I've tried creating an interface to the style in the View (per GWT docs) and calling ensureInjected() in the constructor but this has no effect.
How to have consistent centering?
There is a defect which is said to be fixed in GWT 2.5. For me the issue still exists in 2.5, and for my situation the fix was to set width and height of the panel explicitly:
popup.setWidth("800px");
popup.setHeight("700px");
For me, this works with the following simple snippet (tested on Firefox 3.6 and Chrome 10.0):
#Override
public void onModuleLoad() {
final PopupPanel popupPanel = new PopupPanel();
popupPanel.center();
}
Are you doing something differently?