Link to home
Start Free TrialLog in
Avatar of HomerrSimpson
HomerrSimpson

asked on

help with this bit of applet code

this is kind of a add on question to a solution that i found here the other day but i cant find the link now

want to display to links on a applet and i m using a label so that you can a mouselistner to it but the links doesnt get displayed
can anyone help ? and also i think if the links did get displayed the links will overlapp each how would you solve that as well?

public void init() {
        Panel panel = new Panel();
        panel = new Panel(new GridLayout(4,1));
        panel.add( searchTextField);
        panel.add( searchButton );
        panel.add(urlLabel);
        add(panel);                  
        }

public void displaylinks()
{
try{
   while (rs.next())
   {
System.out.println(link);
    link = rs.getString("links");
    urlLabel = new Label(link);
   }

}catch (Exception e){System.out.println(e.getMessage());}
}

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But it would be better to do


urlLabel.setText(link);

after setting a 'dummy' test first

if you want to use the same label then use:

public void displaylinks()
{
try{
   while (rs.next())
   {
System.out.println(link);
    link = rs.getString("links");
    urlLabel.setText(link);
   }

}catch (Exception e){System.out.println(e.getMessage());}
}
>>after setting a 'dummy' test first

sorry - that should have said

after setting a 'dummy' text first
>>if you want to use the same label then use:

(already mentioned)
to add each link to a new label use something like:


Panel p = new Panel(new GridLayout(4, 1));
public void init() {
        panel.add( searchTextField);
        panel.add( searchButton );
        panel.add(urlLabel);
        add(panel);              
        }

public void displaylinks()
{
try{
   while (rs.next())
   {
System.out.println(link);
    link = rs.getString("links");
    Label l = new Label(link);
    panel.add(l);
    validate();
    repaint();
   }

}catch (Exception e){System.out.println(e.getMessage());}
}
Avatar of HomerrSimpson
HomerrSimpson

ASKER

setText works but i have a lot of links to display but it only display one of them, will i have to set more labels or is there a better solution to stop the over ridding  ?

can you specify coordinates for labels like you could for g.drawString



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> setText works but i have a lot of links to display but it only display one of them,
> will i have to set more labels or is there a better solution to stop the over ridding  ?

See the other two alternatives I suggested.


below is my bit code the repaint doesnt seem to work as when i try a new search the old results remains there which shouldnt.
I want to display the results one below each other like most search engines like google i m currently doin this by having a Gridlayout of 100,1 is there a more effective soution?

Finally i m currently running this command using "appletviewer  applet.html" which works fine but when i actually open the html page the i see the search field and a button but the background is all gray and the results doesnt get displayed can someone help

Panel p = new Panel(new GridLayout(2, 1));
Panel p2 = new Panel(new GridLayout(100, 1));
public void init() {
        panel.add( searchTextField);
        panel.add( searchButton );

        add(p);  
        add(p2);            
        }

public boolean action(Event e, Object arg) {
if (e.target instanceof Button)
      displayResults();
      repaint();
      return true;
            }

public void displaylinks()
{
try{
   while (rs.next())
   {
System.out.println(link);
    link = rs.getString("links");
    Label l = new Label(link);
    p2.add(l);
    validate();
    repaint();
   }

}catch (Exception e){System.out.println(e.getMessage());}
}
Rather than adding labels or dispaying a List, both of which could distort your gui if you have many of them, use a Choice like this. You can compile this and then run it with appletviewer thus

appletviewer AppletLabels.java


SNIP==============================================


/*
 *  <applet codebase="." code="AppletLabels.class" width="300" height="300">
 *  </applet>
 */
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class AppletLabels extends Applet implements ItemListener {
      Panel panel;
      TextField searchTextField;
      Button searchButton;
      Label urlLabel;
      Choice urls;


      public void init() {
            panel = new Panel(new GridLayout(4, 1));
            searchTextField = new TextField("Search");
            panel.add(searchTextField);
            searchButton = new Button("Search");
            urlLabel = new Label("Link");
            urls = new Choice();
            urls.addItemListener(this);
            panel.add(searchButton);
            panel.add(urls);
            add(panel);
            displayLinks();

      }

      private void displayLinks() {
            String[] links = {"AAAAAA", "BBBBBBBBBB", "CCCCCCCC"};
            for (int i = 0; i < links.length; i++) {
                  urls.add(links[i]);
            }
      }
      
      public void navigate(String url) {
            // Navigate to it really!
            System.out.println(url);
      }
      
      public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                  String url = (String)urls.getSelectedItem();
                  navigate(url);
            }
      }

}


i tried out your code CEHJ i found it kind of weird using the choice

rather than doing this

Label l = new Label(link);

how can i replace the old label result with a new result using the same Label l
How many links are you going to have?
could be quite a lot from 20 to hundred

i would like to display the results like what most search engines would do, like google for example if its possible
Using Labels in this way is not really advisable as it forces the gui to lay itself out again and reduces your control over it. You'd be better off using the Combo
Combo could be option but i may want to add more details into the result such as a snippet of what the link contains or an heading and that might lool weird on a Combo.
> You'd be better off using the Combo

A combo is basically the same as a list, it offers no advantages in this situation.
In fact may be less desirable.

Did u try my suggestions yet?
i have tried your suggestions objects

it works but when i enter a new search the old results remains there along with the new search results thats why i asked

rather than doing this

Label l = new Label(link);

how can i replace the old label result with a new result using the same Label l
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial