Solved

help with this bit of applet code

Posted on 2004-08-01
20
251 Views
Last Modified: 2012-05-05
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
Comment
Question by:HomerrSimpson
  • 7
  • 7
  • 6
20 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 11689779
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11689795
But it would be better to do


urlLabel.setText(link);

after setting a 'dummy' test first

0
 
LVL 92

Expert Comment

by:objects
ID: 11689803
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11689807
>>after setting a 'dummy' test first

sorry - that should have said

after setting a 'dummy' text first
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11689810
>>if you want to use the same label then use:

(already mentioned)
0
 
LVL 92

Expert Comment

by:objects
ID: 11689811
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
 

Author Comment

by:HomerrSimpson
ID: 11689839
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
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 11689844
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
 
LVL 92

Expert Comment

by:objects
ID: 11689845
> 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
 

Author Comment

by:HomerrSimpson
ID: 11694248
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11694494
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
 

Author Comment

by:HomerrSimpson
ID: 11697163
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11697267
How many links are you going to have?
0
 

Author Comment

by:HomerrSimpson
ID: 11697542
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 11697606
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
 

Author Comment

by:HomerrSimpson
ID: 11698020
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
 
LVL 92

Expert Comment

by:objects
ID: 11699254
> 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
 

Author Comment

by:HomerrSimpson
ID: 11699348
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
 
LVL 92

Assisted Solution

by:objects
objects earned 200 total points
ID: 11699386
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
 
LVL 92

Expert Comment

by:objects
ID: 11710802
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
difference of if loops 23 49
Way to decrease size of apk file 9 65
arguments to jar 5 26
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question