My program captures the screen's images, resizes and represents them in a JoptionPane icon filed so that the user can decide weather or not to save them.
The prob is that after performing Joption.cancel, the icon shows the previous image the next time the user captures the screen image. in Joption.ok case it works fine.
Any idea why this happens? the snapshots themselves are well rendered(every snapshot captures the very current screen image but the icon shows the previous one once cancel has been clicked) .
rszedSnp=ImageMagick.resize(origSnp_name,30);
ImageIcon icon=new ImageIcon(rszedSnp);
String userIput = (String)JOptionPane.showInputDialog(
new JFrame(),
"Save as:\n",
"taking screen shot",
JOptionPane.PLAIN_MESSAGE,
icon ,
null,
origSnp_name);
I've resolved the problem by using JDialog, with an implemented JPanel containing a JLabel holding the image, instead of using an image icon in the JOptionPane#showInputDialog function. .
BufferedImage img = ImageIO.read(new File(image));
resize(img);
JLabel lbl = new JLabel(new ImageIcon(img));
lbl.setVisible(true);
JPanel imgPan = new JPanel();
imgPan.add(new JScrollPane(lbl))
JDialog dialog = new JDialog();
JPanel globalPan = new JPanel(new GridLayout(2,2));
globalPan.add(imgPan);
dialog.add( globalPan );
dialog.pack();
dialog.setVisible(true);
Related
I am creating a profile page. At the left, I have added a jlabel containing a background image to my jpanel. Now over this panel, I want to add another jlabel for the icon. However, it is not showing up. Please help me.
P.s: This is only part of the code
public CustomerProfile()
{
super("Customer Profile");
setLayout(new BorderLayout());
//PROFILE DETAILS JPANEL
jpProfile = new JPanel();
add(jpProfile, BorderLayout.WEST);
//BACKGROUND IMAGE OF THE PROFILE JPANEL
ImageIcon background_img = new ImageIcon("background.jpg");
Image img = background_img.getImage();
Image tempImg = img.getScaledInstance(350, 600, Image.SCALE_SMOOTH);
background_img = new ImageIcon(tempImg);
background = new JLabel("",background_img,JLabel.CENTER);
jpProfile.add(background);
//PROFILE ICON
ImageIcon profImg = new ImageIcon("female.png");
jlProfileIcon = new JLabel();
jlProfileIcon.setIcon(profImg);
//ADDING PROFILE ICON TO JPANEL
jpProfileIcon = new JPanel();
jpProfileIcon.add(jlProfileIcon);
//jpProfile.setOpaque(false);
//jpProfileIcon.setOpaque(false);
jpProfileIcon.add(jlProfileIcon);
background.add(jpProfileIcon);
}
By default only a JPanel uses a layout manager.
The JLabel does not use a layout manager so the size/location of any component added to the label is not changed. The default size of a component is (0, 0) so there is nothing to paint.
Try:
background = new JLabel("",background_img,JLabel.CENTER);
background.setLayout( new BorderLayout() ); // added
…
background.add(jlProfileIcon);
The following code is not needed:
//jpProfileIcon = new JPanel();
//jpProfileIcon.add(jlProfileIcon);
That is you don't need to create a JPanel, just to add the label to it.
A better option is to paint the background image onto a panel and then you can just add your label normally to the panel. See: Background Panel for a class that implements this functionality.
I'm trying to make a buttom from an image on the JFrame using the ImageIcon and the addMouseListener that will replace the current image with another image by clicking it.
static JPanel jp = new JPanel();
final JLabel jl = new JLabel();
final JFrame jf = new JFrame();
ImageIcon image = new ImageIcon("image1.jpg");
jl.setIcon(image);
jp.add(jl);
jf.add(jp);
jf.validate();
JLabel button = new JLabel(image);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jl.setIcon( null );
ImageIcon image = new ImageIcon("image2.jpg");
jl.setIcon(image);
}
});
The GUI is displayed with image1.jpg, but the button does not work at all and I can't even test whether the replacement from image1 to image2 works. GUI will not do anything even if I attempt to click the image1.jpg displayed on the window.
Edit: Adjusted JLabel varaible to be final for now. Other similar questions intimate that this method should be working but I can't figure out what is wrong with the code.
Not really sure ActionListener works with JLabel either.
No you can't add an ActionListener to a JLabel. An easier approach is to make a JButton look like a JLabel, then you can add the ActionListener to the button:
JButton button = new JButton(...);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.addActionListener(...);
but the button does not work at all
A mouse click is generated when a mousePressed and mouseReleased is received for the same mouse point. So if you move the mouse slightly the event will not get generated. Instead listen for the mousePressed() event.
How does one resize an image that will be placed in a JLabel?
ImageIcon icon = ImageIcon("img.gif");
JLabel label = new JLabel(icon);
i have an URL image, and i want to display it in a panel.
How can i do it ?
One way to achieve this would be to use the URL class to grab the image from the web, create your ImageIcon object and then add it onto your JPanel,
Code is untested, but should demonstrate what you need to do.
URL img = new URL("http://www.example.com/whatever.jpg");
ImageIcon image = new ImageIcon(img);
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?
Cheers.
ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.
I'd probably use an ImageIcon and set it on a JLabel which I'd add to the JPanel.
Here's Sun's docs on the subject matter.
I would use a Canvas that I add to the JPanel, and draw the image on the Canvas.
But Canvas is a quite heavy object, sine it is from awt.
You could also use
ImageIcon background = new ImageIcon("Background/background.png");
JLabel label = new JLabel();
label.setBounds(0, 0, x, y);
label.setIcon(background);
JPanel panel = new JPanel();
panel.setLayout(null);
panel.add(label);
if your working with a absolut value as layout.