You may see this as a duplicate question but please here me out.
I have a JLabel with an image. This JLabel has a mouse listener binded to some function.
Is there any way to make the JLabel disappear (or technically, transparent) in order for the mouse listener function to be preserved?
I know I can set a transparent image as an icon to the JLabel, but I'm wondering if there's some kind of "toggle" function out there.
you could use yourLabel.setVisible(false); is not the same as dispose the frame; however, would you extend your question a little bit more or make it clearer?(I'm having a hard time trying to understand what do you want to do)
Related
I have to create a menu bar with a logo panel at far end of the bar and I have done it. But It's required that logo must have "appearing" effect? How can I do it?
I'm not exactly sure what you mean with "appearring effect", but if you want to create some color effects, you could extend the JLabel class and add some custom Color/Image Effects. Maybe this example for creating a JLabel with various effects gets you started.
See also How to animate a JLabel
Quick question, couldn't find a definitive answer from searching so I thought I'd ask here. In my code I set a JLabel to an image and I would like that image to clear and disappear after I perform a certain action such as clicking a button.
JLabel one = new JLabel(ruby); //ruby is a seperate ImageIcon already defined.
I'm not sure what the command to clear the panel is, an answer would be appreciated! Thanks.
Assuming that the label is already on the screen and you have a reference to the label you should be able to either call remove(label) on the parent container that contains the label or label.setIcon(null) if you want to reuse the label. In either case, you may be required to call revalidate on the parent container
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.
I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p
I have 3 image so I want, if I drag any image that should appear above all the other images. Which technique can use? please any idea???? By the way I am using "ImageIcon".
Thanks in advance.
If what you want to do is click on an image, lift it above the others and then drag it, consider placing them in ImageIcons and these in JLabels, and displaying them in a JLayeredPane, or a JPanel that is held in a JLayeredPane. You can then lift the clicked label onto the JLayeredPane.DRAG_LAYER while dragging, and then drop it down into the DEFAULT_LAYER (or on a JPanel that's on the DEFAULT_LAYER) when done. For an example of this, please see my code in a related question: dragging a jlabel around the screen
If I'm totally off base, sorry, but please correct my incorrect assumptions.
By the way I am using "ImageIcon".
I assume this means you are adding in image to a JLabel and are then adding the JLabel to a JPanel.
so I want, if I drag any image that should appear above all the other images
You need to add a MouseListener to the JLabel. When you click on the label then you can reset its Z-Order to 0. This will cause the label to be painted last and so it will be on top of all other labels on the panel.
So the basic code in the MouseListener would be:
panel.setComponentZOrder(label, 0);