JLabel multiline with HTML - java

I have a JLabel that I would like to contain a text that can go over multiple lines, and resizes if the window changes shape.
I've looked this up and most people seem to recommend wrapping the label text in HTML. This however does not make new lines for me.
My label is located in a JPanel and I suspect that the problem may be that my panel has misconfigured its border, and so the label text just continues beyond the panel border.
here is how the label looks inside the status panel
Here are the settings of the panel:
private final JPanel statusPanel = new JPanel(new FlowLayout());
statusPanel.setBackground(Color.white);
statusPanel.add(latestOrdreLabel);
this.add(statusPanel, new GridBagConstraints(0, 0, 6, 1, 1.0, 1.0
, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
Then the label is set up like this:
private final JLabel latestOrdreLabelResult = new JLabel();
String latestOrdreStatus = getBean().getLatestOrdreStatus(etelOrderInterface.getOrderId());
latestOrdreLabelResult.setText("<html>"+latestOrdreStatus+"</html>");
statusPanel.add(latestOrdreLabelResult);

In HTML, to down line your'e need to use <br/> instead of \n.
You can to use in replace String method, for example:
latestOrdreStatus.replace("\n", "<br/>");

You just need to resize the label.

Related

Java - Distance between JLabel and vertical box

I wrote this code for realize a box into I put four JRadioButtons
JRadioButton beginner = new JRadioButton("Beginner"); beginner.setSelected(true);
JRadioButton intermedie = new JRadioButton("Intermedie");
JRadioButton expert = new JRadioButton("Expert");
JRadioButton custom = new JRadioButton("Custom");
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
Furthermore I set the border of the box using the setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50)) method.
Also, I created a JLabel for indicate the title of the box:
JLabel difficulty = new JLabel("Choose the difficulty:");
This is the portion complete of the code:
Box boxDifficulty = Box.createVerticalBox();
boxDifficulty.add(difficulty);
boxDifficulty.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 50));
boxDifficulty.add(beginner);
boxDifficulty.add(intermedie);
boxDifficulty.add(expert);
boxDifficulty.add(custom);
How can I increase the space between JLabel and JRadioButton?
How can I increase the space between JLabel and JRadioButton?
You can add a spacing component to the panel after you add the label:
boxDifficulty.add(difficulty);
boxDifficulty.add( Box.createVerticalStrut(...) );
A strut is a fixed height(width) invisible component.

Word wrapping a JLabe/JTextArea in a JPanel in a JScrollPane

In my application, I need to word wrap Labels, which are contained in a JPanel (with GridBagLayout), which in turn is contained in a JScrollPane, which is contained in a resizable JFrame. The text should always be as wide as the Scrollpane allows(i.e. be completly visible at once). I tried multiple things already:
Putting tags around the text.
Result: Nothing
Putting on the Text
Result: Wraps, but with static width (does not respond to the Frame being resized) (duh)
Putting on the Text
Result: Nothing
Using a JTextArea, as in https://stackoverflow.com/a/26426585/4936150
Result: Appears to work at first, when the width of the Frame is increased, the words get "unwrapped" (so they are all on one line), but when the width is decreased afterwards, it will stay that way and not "wrap back". Also, weirdly doesn't work in the Nimbus L&F
Implementing Scrollable on the JPanel and returning true from getScrollableTracksViewportWidth()
Result: Works, but I DO want it to scroll horizontally if the Frame is too small for other Components
For reference, here is the source for my Panel class, without any wrapping:
public class ProgPanel extends JPanel {
/**
* Create the panel.
*/
public ProgPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0};
gridBagLayout.rowHeights = new int[]{59, 0, 0};
gridBagLayout.columnWeights = new double[]{1.0};
gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JLabel lblName = new JLabel("Long, long text taht needs to be wrapped, because it is displayed in a very big font on a kindof small screen, bla bla bla bhlergh lol rofl 1337");
GridBagConstraints gbc_lblName = new GridBagConstraints();
gbc_lblName.gridwidth = 2;
gbc_lblName.anchor = GridBagConstraints.WEST;
gbc_lblName.insets = new Insets(0, 0, 5, 0);
gbc_lblName.gridwidth = 1;
gbc_lblName.gridx = 0;
gbc_lblName.gridy = 0;
add(lblName, gbc_lblName);
lblName.setBorder(new EmptyBorder(10, 5, 0, 10));
lblName.setIconTextGap(10);
lblName.setIcon(new ImageIcon("someicon.png"));
lblName.setFont(getFont().deriveFont(Font.BOLD+Font.ITALIC, 48));
}
}
EDIT: Here is the implementation using a JTextArea, which will only grow in width, but not shrink: https://pastebin.com/egMXfwK1 (Pastebin due to long length and tedious SO formatting)
EDIT2: Here are some pictures showing the issue: http://imgur.com/a/mhtV9

How to personalize TextField design with Swing

Is there a way to let the textField of eclipse/swing this way?
You can add a matt look to the border of text field like this :
JTextField textField = new JTextField();
// left side border
textField .setBorder(BorderFactory.createMatteBorder(0, 5, 0, 0, Color.BLACK));
//right side border
textField.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 5, Color.BLACK));
If you need to customize only Inputfield, extends JTextInputField and Override the paint() method.
But probably you will need a full Look and Feel.

Java Swing - setting margins on TextArea with Line Border

As the title says, I am simply trying to set the margins (provide some padding) on a TextArea with a LineBorder set. Without setting the Border, .setMargins works fine. Here is the specific chunk of code.
aboutArea = new JTextArea("program info etc.....");
Border border = BorderFactory.createLineBorder(Color.BLACK);
aboutArea.setSize(400, 200);
aboutArea.setBorder(border);
aboutArea.setEditable(false);
aboutArea.setFont(new Font("Verdana", Font.BOLD, 12));
add(aboutArea);
I have tried each of these:
aboutArea.setMargins(10,10,10,10);
.getBorders(aboutArea).set(10,10,10,10);
UIManager.put("aboutArea.margin", new Insets(10, 10, 10, 10));
but nothing affects the margins after I apply the border, the padding is always 0. Any ideas how to set the padding on the textArea with the border?
What if you try adding a CompoundBorder , won't this do, this will give you almost same thing
JTextArea tarea = new JTextArea("program info etc.");
Border border = BorderFactory.createLineBorder(Color.BLACK);
tarea.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));

Swing - how to mix JTextField and JTextAreas and have same visual appearance?

I am using miglayout to create a form in which there are JTextFields (short input answers) as well as JTextAreas (Longer answers). The problem is twofold.
The border placed around a Scrollpane wrapped text area does not match that of a Text Field.
The width and placement of the textarea/textfield differ, causing them not to line up correctly.
alt text http://grab.by/3O0V
After changing from right/left to right/fill:
alt text http://grab.by/3RMk
You can see that the bounds line up, but that there are still gaps. I tried setting novisualpadding but this did not fix it.
Source code:
package test2;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class Test extends JPanel {
private static final int NUM_CHARACTERS_WIDTH = 20;
private static final int NUM_ROWS = 5;
public Test() {
setLayout(new MigLayout(
"wrap 2",
// Align text labels on the so their right edge meets left edge of the text fields
"[right][left]"
));
add(new JLabel("Text field:"));
add(new JTextField(NUM_CHARACTERS_WIDTH));
add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
add(new JLabel("Scrollpane text area:"));
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)));
add(new JLabel("Text field:"));
add(new JTextField(NUM_CHARACTERS_WIDTH));
}
public static void main(String[] args) {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new Test();
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
What's the preferred way to mix and match jtextfield and jtextareas, while still maintaining visual harmony? I notice now that the text field has a blue highlight around it when focus is in it, as opposed to the text area... another source of visual discontinuity.
I know this question is pretty old, but to get the border for a TextArea to match that of a TextField:
(myTextArea).setBorder(new JTextField().getBorder());
That should give a border to your TextArea like the one around a TextField.
Not sure how you can fix your border problem but to fix your layout situation I would just use springlayout. Springlayout is just a way to better layout your elements within the JPanel. You can find out more about it Java Sun Tutorial
Specifically you use it by setting where you want your North, South, West and East borders of each element. To do this you would have to first take your label calls out of the add so each one can be named. So instead of:
add(new JLabel("Text field:"));
Do:
JLabel myLabelName = new JLabel("Text field:");
add(myLabelName);
For each of your elements (JLabels, JTextAreas and JTextField). Once this is done you can easily set the layout.
Springlayout layout = new SpringLayout();
setLayout(layout);
Then for each of the elements you have to set any of the borders you want. They have to be in the specific order South, the North, West then East. Though you don't have to use all four borders if you don't want to. Here is an example on how to set your first text area, the one on the top.
layout.putConstraint(SpringLayout.NORTH, FirstTextAreaName, 10, SpringLayout.NORTH, this);
layout.putConstraint(SpringLayout.WEST, FirstTextAreaName, this.getWidth()/2, SpringLayout.WEST, this);
layout.putConstraint(SpringLayout.EAST, FirstTextAreaName, -10, SpringLayout.EAST, this);
This example doesn't set the south of the text area but if you did want to it would have to be first. The first line sets the north side of the text area to be 10 pixels away from the top. When setting the other areas you but the previous (above) areas name instead of this and say it is 10 pixels away from the south of the previous one:
layout.putConstraint(SpringLayout.NORTH, SecondTextAreaName, 10, SpringLayout.SOUTH, FirstTextAreaName);
The second line in the above example sets the east side of the text area to start halfway through your main panel. The last, third, line sets the east side of the text area to be 10 pixels from the east side of your main panel.
I know that MiGLAyout (which I love, BTW) has the ability to do special handling for visual alignment vs strict pixel alignment. You may be running into this... The 'al' unit identifier is used for this, but I haven't had to use it so can't provide examples. It would probably be worth downloading the MiG sample project and see if they have the same alignment issue (I'm sure they have panels similar to yours).
For what it's worth, we mix text fields and areas in the same panel quite frequently and don't run into this... We do have to set the border of the scroll pane to be the same as the border of the text field as suggested by Noel Ang.
Also, instead of specifying constraints in the layout constructor, we generally specify them as we add each component - not sure if that makes a difference or not...
For the layout problem, try a columnConstraints value of [right][fill] instead of [right][left].
For the other issue, this appears to be a look-and-feel inconsistency. I ran your code in Windows, and the differences are there too, but less flagrant. My suggestion would be to set identifical borders explicitly for text fields and text areas.
setLayout(new MigLayout(
"wrap 2",
"[right][fill]"
));
JTextField textField;
JScrollPane scrollPane;
add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);
add(new JLabel("No scrollpane text area:"));
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
add(new JLabel("Scrollpane text area:"));
scrollPane = new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH));
scrollPane.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(scrollPane);
add(new JLabel("Text field:"));
textField = new JTextField(NUM_CHARACTERS_WIDTH);
textField.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
add(textField);
If you can't get MigLayout to align your components, considering using java.awt.GridBagLayout:
import static java.awt.GridBagConstraints.*;
setLayout( new GridBagLayout() );
GridBagConstraints leftCons = new GridBagConstraints();
leftCons.anchor = NORTHEAST;
leftCons.fill = NONE;
leftCons.weightx = 1.0;
leftCons.gridy = RELATIVE;
leftCons.gridx = 0;
leftCons.insets = new Insets( 4, 8, 4, 8 );
GridBagConstraints rightCons = new GridBagConstraints();
rightCons.anchor = NORTHWEST;
rightCons.fill = HORIZONTAL;
rightCons.weightx = 1.0;
rightCons.gridy = RELATIVE;
rightCons.gridx = 1;
rightCons.insets = leftCons.insets;
add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);
add(new JLabel("No scrollpane text area:"), leftCons);
add(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH), rightCons);
add(new JLabel("Scrollpane text area:"), leftCons);
add(new JScrollPane(new JTextArea(NUM_ROWS, NUM_CHARACTERS_WIDTH)), rightCons);
add(new JLabel("Text field:"), leftCons);
add(new JTextField(NUM_CHARACTERS_WIDTH), rightCons);
First off +1 for screen shots.
Since you are using Mac, did you try Quaqua Look And Feel? It renders the textboxes/areas properly.
The answer is that MiG Layout folks are working on a fix for their next version.
Hello,
Apple has a nasty habbit of compensating by default and not let the developer decide. This is such a case where they have added a border to make it more visually like OS X. This should be the choice of the layout manager...
MigLayout can compensate for visual bounds like this but it is only done for JTabbedPane in Windows XP. I'm not sure it can be done 100% good in OS X though. I'll have to check. We don't want the text field to just grow into the bounds.
I have added this to the todo list for the next version.

Categories