Solved

help with this bit of applet code

Posted on 2004-08-01
20
249 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
Comment Utility
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
Comment Utility
But it would be better to do


urlLabel.setText(link);

after setting a 'dummy' test first

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>after setting a 'dummy' test first

sorry - that should have said

after setting a 'dummy' text first
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>if you want to use the same label then use:

(already mentioned)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
How many links are you going to have?
0
 

Author Comment

by:HomerrSimpson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
changeXy challenge 13 56
micro services vs rest web services 16 45
Checkbox and ListView in Android Layout 4 36
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now