tags:

views:

106

answers:

3

Hi, So my question is the following :

Say, I am displaying data in in a flextable after getting it from a database, am doing this in a routine manner using loops.

Now, in the entire First Column, the text should be clickable, i.e. I should be able to add a Click Handler to First Column values (as I gotta load another table upon clicking any of the first column text vals.)

Question : How can I generate such Hyperlink tags while in a loop ? The problem comes when I try to do so as I don't know how to name them as the loop runs and use the same concept while adding ClickHandlers.

Should I use something else other than Hyperlink for this task. Kindly explain how ?

I really appreciate help in this as I am a very new GWT coder.

-- Chirayu

+2  A: 

Using a Hyperlink is maybe not the best way to do something like this, because you shouldn't use a ClickHandler with a Hyperlink. A Hyperlink sets a HistoryToken and you should respond to the change of the History.
I would use a Label and maybe style it as a normal link if you want to have the same look and feel. (I will use a Label in the following example but you could change it to a Hyperlink if you want to.)

I would create a class that extends Label. This class would have an ID you set in the constructor. You would send this Id to the server to get the new FlexTable. In the constructor you add a ClickHandler that reads the id field and sends it to the server to get the new FlexTable.

public class FlexTableLabel extends Label implements ClickHandler {
    int id=0;  
    public FlexTableLabel(String text, int id) {
        this.id=id;
        this.setText=text;
        this.addCickHandler(this);
    }

    public void onClick(ClickEvent event) {
        //sends the id to the server, of course you need to replace it with your 
        //service
        yourService.getNewFlexTable(this.id);
    }     
 }

In your loop you would just create objects of the new class and give it the appropriate parameters(I assume you have an ArrayList with the result. The Objects in this ArrayList would have text and id):

for(int i=0; i<result.size;i++) {
    FlexTable.setWidget(i,0, new FlexTableLabel(result.get(i).text, result.get(i).id);
}

I hope this gives you at least something to start, if something is still unclear leave a comment and I will try to help make things clear.

Edit based on Chirayu's post:

It's hard to explain something like this without knowing your application. I normally implement the Singleton Pattern to get a specific widget. So I would create a class like this:

public static YourPanel extends Panel {
    private static YourPanel instance=null;

    public static YourPanel getInstance() {
        if(instance==null) {
            instance=new YourPanel();
        }
        return instance;
    }
}

In your EntryPointClass you would have something like this:

public class YourEntryClass extends EntryPoint {
    public void onModuleLoad() {
        RootPanel.get().add(YourPanel.getInstance());
    }
}

You can now call the YourPanel.getInstance() method in the onSuccess() part of your service to change the content of the panel:

yourService.getNewFlexTable(this.id, new AsyncCallback<ArrayList<String>>() {
    public void onSuccess(ArrayList<String> result) {
    For(int i=0;i<result.size;i++) {
            YourPanel.getInstance().add(result.get(i);
        } 
    }
});

I hope that helps. Leave a comment if it doesn't.

Sample app
Sample app running
Sample app source code

Chris Boesing
@Chris, thanks a lot..I was looking for something like what you explained and I am yet to try it...I don't have the reputation to increase your reputation, but I am very thankful. :DAlso, I will post here more detail if I get stuck. Kindly check tomorrow.
Chirayu
@Chris Boesing, it worked...thanks a ton ! :D (Hope to see you asnwering more of my bugging questions)
Chirayu
Glad it worked. I check the gwt tag at least once a day, so fire away I will do my best to answer what I can
Chris Boesing
Global state is baaaad ;) http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/
Igor Klimer
@Chris, I think this is the solution...little complicated, but thats because I'm new in this...nevertheless, this is a kind of solution for sure...I'm really thankful..will post here agin as comment on final success.
Chirayu
@ Chris, at the - public class YourEntryClass extends EntryPoint implments ValueChangeHandler {....} part, it shows an error with red on the name, saying that an interface expected after this token, if I make it into an interface...then it says, EntryPoint cannot be a super class, as it has to be a class...what to do ?!
Chirayu
@Chris, i tried every combination as you said above, but the application does not even load in the browser...the error log is like : java.lang.AssertionError: This UIObject's element is not set; you may be missing a call to either Composite.initWidget() or UIObject.setElement() at com.google.gwt.user.client.ui.UIObject.getElement(UIObject.java:507) at com.google.gwt.user.client.ui.ComplexPanel.add(ComplexPanel.java:90) at com.google.gwt.user.client.ui.AbsolutePanel.add(AbsolutePanel.java:7
Chirayu
That's what I meant when I said it's hard to explain something like this without knowing your app. I will bust out a small sample app when I get back home, hopefully that will make things clearer.
Chris Boesing
@Chris, thanks for all the help, thanks for giving this your time. Well, I face multiple problems, but for now a sample app might be a great help. See if you get time to do something like that..put it in a new answer....thanks once again.
Chirayu
@Chirayu edited the answer above. Didn't put it in a new answer so other users have it all in one place
Chris Boesing
@Chris, I cannot be more thankful...I'm very very thankful...I think my problem should be resolved now...Because of my limited knowledge, I have been facing bad trouble...this should be it..I'll comment again if there is anything.
Chirayu
@Chris, if I have to manage 'History' as well, then I can just do it normally by assigning new history token with each click and then using it to show/hide widgets or should I do something else ?, and all of this 'History Management' thing should go into the Entrypoint class only, right? (Take me as a dumb student who asks too many questions.)
Chirayu
Yes, just change the history by calling History.newItem() and respond to it in your onValueChange() function. It would be in the Entrypoint class, although I highly recommend you read http://code.google.com/webtoolkit/articles/mvp-architecture.html if you build a large app.
Chris Boesing
@Chris, no more questions...thank you so so much for taking me this far...I jumped into GWT thinking it will be easy, but realise nothing too good is too easy and I need to work hard on it...Thank you so much, I'll always be indebted by your help in this.
Chirayu
+2  A: 

I'd suggest using Anchor, more specifically, via the Anchor(java.lang.String text) constructor:

Creates an anchor for scripting. The anchor's href is set to javascript:;, based on the expectation that listeners will be added to the anchor.

So, you'll get a good ol' <a> that on click doesn't do anything, but you can add a ClickHandler to it, as such:

Anchor anchor = new Anchor("Click me!"); // At this point clicking it won't do a thing
anchor.addClickHandler(new ClickHandler() {
    @Override
    public void onClick (ClickEvent event){
        // Do something cool here
    }
});

// Insert the anchor into the table

One more tip: it'd be best if you could create the ClickHandler once and then add it to every Anchor, instead of creating a new anonymous class for every Anchor you add to the table (like in the example above) - depending on the number of rows, it could really speed up things.

Igor Klimer
@Igor, I knew You would reply..you are the nicer of the senior members around(Hilbrand is also there) ! :DI'll take your advice and proceed, will post here if I need help. Kindly check again tomorrow. Thanks a lot.
Chirayu
No problem, glad I could help :)
Igor Klimer
@Igor Kilmer, It worked mate...thanks a lot ! :D
Chirayu
A: 

NOT AN ANSWER...more of a call to Chris and Igor from above as could not fit this much thing in a comment.

Hey @Chris, one question, if you come to read this again (hopefully) ... I am confused about one thing, so I did as you explained above, but now if my panel is defined in my EntryPoint class, which has the 'for loop' given by you above...

then in other class FlexTablelabel, in the part where I add the services, i.e. onClick part...how am I supposed to add stuff into my panel's which are defined in the EntryPoint class ?

In,

public void onClick(ClickEvent event) {

        //sends the id to the server, of course you need to replace it with your 
        //service
        yourService.getNewFlexTable(this.id);
    }     
}

How to access panels, methods etc. from the EntryPoint class. I know I can do EntrypointClass EC = new EntrypointClass(), but it does not function as its supposed to.

Chirayu
Dependency Injection (DI) is what I'd suggest. Google Gin http://code.google.com/p/google-gin/) is a very good DI framework for GWT and uses Guice (http://code.google.com/p/google-guice) as its base. While you're at it, you might want to look into MVP (Model-View-Presenter) - search for it on SO. Believe me, it makes GWT apps much more maintainable and easier to test.
Igor Klimer
Chirayu
Since you are in the `FlexTableLabel` class, you can reference all the fields from it in your `onClick` method - including its text, etc. So I don't see a problem (unless I'm missing something). Oh, and this whole question is becoming (has become, to be more accurate) a huge mess - if you need further assistance on a new topic (like DI), please create a new (valid ;)) question. That way it will also be helpful to other users.
Igor Klimer
@Igor, well I didn't intend for it to be a mess, but I guess my lack of clarity in putting up the question has led to its present state...anyways, sorry for that...I am stuck, will try to figure out something(though I doubt that I will be able to). Thanks for all the help.
Chirayu
I don't want to discourage you from posting more questions here (far from it!), but SO is built to answer one specific question/problem, not as a forum for general help (there's a GWT Google Group for that). But my impression is that you need to get back to basics, don't skip ahead, browse the official docs (they're very good and cover almost everything there is to GWT), play with the samples included with GWT and their source codes (very useful stuff there). *Then* if you have questions, the ones left should be "SO-friendly" questions ;)
Igor Klimer
@Igor, thanks for the advice, will use it and will keep it in mind...I'm 23, a stupid novice, and doing internship.
Chirayu