user413715
user413715

Reputation: 19

How to create an URL in Blackberry program

Can anyone tell me how to create an URL link in Blackberry, which opens a web page after it's been clicked?

Upvotes: 1

Views: 452

Answers (1)

Marc Paradise
Marc Paradise

Reputation: 1939

// This can eb a nested class in your screen class. 
class URLButtonField extends ButtonField {  
    String url; 
    public URLButtonField(String label, String url) { 
        super(label); 
        this.url = url; 
    } 
    public String getURL() { 
        return url; 
    } 
    public void setURL(String url) { 
        this.url = url; 
    } 
}    



// member variable of your screen class- this will let you access it later 
// to change the URL
URLButtonField bf;

// In your screen's constructor: 

bf = new ButtonField("Example", "http://www.example.com"); 
bf.setFieldChangeListener( new FieldChangeListener() { 
    void fieldChanged(Field field, int context) { 
        if (field == this) { 
            BrowserSession session =- Browser.getDefaultSession();
            session.displayPage(getURL()); 
        }
    } 
} );         
add(bf);

You can then change the text or destination URL of "bf" at any time, and whatever you change it to will be the URL that is launched when it is clicked:

// In response to some activity: 
bf.setText("Example Two"); 
bf.setURL("http://www.example.com/two"); 

Upvotes: 1

Related Questions