Question

I have a JFrame and I want to load a image to it when I click the load image button. Once I click next button I want to change the image. Loading image to a variable is no problem. I load it to Image img. I just want to show it. Which means i want to load it to a JLable or JPanel. How can I do this. Please help me.

Was it helpful?

Solution

OTHER TIPS

JLabel supports Icon/ImageIcons. This means that you can create a simple JLabel, pass an ImageIcon to it, and have it display. This should probably cover what you need.

JLabel label = new JLabel(new ImageIcon(image));

Create a custom component and override the paint(Graphics g) method. Use the given Graphics object to paint the image. You could cast to Graphics2D first if you need some more advanced options. Look through the methods defined by Graphics(2D), you'll find what you need pretty soon.

A subclass of JPanel would work well. The best choice really kind of depends on how you managed the contents of the JFrame.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top