Reputation: 1683
I'm trying to get used to MigLayout
and I can't figure out this problem.
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
JLabel htmlLabel = new JLabel();
panel.setLayout(new MigLayout());
htmlLabel.setText(""); // This is where I put my text.
panel.add(htmlLabel);
frame.setPreferredSize(new Dimension(400, 300));
frame.setMinimumSize(frame.getPreferredSize());
frame.setMaximumSize(frame.getPreferredSize());
frame.setLocation(0, 0);
frame.add(panel);
frame.setVisible(true);
}
So with this example, if I do
htmlLabel.setText("Some TEXT")
htmlLabel.setText("Some LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG TEXT");
<html></html>
around the text i.e. htmlLabel.setText("<html>Some LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG TEXT</html>")
BoxLayout
to the panel. i.e. panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
So what's wrong with #3? Why does MigLayout
center the text like that as the text grows but just when inside html
tag?
NOTE: I know html
tag will make the text wrap but it shouldn't move to the center...
After trying Charles Knell's answer, it worked. Just an update on how to do it with MigLayout
. I add my label to the layout like this:
add(helpLabel, "grow")
Upvotes: 2
Views: 183
Reputation: 21
This is a known issue in MigLayout (https://github.com/mikaelgrev/miglayout/issues/41). It's a regression in 5.0 and will be fixed in the next release (current release as of May 2018 is 5.1).
dguay's workaround works well if it's okay to grow the HTML JLabel
.
Upvotes: 1
Reputation: 583
You are using the html tag is a very unconventional way.
That might explain why you seem to get some unexpected results.
The minimum that you need, tag wise, is
<html><body><p>your content</p></body></html>
see https://www.w3schools.com/html/html_elements.asp
I ran your code as follows and it works perfectly:
package htmlmiglayout;
import javax.swing.*;
import net.miginfocom.swing.*;
import java.awt.Dimension;
public class HTMLMigLayout {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //new
JPanel panel = new JPanel();
JLabel htmlLabel = new JLabel();
panel.setLayout(new MigLayout());
htmlLabel.setText("<html><body><p>Some LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG TEXT</p></body></html>"); // This is where I put my text.
panel.add(htmlLabel);
frame.setPreferredSize(new Dimension(400, 300));
frame.setMinimumSize(frame.getPreferredSize());
frame.setMaximumSize(frame.getPreferredSize());
frame.setLocation(0, 0);
frame.add(panel);
frame.setVisible(true);
}
}
However, if I use different label dimensions and set the alignment so that the top of the text begins at the top of the label, I can reproduce your output.
Here is the modified code:
package htmlmiglayout;
import javax.swing.*;
import net.miginfocom.swing.*;
import java.awt.Dimension;
public class HTMLMigLayout {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //new
JPanel panel = new JPanel();
JLabel htmlLabel = new JLabel();
htmlLabel.setMaximumSize(new Dimension(300,95));
htmlLabel.setMinimumSize(new Dimension(300,95));
htmlLabel.setVerticalAlignment(SwingConstants.TOP);
panel.setLayout(new MigLayout());
htmlLabel.setText("<html><body><p>Some LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG LONNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNG TEXT</p></body></html>"); // This is where I put my text.
panel.add(htmlLabel);
frame.setPreferredSize(new Dimension(400, 300));
frame.setMinimumSize(frame.getPreferredSize());
frame.setMaximumSize(frame.getPreferredSize());
frame.setLocation(0, 0);
frame.add(panel);
frame.setVisible(true);
}
}
and here is the output
So, You are getting default JLabel dimensions which don't allow for displaying the end of the string.
Upvotes: 2
Reputation: 151
I believe your html tags are defining a border around the page, and your "long text" is reaching that border, creating a new line by the space.
It's the same way when writing text. If you reach the near the end of a line, then have a word that's longer than the character limit per line, it doesn't do a hyphen, similar to the way books do. It will start a new line, continuing the word.
My reasoning behind this? The text starts a bit off the left side of the page, yet doesn't reach the very end of the same line. It's not symmetrical.
I don't have experience in MigLayout, but I believe this may be what's causing the distortion between the text from using the html tags vs not using them.
Upvotes: 1