How to make the content of a SmartGWT label not trim spaces? - java

Pretty self-explanatory. Whenever I set the contents of a label, any spaces at the beginning or end are removed. I've looked at the API and found little to help me. setExtraSpaces() will add spacing to the top and bottom, which I don't want. Something like setLeft doesn't seem to add any actual space -- perhaps some other layout is conflicting to cause that not to happen but even then it seems like a bit of a work around. So, basically, I just want to know if there's any way to make the content exactly what I put in?

From the JavaDoc:
setContents
public void setContents(String contents)
The contents of a canvas or label widget. **Any HTML string is acceptable**.

Related

How do I wrap text in a jTextPane?

I'm currently using a jTextArea, and that has a setLineWrap() method to indicate whether text should be wrapped or not. Do jTextPanes have an equivalent to this? I know it can wrap words, but I need it to wrap characters.
For example, "oooooooooooo..." should be wrapped and displayed as two or more lines of o's instead of one long line with a scroll bar. So is this possible with jTextPanes?
JTextPanes word wrap automatically, you have to set the size.
jTextPane.setSize(someValue);
The size should be set to something small.
But this doesn't help you because it doesn't do that with characters... Try using <br> maybe? Or check this link: http://java-sl.com/wrap.html

Vaadin 10 Grid add custom image icon

I am using a Vaadin 10 (Flow) Grid to stream data to new rows, and I am trying to add a custom image icon to my TemplateRenderer.
I have a TemplateRenderer setup like this:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which displays my numbers correctly in the grid cell like this:
I would like to have my image rendered inside the same cell, to the left of the numbers.
This was my (very crude) attempt to import it using html:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\"><img src=\"i.imgur.com/3LQBglR.png\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which give's me this:
I think that I might have to wrap the image and numbers into a HorizontalLayout with the image being a separate component - I think I could handle that, but I am unsure of how to do the actual rendering of the image. I can probably accomplish it simply with the internal Vaadin Icons set, but I need to use my own little images.. all icons that I am going to use will be at or less than 25 x 25px.
Thank you so much in advance!
I think you're on the right track, but there's one small detail that causes you trouble. The URL i.imgur.com/3LQBglR.png doesn't define a protocol, which means that the entire string will be treated as a path relative to the location of the hosting page. It will thus try to find a file in a directory named i.imgur.com on your own server.
To fix this, you need to also include the protocol in the image URL, i.e. https://i.imgur.com/3LQBglR.png.
I can also offer a small suggestion for how to make the code easier to read. HTML also supports using ' for enclosing attribute values. This is more convenient to use from Java string since you don't need to use \ to escape ' characters. Your template string could thus be "<div class$='[[item.class]]'><img src='https://i.imgur.com/3LQBglR.png'>[[item.size]]</div>".

Swing Component with automatic line wraping and limited width with HTML support

I want to implement a small tooltip with a scrollbar like the one in eclipse that appears when hovering above a class or member.
The problem I have is finding a way to limit only the width but not the height of a component within the scroll pane I have inside my tooltip. The component should support HTML and also wrap the text correctly when it exceeds the width of the inner bounds, but all components I have tried out have either line wrapping or HTML rendering, but not both
A way to limit only width is also nowhere to be found as every "setXSize" where X is "preferred" "max" "min" etc. all require two arguments and there is no "setMaxWidth" method for components.
Setting "setMaximumSize(new Dimension(256, Integer.MAX_VALUE);" would seem like a solution but it doesnt work as parameters set by "max" and "min" are ignored most of the time which is quite frustrating.
On request a current example of the implementation:
public class MyTooltip extends JTooltip{
private JScrollPane scroll;
private JEditorPane pane;
public MyTooltip(String htmlCode){
this.pane = new JEditorPane();
this.scroll = new JScrollPane(this.pane);
this.pane.setEditable(false);
this.pane.setContentType("text/html");
this.scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//Here the problems begin
this.pane.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.scroll.setMaximumSize(new Dimension(512, Integer.MAX_VALUE));
this.pane.setText(htmlCode);
this.add(scroll);
}
}
the actual code is a bit more complex ofc. but I think this is a good approximation ...
Have you tried JTextPane with HTMLEditorKit (content type text/html)?
I think that's what you need.
Ok, the whole problem just solved itself: One had the idea to let the user write his own texts to be displayed on the tooltips, but that included letting him use multiple "spaces" for indentation when he called for it.
To let HTML render this as intended we replaced every "space" with an & nbsp; so no optimization on gaps between characters would be performed, but this of course has the result that no algorithm for automatic line wrapping would accept any "gap" between words as a suitable place to break the line.
So our implementation actually works as intended, only the Strings we give the tooltip to display are not suitable to be line broken.

How to align text appended to a JScrollPane

I think the screenshot is worth a couple of paragraphs
The best way I can think of doing it is to check for the length of the string and depending give/take \t, but it just seems inelegant. Is there a simpler/better way?
I would use a JTable for this. You can put one in a JScrollPane. This will get you aligned columns automatically. Plus you will get column headers which will aid in understanding what the data means.
If you want to stick with plain text and have an upper bound on the length of fields (which you'd need to guarantee alignment), then you could use printf.

JList: Horizontal_WRAP fitted?

Hey,.. i wanna show pictures with names onit in a jList, i know it get's also in a JPanel but i'm now using a jList, doesn't matter..
My question is why does the jlist don't fit the images only in 2 horizontal 'cells' and then go one row down?
sry my english is bad and i don't know how to describe it better, but look on the picture, why does the jlist dont set the e.g. 3rd picture right next to the 2nd?
JList.HORIZONTAL_WRAP works correctly in the ListDialog JWS demo, as described in Initializing a List. I suspect a layout problem, but you might compare your code to the examples found there.
If you use HORIZONTAL_WRAP you can adjust the number of columns with setVisibleRowCount. If you want them automatically fitted to the width of the list, use 0 or something negative.

Categories