Reputation: 49
I'm messing around creating a program that pulls movie information from IMDB's top 250 rated movies (Movie, Year, Rating) using JSoup
. I've managed to figure out how to print this list within a JScrollPane
. I've also created a separate ArrayList
that contains the links to the page of each movie. What I want to do is make each item in the JList selectable so that if the user left-clicks on a movie it will take you to the URL. How can I go about this?
import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.awt.Dimension;
import java.io.*;
import java.util.*;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
public class MovieList {
public ArrayList<ArrayList<String>> movieInfo;
public static void main(String[] args) throws IOException{
ArrayList<ArrayList<String>> movieInfo = new ArrayList<>();
ArrayList<String> linkInfo = new ArrayList<>();
String imdb = "http://www.imdb.com/chart/top?ref_=nv_mv_250_6";
Document doc = Jsoup.connect(imdb).get();
Elements title = doc.select("td.titleColumn");
Elements year = doc.select("span.secondaryInfo");
Elements rating = doc.select("td.ratingColumn.imdbRating");
Elements link = doc.select("td.titleColumn a");
for(Element linkList: link){
linkInfo.add(linkList.attr("abs:href"));
}
int i=0;
for(Element movieList: title){
ArrayList<String> movie = new ArrayList<>();
i++;
movie.add(i+": "+movieList.getElementsByTag("a").text());
movieInfo.add(movie);
}
int x=0;
for(Element yearList: year){
movieInfo.get(x).add(1,yearList.getElementsByTag("span").text());
x++;
}
int y=0;
for(Element ratingList: rating){
movieInfo.get(y).add(2,"Rating: "+ratingList.getElementsByTag("strong").text());
y++;
}
DefaultListModel<String> listModel = new DefaultListModel<>();
for(ArrayList<String> j: movieInfo){
listModel.addElement(j.toString()+"\n");
}
JList<String> jList = new JList<>(listModel);
JScrollPane scrollPane = new JScrollPane(jList);
scrollPane.setPreferredSize(new Dimension(575,1080));
JOptionPane.showMessageDialog(null, scrollPane);
}
}
Upvotes: 0
Views: 613
Reputation: 347314
What I want to do is make each item in the JList selectable so that if the user left-clicks on a movie it will take you to the URL
Start with How to use list selection listener to Selecting items in a list to see how you can detect selection changes
it will take you to the URL
See How to Integrate with the Desktop Class to see how you can open the URL in the user's browser
Upvotes: 1