• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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());}
}

0
HomerrSimpson
Asked:
HomerrSimpson
  • 7
  • 7
  • 6
3 Solutions
 
CEHJCommented:
Try


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

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

0
 
CEHJCommented:
But it would be better to do


urlLabel.setText(link);

after setting a 'dummy' test first

0
 
objectsCommented:
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());}
}
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
>>after setting a 'dummy' test first

sorry - that should have said

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

(already mentioned)
0
 
objectsCommented:
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());}
}
0
 
HomerrSimpsonAuthor Commented:
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



0
 
objectsCommented:
Another approach would be to use a List to display your links. You then don't need to worry about MouseListener's at all and just add a single ActionListener to your list.


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

        urls.addActionListener(this);
        }

public void actionPerformed(ActionEvent event)
{
   String url = urls.getSelectedItem();
   System.out.println(url);
}

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

}catch (Exception e){System.out.println(e.getMessage());}
}
0
 
objectsCommented:
> 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.


0
 
HomerrSimpsonAuthor Commented:
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());}
}
0
 
CEHJCommented:
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);
            }
      }

}


0
 
HomerrSimpsonAuthor Commented:
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
0
 
CEHJCommented:
How many links are you going to have?
0
 
HomerrSimpsonAuthor Commented:
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
0
 
CEHJCommented:
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
0
 
HomerrSimpsonAuthor Commented:
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.
0
 
objectsCommented:
> 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?
0
 
HomerrSimpsonAuthor Commented:
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
0
 
objectsCommented:
you need to use a seperate label for each url.

if you want to clear the previous results when you do a new search then you also need to remove all the old labels.
To make it more manageable I'd suggest using a seperate panel to store yourr labels (and just your labels) allow you to use
panel.removeAll() to remove all the existing labels.
0
 
objectsCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 7
  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now