vaadin label text not wrapping - java

As it says on the tin.. I cannot get a label's text to wrap. Essentially I am building like a comments panel, user enters a comment and its displayed with timestamp etc.
I want the labels to both display with ContentMode.PREFORMATTED but also wrap.
The layouts of which contain the label are fixed width (100%) as is the label obviously by default, from what I have readin the book of vaadin what I am doing should work?
here is my code snippet:
VerticalLayout container = new VerticalLayout();
rootLayout.addComponent(container);
VerticalLayout comment = new VerticalLayout();
container.addComponent(comment);
Label createdByLbl = new Label(entity.getManagedMetadata().getAttrValue("createdBy") + " said:");
createdByLbl.setId("conversation.comment.username." + repositoryUID);
createdByLbl.setStyleName("conversation-comment-username");
Label createdDateLbl = new Label(entity.getManagedMetadata().getAttrValue("createdDate"));
createdDateLbl.setId("conversation.comment.createddate." + repositoryUID);
createdDateLbl.setSizeUndefined();
String text = entity.getDataNode().getAttrValue("text");
Label textLbl = new Label(text, ContentMode.PREFORMATTED);
textLbl.setId("conversation.comment.text." + repositoryUID);
comment.addComponent(createdByLbl);
comment.addComponent(textLbl);
comment.addComponent(createdDateLbl);
comment.setExpandRatio(createdByLbl, 0.2f);
comment.setExpandRatio(textLbl, 0.7f);
comment.setExpandRatio(createdDateLbl, 0.1f);
Note the container is also wrapped by CssLayout (rootLayout), which is full sized.
I want the textLbl as seen above to display as formatted should the user enter text on separate lines themselves and wrap if they have entered a long paragraph comment.
Here is a picturing showing my dilemma.
Any help would be appreciated.
Thanks,

Try with css.
For example:
textLbl.addStyleName("wrapLine");
Css:
.wrapLine{
word-wrap: break-word; /* IE */
white-space: -moz-pre-wrap; /* Firefox */
}

Related

Label doesn't scale down correctly to 0.07f, but if I use font.setUseIntegerPositions(false), it shows both label and font in libGDX

I try to make a label to display "You Win" and add it to winStage, but I cannot manage to display the label correctly on winStage. Some words don't show on the screen, and if I downscale further than 0.044f, the String disappears. when I add font.setUseIntegerPositions(false) to my code, the font shows correct, but the wrong label also shows. Is there a way to get this work? (hide the wrong label or get the label shows correctly), code uses Kotlin, but very similar to java
YUW N is the wrong label I want to remove, the correct "You Win" only shows when I add font.setUseIntegerPositions(false)
here's the relevant code part
private val winViewport = StretchViewport(mainStage.width, mainStage.height)
val winStage = Stage(winViewport, winBatch)
var font = BitmapFont()
var labelStyle = Label.LabelStyle()
labelStyle.font = font
labelStyle.fontColor = Color.RED
var label2 = Label("YOU WIN", labelStyle)
label2.setBounds(0f, winStage.height*2/3, winStage.width*1, winStage.height/6)
label2.setFontScale(0.07F) // 0.07f
font.setUseIntegerPositions(false)
label2.setWrap(false)
winStage.addActor(label2)
winStage.act(delta)
winStage.draw()

how to change colour of label in swt tree editor

I have a TreeEditor and some text in its first column. I need to append some text to it using a label or string. In addition, I want to change the color of the appended string to green. I tried to do it with this code, but it isn't working:
Label label = new Label(this.tree ,SWT.RIGHT);
label.setFont(new Font(getSite().getShell().getDisplay(), ("Hello"), 18,
SWT.BOLD));
label.setText("hello:));
label.pack();
treeItem.setText(1, hi + " " + label.getText());
How could I accomplish this?
you might want to look at org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider

Showing required indicator on GWT's Label

In my GWT app I want to indicate the fields as required. Please advise how do I do that?
If i just add a * in the label text then it is not very well noticeable as it is of the same color as the label text and also aligned to the label rather like a superscript character.
I don't know if applying style sheet can make it work? Any other web 2.0 ideas which do not involve a lot of work?
Currently, it's displayed as
Label fullName = new Label("Full Name*");
The simplest way is
String strReqLabel = "<span style=\"color: #DC143C;\">*</span>"
Label reqLabel = new Label(strReqLabel + " " + "First Name");
Just separate the * from the label, to another label:
Label label = new Label("Full name");
TextBox tb = new TextBox();
Label reqLabel = new Label("*");
reqLabel.setStyleName("red_label");
then on the war/app.css crete a new item
.red_label {
color: red;
font-size: 8pt;
overflow: hidden;
}
Smething like this should work.
Alternatively,
Suffix the label with * mark - so that who are really cautious of filling the form will observe it and do the needful. For those you are not cautious, after submitting the form,
you can think of making the outline in red color. This way, you need not make the form too flashy at initial look.
So I did it as:
public HorizontalPanel createLabel(String name){
HorizontalPanel hPanel = new HorizontalPanel();
hPanel.add(new Label(name));
Label star = new Label("*");
star.setStyleName("req-label");
hPanel.add(star);
return hPanel;
}
and css as:
.req-label{
color: #FF0000;
font-weight: bold;
}

Getting my scrollPane to scroll under programatic control

I have a Groovy app which uses a scrollPane built via swing builder:
BinsicWindow(def controller)
{
controlObject = controller
swinger = new SwingBuilder()
mainFrame = swinger.frame(
title: "Binsic is not Sinclair Instruction Code",
size:[640, 480],
show:true,
defaultCloseOperation: WindowConstants.DISPOSE_ON_CLOSE){
scrollPane(autoscrolls:true) {
screenZX = textArea(rows:24, columns:32) {visble:true}
}
screenZX.setFont(new Font("Monospaced", Font.PLAIN, 18))
}
}
I add text to the textArea programatically (i.e. no user input) and I would like the textArea to scroll down automatically as content is added. But the view remains fixed at the top and I can only see the bottom (once the screen is more than full) by dragging the mouse.
Can I fix this? I have been searching for an answer to this for a wee while now and getting nowhere. Apologies if it's a simple answer.
The following lines should scroll your textarea to the last text position:
rect = screenZX.modelToView(screenZX.getDocument().getLength() - 1);
screenZX.scrollRectToVisible(rect);

Changing the background color of a paragraph in JTextPane (Java Swing)

Is it possible to change the background color of a paragraph in Java Swing? I tried to set it using the setParagraphAttributes method (code below) but doesn't seem to work.
StyledDocument doc = textPanel.getStyledDocument();
Style style = textPanel.addStyle("Hightlight background", null);
StyleConstants.setBackground(style, Color.red);
Style logicalStyle = textPanel.getLogicalStyle();
doc.setParagraphAttributes(textPanel.getSelectionStart(), 1, textPanel.getStyle("Hightlight background"), true);
textPanel.setLogicalStyle(logicalStyle);
UPDATE:
I just found out about a class called Highlighter.I dont think you should be using the setbackground style. Use the DefaultHighlighter class instead.
Highlighter h = textPanel.getHighlighter();
h.addHighlight(1, 10, new DefaultHighlighter.DefaultHighlightPainter(
Color.red));
The first two parameters of the addHighlight method are nothing but the starting index and ending index of the text you want to highlight. You can call this method multiple timesto highlight discontinuous lines of text.
OLD ANSWER:
I have no idea why the setParagraphAttributes method doesnt seem to work. But doing this seems to work.
doc.insertString(0, "Hello World", textPanel.getStyle("Hightlight background"));
Maybe you can work a hack around this for now...
I use:
SimpleAttributeSet background = new SimpleAttributeSet();
StyleConstants.setBackground(background, Color.RED);
Then you can change existing attributes using:
doc.setParagraphAttributes(0, doc.getLength(), background, false);
Or add attributes with text:
doc.insertString(doc.getLength(), "\nEnd of text", background );
Easy way to change the background color of selected text or paragraph.
//choose color from JColorchooser
Color color = colorChooser.getColor();
//starting position of selected Text
int start = textPane.getSelectedStart();
// end position of the selected Text
int end = textPane.getSelectionEnd();
// style document of text pane where we change the background of the text
StyledDocument style = textPane.getStyledDocument();
// this old attribute set of selected Text;
AttributeSet oldSet = style.getCharacterElement(end-1).getAttributes();
// style context for creating new attribute set.
StyleContext sc = StyleContext.getDefaultStyleContext();
// new attribute set with new background color
AttributeSet s = sc.addAttribute(oldSet, StyleConstants.Background, color);
// set the Attribute set in the selected text
style.setCharacterAttributes(start, end- start, s, true);

Categories