I'm trying to find a way to replace all the contents of a JDialog to simply an image. It's for the about page of a project I'm working on and I want when the user clicks on the About section, an image to popup in the style of a JDialog(and to disappear when focus is lost). Example: http://www.tecmint.com/wp-content/uploads/2012/08/About-Skype.jpg There Skype displays only an image they've created as their "About" page. How can I make an "image dialog" in Java(swing)?
Asked
Active
Viewed 1.9k times
3 Answers
6
How can I make an "image dialog" in Java(swing)?
Use an undecorated JDialog with a JLabel containing an ImageIcon:
JDialog dialog = new JDialog();
dialog.setUndecorated(true);
JLabel label = new JLabel( new ImageIcon(...) );
dialog.add( label );
dialog.pack();
dialog.setVisible(true);

camickr
- 321,443
- 19
- 166
- 288
5
BufferedImage image = ImageIO.read(new File("myfile.png"));
JLabel picLabel = new JLabel(new ImageIcon(image));
JOptionPane.showMessageDialog(null, picLabel, "About", JOptionPane.PLAIN_MESSAGE, null);

martinez314
- 12,162
- 5
- 36
- 63
3
Here you go, I have annotated the code for you
import javax.swing.JOptionPane; //imports
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import java.awt.Toolkit;
import java.awt.Dimension;
public class img{
public static void main(String[] args){
JFrame f = new JFrame(); //creates jframe f
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //this is your screen size
f.setUndecorated(true); //removes the surrounding border
ImageIcon image = new ImageIcon(diceGame.class.getResource("image.png")); //imports the image
JLabel lbl = new JLabel(image); //puts the image into a jlabel
f.getContentPane().add(lbl); //puts label inside the jframe
f.setSize(image.getIconWidth(), image.getIconHeight()); //gets h and w of image and sets jframe to the size
int x = (screenSize.width - f.getSize().width)/2; //These two lines are the dimensions
int y = (screenSize.height - f.getSize().height)/2;//of the center of the screen
f.setLocation(x, y); //sets the location of the jframe
f.setVisible(true); //makes the jframe visible
}
}
[[OLD]] The code below will do what you're looking for.
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class img{
public static void main(String[] args){
JLabel lbl = new JLabel(new ImageIcon(diceGame.class.getResource("image.png")));
JOptionPane.showMessageDialog(null, lbl, "ImageDialog",
JOptionPane.PLAIN_MESSAGE, null);
}
}

MeryXmas
- 63
- 5
-
That looks great, though what can I do to remove the window borders of the JOptionPane, so that the image is the only thing left? – martin Mar 06 '13 at 22:00
-
Where does the `ImageIcon image = new ImageIcon(diceGame.class.getResource("image.png"));` come from, I mean the diceGame explicitly. Why is it not simply "img.class~" as the class is called? – N30 May 07 '14 at 11:09