Results 1 to 16 of 16
- 09-21-2010, 05:36 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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 09:22 AM. Reason: to optmize reply
- 09-27-2010, 02:44 PM #2
Member
- Join Date
- Sep 2010
- Location
- Columbus, Ohio
- Posts
- 4
- Rep Power
- 0
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); } }
- 09-27-2010, 09:34 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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...
- 09-27-2010, 09:57 PM #4
Have you looked at the AppletContext's showDocument() method?displays the results on separate page outside the Java applet
- 09-27-2010, 10:08 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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---
- 09-27-2010, 10:15 PM #6
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 javascriptLast edited by Norm; 09-27-2010 at 10:17 PM.
- 09-28-2010, 03:09 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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 ---
- 09-28-2010, 03:36 AM #8
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);
- 09-29-2010, 11:52 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
Sorry for the delayed post. I'm attempting both suggestions offfered by Norm and ceautery and will advise of the outcome shortly.
Best--
- 10-06-2010, 07:04 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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 07:47 PM. Reason: edit
- 10-06-2010, 10:05 PM #11
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.
- 10-07-2010, 09:19 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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!
- 10-07-2010, 09:30 PM #13
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.
- 10-07-2010, 09:39 PM #14
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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.
- 10-07-2010, 10:02 PM #15
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.
- 10-08-2010, 05:43 PM #16
Member
- Join Date
- Sep 2010
- Posts
- 11
- Rep Power
- 0
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:
To mimic the html hyperlink effect and have the cursor change into hand when in the Jlist area, I used the following: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"); } } };
this process took me a long time to achieve, so I hope it is helpfull to some else.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(); }
Again thank you, Norm.Last edited by mbarandao; 10-08-2010 at 05:59 PM. Reason: typo
Similar Threads
-
JList
By nawl in forum New To JavaReplies: 2Last Post: 05-21-2010, 06:37 AM -
Content type for MS files "content/unknown" with URLConnection
By serjant in forum NetworkingReplies: 2Last Post: 05-30-2009, 10:42 AM -
How to use a JList?
By glhansen in forum New To JavaReplies: 3Last Post: 03-24-2009, 10:27 AM -
JList
By pinks_70986 in forum New To JavaReplies: 1Last Post: 02-12-2009, 08:36 AM -
About JList
By hungleon88 in forum Advanced JavaReplies: 5Last Post: 08-30-2008, 09:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks