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

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.

Related

Text Not Aligning To the Center of JLabel in Java

I have a JLabel on the bottom of my panel that gives text instructions to the user. Some of the text went off the screen because it was too long. To fix this, I added tags. However, now the text is no longer centered, it is now aligned to the left. Why is this case. Shouldn't this code center the text?
detailedInstructions.setText("<html><div align=center>" + test.getMicroSteps()[currMicroStep].getDescription() + "</div><html>");
Instead of using HTML code, simply call
detailedInstructions.setHorizontalAlignment(SwingConstants.CENTER);
Or use the JLabel constructor that sets the horizontal alignment.
Note that if the JLabel displays an ImageIcon, then you'll also want to set it's horizontal text position which determines the location of the label's text relative to its image.
Other potential issues is that the JLabel itself might not be centered at teh bottom of the GUI. To test this, put a border around the label to see it's actual placement, and if so, then you will need to work with the settings of the label's container's layout manager.

Can't vertical center align text inside a JLabel

numBox is a square JLabel like the ones in the game 2048. The text where the number goes will not vertically center.
neither
numBox.setVerticalAlignment(JLabel.CENTER);
nor
numBox.setVerticalTextPosition(JLabel.CENTER);
are working.
Text shows up horizontally centered but at the top of the box that the label shows up in.
How do I get the text to show up in the middle of the JLabel?
You can give an alignment suggestion to the layout manager by using:
label.setAlignmentY(JLabel.CENTER_ALIGNMENT);
If this doesn't help then post a proper SSCCE that demonstrates the problem.

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

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

Overlapped JLabel in Frame

I am writing a code in Java Swing in which a label is added in frame on the click of the button. And the Labels are added such that it overlap each other to create a stack like view.
I am creating my own layout using label.setbouds(x,y,100,100) function.
And each time button is clicked in its action listener a new label is added.
For example
if(e.getSource()==button){
label.setBounds(x+5,y+5,100,100);
frame.add(label);
frame.repaint();
}
Now the problem is when these labels are painted the first label added always remain on the top and newly add Labels are overshadowed by odd label.
I need a help if someone could explain how i can paint the frame such that newly added frames comes on the top and old labels get overlapped.Any suggestion is appreciated
Regards
ACoder
Insert your labels in a JLayeredPane with the proper index. Also make sure to set your JLabel as opaque (setOpaque(true)) so that it will actually obscure the content of the labels below. However, if a label below is bigger than the label on top, you will still see some part of it

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