Results 1 to 16 of 16
  1. #1
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default How to Hyperlink the content of a Jlist

    I need some guidance in implementing a hyperlink function inside a Jlist of an applet.

    Specifically, I have a Jlist that displays the values retrieved from a c++ hash table --a paralell application. I want to be able to create a hyperlink function within the Jlist content which once clicked, wraps the clicked value into a php script and thus performs a mysql query (ie: the content of Jlist is : "Angelique Kojo" Once clicked on the value, I want to perform a seach inside mysql with key words "Angelique Kojo".)

    Any assistance in this area is greatly appreciated.
    Last edited by mbarandao; 09-25-2010 at 10:22 AM. Reason: to optmize reply

  2. #2
    ceautery is offline Member
    Join Date
    Sep 2010
    Location
    Columbus, Ohio
    Posts
    4
    Rep Power
    0

    Default

    What do you have so far, and what part are you having trouble with?

    You can emulate a hyperlink with a mouse listener, and grab the selected object in the JList and do whatever with it. Here's some dummy code as an example:

    Java Code:
    public class Test extends Applet {
      
      JList list;
      JLabel label;
      
      public void init() {
        setLayout(null);
        String[] items = {"Angelique", "Joe", "Fred"};
        list = new JList(items);
        label = new JLabel("Last item clicked");
        
        MouseListener ml = new MouseAdapter() {
          public void mouseClicked(MouseEvent e) {
            label.setText((String)list.getSelectedValue());
          }
        };
    
        list.setBounds(10, 10, 100, 100);
        list.addMouseListener(ml);
        add(list);
        
        label.setBounds(150, 10, 200, 20);
        add(label);
      
      }
    }

  3. #3
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    ceautery, thank you for your reply and for your time. A portion of entire project currently performs the following task --a task which I wish to change to hyperlink):

    The program retrieves and displays a value inside my list (ie: the value is "Angelique Kojo"); I have a mouseclick event (once cliked on the value) the program reads a Txt file, looking for the content of the value clicked ; it
    activates an audio player class and plays an audio file called
    "Angelique Kojo.mp3" (the portion of my program that performs the above described task is:

    Code
    ----------
    private void jListSongsMouseClicked(java.awt.event.MouseEvent evt) {

    songlist =readSongList("wavlist.txt");//this codes enables original sound clip playback. It reads from the file "wavlist.txt" the names of all match sounds.
    int index = + jListSongs.getSelectedIndex();
    if (evt.getClickCount() != 1 || index < 0 || songs.isEmpty())//getClickCount number determines after how many clicks an action is initiated --one click initiates after the click, two after two clicks and so on --Mossa 9/13
    return;


    SongData songdata = (SongData) songs.get(index);
    String songfile = (String) songlist.get(songdata.id);
    float start = songdata.db_frame_start / framespersecond;
    float duration = (songdata.db_frame_end - songdata.db_frame_start)/ framespersecond + endlength;

    if (songfile == null) {
    System.out.println("Cannot find original file in the database " + songdata.id);
    return;
    }

    File f = new File(songfile);

    if (!f.canRead()) {
    System.out.println("Can't read original sound clip file: " + songfile);
    return;
    }

    deactivate();

    //display wfft info
    AudioFormat [] audioformat = new AudioFormat[1];
    byte [] buffer;

    if (songfile.toLowerCase().endsWith(".mp3"))
    buffer = loadMp3(f, start, duration, audioformat);
    else
    buffer = loadWav(f, start, duration, audioformat);

    if (audioformat[0].getChannels() == 2)
    buffer = stereoToMono(buffer);

    displayWindowedFFT(buffer, (int) audioformat[0].getSampleRate(), true, true);//displays graph of original sound

    //fftDisplayPanel.displayFFT(dbuffer, freq[0], false);

    // play song
    mp3player = new MP3Player(this);//this line and the next line needs to be added in order to play original sound from retreival
    mp3player.start();

    jLabelStatus.setText("Playing " + songdata.name + "...");
    System.out.println("Start time: " + start + " Duration: " + duration);

    if (songfile.toLowerCase().endsWith(".mp3"))
    mp3player.play(songfile, start, duration);
    else
    wavplayer.play(songfile, start, duration);


    }

    End of Code

    Now, I want to replace this process with one that takes the value
    "Angelique Kojo" and performs a mysql search via php and displays the
    results on separate page outside the Java applet(ie:
    /searchresults.php).

    Does any of this make sense to you?
    Please advise...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    displays the results on separate page outside the Java applet
    Have you looked at the AppletContext's showDocument() method?

  5. #5
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    I appreciate the response, Norm. I have briefly read some internet writing in that area; however, I have not been able to achieve much --as I'm still trying to turn the content of the Jlist into a hyperlink. I would appreciate a more detailed guidance if you can.

    Best---

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Do you have a listener that gets control when an item in the list is selected?
    That listener would create an URL and call the showDocument method.

    hyperlink sounds like an HTML thing done with the anchor tag <A or with some javascript
    Last edited by Norm; 09-27-2010 at 11:17 PM.

  7. #7
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Norm, thanks again for the reply; I do have a mouselistener " jListSongs.addMouseListener(new java.awt.event.MouseAdapter(){". My current challenge is to minic the html hyperlink effect. In my earlier post, I detailed the current function of the code with the above listener.

    You write: "...listener would create an URL and call the showDocument method"; could you provide an example code with this suggested strategy in the context of my challenge?

    Best ---

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    The listener I was thinking of was: ListSelectionListener
    When a list item was selected the listener is called.

    The code used in the listener would be something like this:
    URL theURL = new URL (put the address here);
    AppletContext appletCntxt = getAppletContext();
    appletCntxt.showDocument(theURL, parameter for new window; see doc);

  9. #9
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Sorry for the delayed post. I'm attempting both suggestions offfered by Norm and ceautery and will advise of the outcome shortly.

    Best--

  10. #10
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Hello all:

    I'm still experiencing difficulty creating a hyperlink of the content of my Jlist.

    Following some guidance provided from this forum, I add the following code to my program with no success. My need is to make the content of the Jlist Hyperlinkable so that users can click on the value of the Jlist and be transported to a php script page outside of the java on a regular html page which will process a php/mysql search. Itrust that someone here is fimilar with this process and can help...

    The code I used is the following:

    ---Code begins----
    jListSongs.addMouseListener(new java.awt.event.MouseAdapter() {

    ListSelectionListener listSelectionListener = new ListSelectionListener() {

    public void valueChanged(ListSelectionEvent e) {
    {
    URL theURL = null;
    try {
    theURL = new URL("http://examplesite.com/search.php");
    } catch (MalformedURLException ex) {
    Logger.getLogger(SoundRetrievalApp.class.getName() ).log(Level.SEVERE, null, ex);
    }

    AppletContext appletCntxt = getAppletContext();
    appletCntxt.showDocument(theURL, "_blank");
    }
    }

    };

    ---Code ends----
    Last edited by mbarandao; 10-06-2010 at 08:47 PM. Reason: edit

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Break the development process up into steps:
    1) Write a small simple program that calls the showDocument() method and causes a new page to load in the browser.
    2) Merge that code into the listener when you get it working.

    Write the small program to use showDocument and an HTML page to call the applet. If you have problems getting it to work, post them here with all error messages and an explanation of what the problem is.

  12. #12
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Good day Norm:

    Thanks for your continued replies to my request for help. I was able to create a hyperlink on the Jlist content. However, this was success after attempting the process with the following code --many attempts using the showDocument() and listSelectionListener() were without success:

    --successfull code begins--
    public void browse() {
    setForeground(activeColor);
    //setBorder(activeBorder);
    try {
    Desktop.getDesktop().browse(new URI("http://examplesite.com/searchload/music/searchloadresults.php"));
    } catch(Exception e) {
    e.printStackTrace();
    }
    --code ends--

    The shortcoming of this process is that i cannot specify the target (ie: "_blank" or "_self").

    Are you fimilar with this option -target option that is- using the browse() method?

    Best!

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please use code tags when posting code. Info here: Java Forums - BB Code List

    I didn't know that the Desktop class would work in an applet. I have never tried it. I've always used the showDocument method.

  14. #14
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the reply. Any other suggestions as to how to make showDocument() work. currently, I get no errors, the application simply is unresponsive on mouseclick.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Did you try this approach?
    Break the development process up into steps:
    1) Write a small simple program that calls the showDocument() method and causes a new page to load in the browser.
    2) Merge that code into the listener when you get it working.

    Write the small program to use showDocument and an HTML page to call the applet. If you have problems getting it to work, post them here with all error messages and an explanation of what the problem is.

    Does the showDocument method work for you in a standalone simple applet?

    Does the mouse click work for a simple standalone program? If not copy and paste the code for the small program here to get help making the listener work.

    If both small programs work, then you should be able to merge them and get them to work together.

  16. #16
    mbarandao is offline Member
    Join Date
    Sep 2010
    Posts
    11
    Rep Power
    0

    Default

    Good day Norm and everyone else on the forum:

    After so many different attempts and following suggestions offered here, I finally got my Hyperlink of Jlist content to work. Great thanks to Norm and his guidance.

    For anyone who may be going through a similar process, why reinvent the wheel, here is what is working for me.

    I'm working on a complex project therefore the process for making the hyperlink to work is strategically placed in different areas of my application code.

    Under my private void initComponents(), I have the following code:

    Java Code:
     public void valueChanged(ListSelectionEvent lse) {
    if( !lse.getValueIsAdjusting()) {
    
             String selection = (String) jListSongs.getSelectedValue();
            System.out.println("you have selected:" + selection);
    
            {
                URL theURL = null;
    
                try {
    theURL = new URL("http://examplesite.com/searchloadresults.php");
    } catch (MalformedURLException ex) {
    Logger.getLogger(MyApplication.class.getName() ).log(Level.SEVERE, null, ex);
    }
                     AppletContext appletCntxt = getAppletContext();
                     appletCntxt.showDocument(theURL, "_top");
                          }
    }
    
    
    };
    To mimic the html hyperlink effect and have the cursor change into hand when in the Jlist area, I used the following:
    Java Code:
    //This code is to enable the cursor to change to hand when hovering over the JList ---Mossa 10/6/10
    
        /** Set the color to the hover color. */
    	public void mouseEntered(MouseEvent me) {
            jListSongs.setCursor(new Cursor(Cursor.HAND_CURSOR));//adding this line changes the cursor into a hand in the Jlist area
                }
    
        public void mouseExited(MouseEvent me) {
            jListSongs.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//adding this line changes the cursor into a hand in the Jlist area
                }
        public void actionPerformed(ActionEvent ae) {
    		}
    
        public void mouseClicked(MouseEvent e) {
              }
    
    
        //};
    
        public void mouseReleased(MouseEvent me) {}
    
    	public void mousePressed(MouseEvent me) {}
    
    	/** Set the color to the standard color. */
    	public void focusLost(FocusEvent fe) {
    		//setForeground(standardColor);
    		//setBorder(standardBorder);
    	}
    
    	/** Set the color to the hover color. */
    	public void focusGained(FocusEvent fe) {
    		//setForeground(hoverColor);
    		//setBorder(hoverBorder);
    	}
    
         public void setEditable(boolean b) {
    	if (b != editable) {
    	    boolean oldVal = editable;
    	    editable = b;
    	    enableInputMethods(editable);
    	    firePropertyChange("editable", Boolean.valueOf(oldVal), Boolean.valueOf(editable));
    	    repaint();
    
        }
    this process took me a long time to achieve, so I hope it is helpfull to some else.

    Again thank you, Norm.
    Last edited by mbarandao; 10-08-2010 at 06:59 PM. Reason: typo

Similar Threads

  1. JList
    By nawl in forum New To Java
    Replies: 2
    Last Post: 05-21-2010, 07:37 AM
  2. Replies: 2
    Last Post: 05-30-2009, 11:42 AM
  3. How to use a JList?
    By glhansen in forum New To Java
    Replies: 3
    Last Post: 03-24-2009, 11:27 AM
  4. JList
    By pinks_70986 in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 09:36 AM
  5. About JList
    By hungleon88 in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2008, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •