java JLabel text exceeds parent JLabel width - java

I have a class called iconLabel that extends JLabel to make buttons.
I use the Font Awesome font to set the text of the JLabel to an icon. I also add another JLabel to that iconLabel object in the constructor to display text on the icon. The problem is that the text on the icon exceeds the width of the icon, so you'll get "...". How can I make it so the JLabel text may exceed the icon width?
Here is an image for clarification.

The problem is that the text on the icon exceeds the width of the icon, so you'll get "...".
A JLabel can display an Icon with text painted on top of the Icon. There is no need for a custom class to do this.
The basic code is:
JLabel label1 = new JLabel( new ImageIcon(...) );
label1.setText( "Easy Way" );
label1.setHorizontalTextPosition(JLabel.CENTER);
label1.setVerticalTextPosition(JLabel.CENTER);
The thing is that I want to use the font awesome icons
So you use the setText() method and you get an Icon painted? Cool!
Maybe you can use a JPanel with an OverlayLayout. Then you add the two labels to the panel, making sure the alignmentX/Y values are both 0.5f so the components are centered in the panel. Now the panel should be the size of the largest label.
Or maybe you can use the Text Icon. This class allows you to create an Icon from a text String. You will still need to labels but the sizing should work correctly.
The code should be something like:
JLabel iconLabel = new JLabel();
iconLabel.setFont(...);
TextIcon icon = new TextIcon(iconLabel, "...")
Jlabel label = new JLabel("some text");
label.setIcon(icon);

Related

How do I stop the position of JLabels changing inside a JPanel whenever I change the text of other JLabels in the same JPanel?

I have multiple JLabels in a JPanel. Whenever I change the text in one, it causes the other JLabels inside the JPanel to move. How to I lock them in place?
The approach really depends how you labels are positioned and what LayoutManager you are using.
JLabel by default will calculate its size mostly based on the label text and font size. If you are changing the text, then the JLabel size will change as well and thus make the layout manager to reposition the other labels.
Here is an example that uses FlowLayout and hard sets JLabel preferred size so that it wont change when its text changes. Other layout managers might choose to ignore the preferred size:
JFrame frame = new JFrame();
frame.getContentPane().setLayout( new FlowLayout() );
JLabel l = new JLabel("text1");
l.setPreferredSize( new Dimension( 50, (int)l.getPreferredSize().getHeight() ) );
frame.getContentPane().add(l);
frame.getContentPane().add( new JLabel("text2") );
frame.getContentPane().add( new JLabel("text3") );
Here if you change text for l it wont shift the other 2 labels.

Putting a Panel on top of a JLabel backGround (Java)

I've been fruitlessly searching the internet and nothing that people suggest seems to have any effect for me.
I have a JFrame which I'm trying to put a JPanel in. That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground. The issue is every type of layout manager I've seen suggested just does not work as advertised for me. The best I've gotten to work so far is this approach:
public MenuBackgroundPanel(AsteroidsFrame frame)
{
this.gameFrame = frame;
this.setLayout(new OverlayLayout(this));
ImageIcon image = new ImageIcon(getClass().getResource("/resources/background1.gif"));
imageLabel = new JLabel(image, JLabel.CENTER);
mp = new MainMenuPanel(gameFrame);
mp.setMaximumSize(new Dimension(300,200));
this.add(mp);
this.add(imageLabel);
this.setVisible(true);
}
Unfortunately, I'm getting really strange alignments and trying to set location on the background (to actually get it to start at the JFrame's (0,0) or moving the button panel just seems to have no effect. Printing the location of each object says they're both at (0,0) but the image I'll link shows this is just not the case. My point is, I've tried things like JLayeredPane or setting the JLabel as the contentpane of the Jframe and making it transparent but nothing seems to do anything. One or the other of the two objects just covers the other completely.
As you can see the objects are not at all aligned.
Could anyone help me with this?
That JPanel ideally would have a JLabel with an imageicon as the background and a set of buttons in its own Jpanel in the foreground
Easiest way for something like this when the child panel is fully contained in the label image is to just set the layout manager of the JLabel and then add your components to the label.
JLabel background = new JLabel( new ImageIcon(...) );
background.setLayout( new GridBagLayout() );
JPanel buttons = new JPanel();
buttons.setOpaque( false );
buttons.add(...);
background.add(buttons, new GridBagConstraints() );
Now the button panel will be centered on the label.
As you can see the objects are not at all aligned
If you want to use the OverlayLayout then you need to play with the alignmentX/Y properties of each component. You would probably want to set them both to .5. Check out: Java Layout with Component always in Top Right for an example of how changing these values can affect the layout.

Put label on a label in netbeans GUI builder

Trying desperately to put a label over another label in netbeans because the one label is acting as the background image and I want the label in the foreground to have a different image.
Every time i drag another label on top, the JFrame gets bigger and the label tries to slot down the side.
Here is what I've got (I want the label where the red circle in the middle is not on the right down the side):
:
If you want to put one component on another, then you can use Layered Panes.
A layered panel will let you specify that some child components should be layered above other child components.
Update:
An example:
JLayeredPane pane = new JLayeredPane();
JLabel lbOne= new JLabel("Label one");
JLabel lbTwo = new JLabel("Label two");
pane.add(lbOne, 0);
pane.add(lbTwo , 1);
You just need to set the JFrame layout to null..
If you use panel below set the panel layout also null hope its work for you .

Java how to preserve newline from String to JLabel?

JLabel newLabel = new JLabel();
String a = b;//b is String from database.
newLabel.setText(a);
I need to generate text pulled from my database which contains multiple line, but when i put them into the label, all of the text became same line.
I tried using JTextArea and it works, however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel... which I wanted all the content to be aligned to the left. Any help would be much appreciated! thanks
however, for some reason it messed with all the other component's alignment in a 1 column BoxLayout panel
The is related to the setAlignmentX(...) method of each component.
A JLabel uses 0.0f (for left alignment). A JTextArea and JScrollPane use 0.5f (for center alignment).
Using components with different alignments will cause alignment issues when using a BoxLayout.
The solution is to change the alignment of the text area or scroll pane (whichever component you add to the BoxLayout).
So the basic code would be:
JLabel label = new JLabel(...);
JTextArea textArea = new JTextArea(...);
textArea.setAlignmentX(0.0f);
JPanel panel = new BoxLayout(...);
panel.add(label);
panel.add(textArea);
Now both components will be left aligned.

Can I highlight text in a JLabel?

I have a JLabel with some text in it. This JLabel takes up the whole bottom side of my JFrame. I have changed the background of it to yellow, and the whole bottom side becomes yellow. Is there any way to make it look like the text, which is within the JLabel, is being "highlighted" with only the text having a background color of yellow, and the rest having a different color? The top part of my JFrame is a JPanel.
combo.addItemListener(new ItemChangeListener());
JPanel container = new JPanel();
container.setBackground(Color.BLUE);
response.setFont(new Font("Times new Roman", Font.BOLD, 40));
response.setHorizontalAlignment(SwingConstants.CENTER);
response.setBackground(Color.YELLOW);
response.setOpaque(true);
add(response , BorderLayout.CENTER);
container.add(selectone);
container.add(combo);
this.add(container, BorderLayout.NORTH);
There is a very simple way to do that - use HTML in the text of the label. Mind you, Swing interprets a pretty old version of HTML, that doesn't meet today's standards.
JLabel response = new JLabel( "<html><span bgcolor=\"yellow\">This is the label text</span></html>" );
Nowadays, one should work with style sheets. But if you want to do that, you should probably switch from Swing to JavaFX.
For further details on using HTML in Swing components, see the applicable part of the tutorial.
Use a wrapper panel that respects the preferred size of the label:
JLabel label = new JLabel("...");
label.setOpaque( true );
label.setBackground(...);
JPanel wrapper = new JPanel();
wrapper.add( label );
frame.add(wrapper, BorderLayout.CENTER);
The default layout for a JPanel is a FlowLayout that is horizontally center aligned.
If you want both horizontal and vertical centering then use a GridBagLayout on the panel, with the default GrigBagConstraints when you add the label to the panel.

Categories

Resources