Java Swing: popup : display small icon in menu - java

I have a right click popup menu that works fine.
I now want to add a little image next to the text.
I managed to display the image in the popupmenu but its the full size version.
what I want to know is:
is there a way to automatically reize the icon to be the right size ( same as the text ) or do I have to resize the image?
Jason

You have to resize the Icon to be whatever size you want it to be. Something like:
ImageIcon icon = new ImageIcon( ... );
Image image = icon.getImage().getScaledInstance(64, 64, Image.SCALE_SMOOTH);
JLabel label = new JLabel( new ImageIcon( image ) );

Related

How to set an offset to AWT Image or Swing ImageIcon?

I want to initialize an ImageIcon with an offset. The resizing is already working properly, but how can I tell the resized image icon to map to a certain x and y coordinate?
I have a map in my project and the attached snippet is the initialization of the image icon.
The image icon is then printed to a JLabel with a static size and position.
ImageIcon imageIcon = createImageIcon("/mapEnd1.png", "Map");
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(600, 800, java.awt.Image.SCALE_SMOOTH);
imageIcon = new ImageIcon(newimg);

How to get Image (obj) from ImageIcon

I am trying to give my interface a new function, but I have encountered some obstacles. I want to enlarge image on JLabel when mouseEnters.
Here is how my JLabels looks:
int sacle = 50 //Size of my JLabel Icon
int zoom = 10 // How much the icon should enlarge
imageIcon = new ImageIcon(new ImageIcon(myClass.class.getResource(Picture))
.getImage().getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
JLabel stackIsGreat = new JLabel();
stackIsGreat.setIcon(imageIcon);
//and I add multiple of such JLabels`
And the code goes on and on. I wanted to creat a function and add it to mouseListener, so all will behave the same. I wanted to achive that with:
//inside external method
activeLabel = (javax.swing.JLabel)(e.getSource());
ImageIcon temp = (ImageIcon) activeLabel.getIcon();
But there is no way I know I could get use of this, because java says I need Image to create my enlarged ImageIcon
ImageIcon enlarged = new ImageIcon((Image).getScaledInstance(scale + zoom, scale + zoom, Image.SCALE_SMOOTH))
How can I retrive the image used to crate the JLabel from code.
Any help would be appreciated.
I want to enlarge image on JLabel when mouseEnters.
Instead of creating your own MouseListener you could use a JButton to give you the rollover effect:
Something like:
JButton button = new JButton(...);
button.setBorderPainted( false );
ImageIcon icon = (ImageIcon)button.getIcon();
Image image = icon.getImage();
Image scaled = image.getScaledImage(...);
button.setRolloverIcon( new ImageIcon( scaled ) );

How to set an ImageIcon to a JButton and resize the picture according to the button's size?

I've been wondering how can I set an ImageIcon to a button using the Image path and set it to a JButton?
I can resize the image's size according to the buttons size:
frontViewImageFile = fc.getSelectedFile();
MainFrame.btnFrontView.setIcon(new ImageIcon(ImageIO.read(
frontViewImageFile).getScaledInstance(150, 150, Image.SCALE_SMOOTH)));
But the image came from a file chooser and I can use getScaledInstance method to resize the picture.
How can i do this with image path since method getScaledInstance is undefined for the type String?
ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));

How Do I Set An Icon For JLabel

I want to set an icon for a jlabel, can somebody give me example syntax
I have tried this :
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
panel.add(icon);
icon.setIcon(chromo);
After I tried this the label didn't show up on the panel at all.
Try this:
JLabel icon = new JLabel();
ImageIcon chromo = createImageIcon("res/icon.png");
icon.setIcon(chromo);
panel.add(icon);
And don't forget to check your layout, if it is absolute then you need to add bounds to it.
icon.setBounds(startX, startY, width, height);

abnormal resizing of java.awt.Image on using getImage() and getScaledInstance()

I m making a java rmi based application where i pass and receive an ImageIcon object from the server...
(the image is stored in a separate URL in server)
The function involves the following....
1. Getting the image from the server at first....(on button press A)
2. Replacing it with a image file in the client[optional]....(on button press B)
3. Remove the image with a default image[optional]....(on button press C)
4. Sending it back to the Server....................(On button press D).....
Here the image is displayed in ajlabel calledimg_label
The codes i've used are as follows.....
Variables used
java.awt.Image img;
javax.swing.ImageIcon CurrentImageIcon;
javax.swing.ImageIcon DefaultImageIcon;
// CurrentImageIcon contains the image to be displayed in the img_label....
// img is used for copying as well for scaling......
// DefaultImageIcon holds the default Image......
On Button Press A
img = temp.getImage();
CurrentImageIcon = new ImageIcon(img);
// Assuming temp holds the ImageIcon taken from the server.......
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press B
String url_text = jTextField.getText(); // taking the url frm the field.....
CurrentImageIcon = new ImageIcon(url_text);
img=CurrentImageIcon.getImage();
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press C
img = DefaultImageIcon.getImage();
CurrentImageIcon = new ImageIcon(img);
img=img.getScaledInstance(83,85 , Image.SCALE_DEFAULT);
img_label.setIcon(new ImageIcon(img));
img_label.revalidate();
img_label.repaint();
On Button Press D
// ImagetoSend is an ImageIcon to be sent to the Server.....
ImagetoSend = CurrentImageIcon;
CurrentImageIcon = null;
Now the problem i m getting is a weird one......
The image is getting embedded as i wanted on repainting when i click this button........
But when i download the recently uploaded image next time on Button press A....it is displayed either magnified or reduced to size even though i included the getScaledInstance method....
like this...
The image i am handling is a jpg image....
I even checked the image at Server directory.....No size change has occured on that file which was uploaded from client to server. But the change is observed when it is downloaded and embedded to the jlabel...
Can Anyone help me to sort out this issue...??
Somehow it is getting scaled down twice. Either you scale it down then send it to the server where it is scaled down, or the both button clicks scale it. Eliminate the scale down code on the server side and see what happens.

Categories