Can I make resizable the icon inside a jlabel? - java

Is there any property or any code that I can implement to make an icon inside a JLabel resizable. Or is there any other item which can store an image, that I could use to have a resizable image inside a JFrame?

use this code :
photo = new ImageIcon(UrlofPhoto);
Image im = photo.getImage();
Image Newim = im.getScaledInstance(yourlabel.getWidth(), yourlabel.getHeight(),Image.SCALE_SMOOTH);
ImageIcon Newphoto=new ImageIcon(Newim);
yourlabel.setIcon(Newphoto);
don't forget to set an initialised size to yourlabel

Generally, I would't suggest trying this with a JLabel as JLabel has a lot of other features you really don't want to messing with (alignment and text).
Generally, a better solution is to use a dedicated "image" panel, which you can provide additional control over to fine tune how you want the scaling to work.
For example
If you're really stuck on using JLabel, I would recommend attaching a ComponentListener to it and resizing the underlying image when ever it changes size. The problem with this is componentResized may be called repeatedly in quick succession, meaning you will need to devise some kind of coalescing algorithm that only reacts to the last event within a given period of time...

Check out Darryl's Stretch Icon which will dynamically scale the icon to fill the label.

Related

Setting size on JLabel displaces other components in java

I am setting a JLabel for the error messages in my program, so initially the label is empty label.setText(""), but when there is an error it should change to something like label.setText("Error, you have entered invalid data...").
If I use setSize(x,y) on the label, it forces other components to displace when error message takes place. But using setPreferredSize(Dimension(x,y))doesn't impact them.
Q1. Why is that?
Q2. What is the difference between setSize(x,y) and setPreferredSize(Dimension(x,y))
Q3. Does it have to do anything with layout?
Thank you in advance for explanation!
P.S. I am using GridBagLayout for positioning my components on the JPanel.
Don’t use the setSize method.
setSize is called by LayoutManagers, like GridBagLayout, to lay out child components. When you call setSize explicitly, you are fighting with the GridBagLayout. Eventually, GridBagLayout will undo your setSize call, when it calls setSize for its own purposes.
In other words, any call to setSize eventually will be wiped out by the parent layout.
setPreferredSize will not be wiped out. Most LayoutManagers, including GridBagLayout, do their best to respect a component’s preferred size.
However, you should not be calling setPreferredSize. Components already have a preferred size by default, and it is almost certainly better than any numbers you can come up with. For instance, a JLabel’s default preferred size is the size which is just large enough to accommodate its text, icon, and borders.
Computing a preferred size is harder than you might think. How many pixels does text use? How many pixels high is a 12 point font? 12 points is not 12 pixels. 12 points is 12⁄72 inch. How many pixels is that? It depends on the user’s monitor and graphics resolution. All of this is known to the Swing rendering system, and JLabel uses all of that information to determine its default preferred size. You should not try to reinvent all of that work, and you should not try to replace that work with something simpler, as it will be inadequate.
If you just let the JLabel keep its preferred size, GridBagLayout will do its best to accommodate that. If the window itself does not have room to display the JLabel’s new text, you probably should call the window’s pack() method after changing the text.
Update: This appears to be an XY problem—you really want a message that you can show and hide.
You want your layout to be big enough to accommodate your message text as soon as you create it. This is typically done with a CardLayout, which lets you place several components on top of each other, with only one of them visible at any given moment. Since you want to show no text at all, initially, you would add an empty JLabel as the first component in the CardLayout, so it is shown by default:
JLabel label = new JLabel("Error, you have entered invalid data...");
CardLayout messageLayout = new CardLayout();
JPanel messagePane = new JPanel(messageLayout);
messagePane.add(new JLabel(), "blank");
messagePane.add(label, "message");
// Do not add label directly to your user interface.
// Add messagePane instead.
mainWindow.add(messagePane);
// ...
// Show message
messageLayout.show(messagePane, "message");
// ...
// Hide message
messageLayout.show(messagePane, "blank");
"message" and "blank" are never seen by the user. They are just unique identifiers for each component (“card”) in the CardLayout. You can make them anything you want.
The setSize() function sets the size not based on any LayoutManager. Thats why you should always use setPrefferedSize() when working with a LayoutManager. setPrefferedSize() firstly tries to be conform with the LayoutManagers dimensions if then possible Java tries to set the size of the Label according to your setPrefferedSize() input.
So yes, it does have anything to do with layout. If possible, you should only use setPrefferedSize() as you are working with layout managers.

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

Position image in a JTextPane (like using setBounds to position JPanel in a JFrame)

I'm new to Java and I would like to know how to position an image within a JTextPane. Is it possible? Is there any other methods for positioning an image in a JLabel?
The gridBagLayout can be used to position components within a JFrame.
Take a look here: http://docs.oracle.com/javase/7/docs/api/java/awt/GridBagLayout.html
I usually use gridLayout,it is less flexible, but easy to use for simple layouts.
You can create a JLabel for the image, call setBounds() for the image and call jTextPaneInstance.add(theImageLabel).Or you can specify any desired LayoutManager for the jTextPaneInstance and just add the label with appropriate constraint.
If you mean something like text flow around the image it's much much more difficult to implement but also possible (depends on EditorKit you use there).

Java Swing JLabel Text longer than bounds

I have a problem with Java Swing JLabel.
The text i want to display on the JLabel exceeds the bounds of the JLabel. I want to display it via a Marqueeeffect. I already implemented the effect but when there is a string that exceeds the bounds of the JLabel it gets cut off and the rest gets replaced with "...".
My question is, if there is any opportunity to set the textlength for a JLabel individually, not depending on the bounds, that it doesnt get cut off?
Hope somebody got an answer for me.
I dont use any LayoutManagers and i dont want the JLabel to get resized, it should only can contain text longer than the bounds of it.
I want to display it via a Marqueeeffect.
Check out the Marquee Panel.
In this LayoutTest, you can see how the label's UI delegate uses layoutCompoundLabel() to elide the text when label's size falls below the preferred size.
In this MarqueeTest, MarqueePanel has a default FlowLayout, which adopts the display label's preferred size.
The Swing JLabel was not designed to do marquee scrolling.
Here's the source code for JLabel. You can modify the text handling routines to do a marquee scroll rather than compressing the text with an ellipsis.
Oh, you'd better use a layout manager. Your marquee JLabel won't layout correctly without a layout manager.

How to find if image is clicked in JApplet?

It is to support image dragging. How to find if an image is clicked in JApplet?
I'm sure there is an easy way to do this, but I've looked everywhere and cannot seem to find it.
Options:
Use a JLabel to hold the image, and give it a MouseListener. Simple.
Or create a JButtton and use the Image as the button's ImageIcon. Probably simpler.
See the Drag and Drop and Data Transfer lesson of the tutorial.
If the purpose of the dragging is to change the order in a slideshow or similar, look to a JList. See setDragEnabled(true) & How to Use Lists for more details.
For the display component, I would recommend a JLabel as suggested by #Hover. JList uses a JLabel as the rendering component by default.

Categories