Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

help with this bit of applet code

Posted on 2004-08-01
20
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 6
20 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 800 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 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

715 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