Reputation: 365
Code is pretty simple. I get a URL string from an arraylist. I am then trying to draw an image in my JPanel. If I don't initialize a layout in the code, it returns an error. If I do initialize a layout, there's no error, but the icon doesn't display either. Am I missing something? Thank you!
public class CharacterPage extends JFrame {
private JPanel imagesPanel;
private WikiDB wikiDB;
public CharacterPage(WikiDB db) throws IOException {
super("Character Page");
setContentPane(rootPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(new Dimension(500,500));
imagesPanel.setLayout(new BorderLayout());
this.wikiDB = db;
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<String> characterImages =
wikiDB.searchImages(characterID);
//array contains this string: http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg
for (int x = 0; x < characterImages.size(); x++){
String imageURL = characterImages.get(x);
Graphics g = getGraphics();
try {
URL url = new URL(imageURL);
//icon.paintIcon(imagesPanel,g,300,100);
JLabel wIcon = new JLabel(new ImageIcon(url));
imagesPanel.setVisible(true);
imagesPanel.add(wIcon);
} catch (MalformedURLException mue){
mue.printStackTrace();
}
}
});
}
Below is a separate program I used to read my URL. Using the code below, I can read my URL without returning any errors, but it still doesn't display in the GUI.
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(new Dimension(500, 500));
imagesPanel.setLayout(new GridLayout());
BufferedImage img1 = null;
try
{
URL url1 = new URL("http://cosplayidol.otakuhouse.com/wp-content/uploads/2012/06/s-1-1.jpg");
URLConnection conn1 = url1.openConnection();
conn1.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
InputStream in1 = conn1.getInputStream();
img1 = ImageIO.read(in1);
} catch (IOException e)
{
e.printStackTrace();
}
JLabel wIcon = new JLabel(new ImageIcon(img1));
imagesPanel.add(wIcon);
}
}
EDIT: I am getting closer in that I can now pop up a separate frame containing the picture using this code:
public class SimpleGUI extends JFrame {
private JPanel imagesPanel;
private JFrame mainFrame;
public SimpleGUI() throws IOException {
super("GUI Page");
setContentPane(imagesPanel);
pack();
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(new Dimension(500, 500));
mainFrame = new JFrame();
imagesPanel.setLayout(new GridLayout());
imagesPanel.setBounds(0,0,200,200);
mainFrame.add(imagesPanel);
mainFrame.setVisible(true);
I can't seem to find the original frame in my GUI form, so I can add the imagesPanel to that instead. I am using IntelliJ's GUI form builder.
Upvotes: 2
Views: 737
Reputation: 285440
One problem, you're trying to add multiple components to a BorderLayout using JPanel from within a for loop and by doing this, you'll over-write all that was added before with anything added new. Use a much more appropriate layout such as a GridLayout instead.
Another problem, where do you add the imagesPanel to anything? Calling setVisible(true)
on it will do nothing of use, but adding it to the JFrame or to a JPanel that is ultimately held by the JFrame will do a lot.
Also, you need to do some serious debugging usually before coming here -- does your reading in of the image work at all? Create a simple GUI, one that reads in the Image (use ImageIO.read(URL url) for this), creates an ImageIcon and displays the Icon in a JOptionPane. If this works, then the problem is elsewhere. If it doesn't then work on correcting the URL address.
Upvotes: 3