how to make JLable visible and Translucent (almost invisible) on mouse hovering - java

I made a Wrappable JLabel which shows text message within the label with multiple lines.
The label and text (i.e. foreground and background) are visible all time.
Now I want to change the JLabel such that it should be visible only after mouse hover and
rest of the time it should be translucent. How can I achieve this? please help me ..
thanks in advance .

For mouse hover over the JLabel you have to call repaint() in all methods from MouseXxxListener, reason is the this notifier isn't implemented in the JLabel API, more inc. descriptions in the post by #kleopatra

Related

How do i get rid of a blue border surrounding my button? For Java GUI

Because it wont let me add an image - new account
This is utilizing the Java GUI
The above image is an image pasted onto a button, I've tried to make the button transparent so that the user can't see it but I can't seem to get rid of this blue border.
Code I have so far
boss2 = new JButton(); //declared the static button earlier on in the code
boss2.setSize(300, 300);
boss2.setLocation(315, 200);
boss2.setIcon(new ImageIcon("dragon.gif"));
boss2.setRolloverIcon(new ImageIcon("dragon.gif"));
boss2.setOpaque(false);
boss2.setContentAreaFilled(false);
boss2.setBorder(null);
Is there a way to get rid of the blue border surrounding my image?
edit - sorry for the earlier mishap, uploaded the wrong file
I would suggest that what you are seeing is the focus rectangle, used to "highlight" the button as having keyboard focus.
You can use boss2.setFocusPainted(false); to stop it from been painted.
To not have a boarder drawn for a JButton (assuming that you are using javax.swing.JButton) you can simply do:
boss2.setBorderPainted(false);

JLabel positioned on top of TextPane, disappears when text pane repainting

I have positioned a Jlabel on top of a text pane, which acts like a top border to the textpane. My problem is when typing on the textpane, the label get disappeared. Please give me a way to keep the jlabel visible all the time.
Thanks in advance.
---------Edit---------
Giving a code sample would be lengthy. Please refer the images below to get an idea about what happens.
1) before typing in textPane.
2) after typing.

How can I change colors of components when the mouse is pressed in a JFrame in Java?

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

gwt focus on flowpanel or any other panel

I am doing project in gwt. In which i have added multiple flowpanel which contains other things like textboxes, labels and buttons. My question is how to focus on a particular flowpanel. I know which panel to focus, but not found any method to do the focus.
I have also used focuspanel but it just focus on that panel and no scroll event happens if panel is last panel.
Please if anyone knows how to do this, please reply.
Thanks
Try using setFocus on a TextBox that is inside the FlowPanel you wish to focus to.

How do I set Z-order for image in Swing Java

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);

Categories