wrap label text in j2me

为君一笑 提交于 2019-12-10 22:48:58

问题


I've built a list and inserted labels in each cell. For now the text that is too long simply disappear. I'd like to wrap the text so it is entirely visible inside each cell.

Can you please help?

update: issue solved

For those who need an answer, I used LWUIT's HTMLComponent inside a container. The HTMLComponent allows you to use HTML code. That would allow you to format your list the way you want it to be.


Here is more details on the solution.

In Java ME with LWUIT, I used a HTMLComponent to get the precise layout I wanted. The best way for me was to use an HTML Table inside the HTMLComponent. It just behaves like HTML.

String html_code = "";

html_code  = "<table width='100%'>";
html_code += "<tr><td><strong>"+fullname+"</strong></td></tr>";
if (title.length()>0) { html_code += "<tr><td><i>"+title+"</i></td></tr>"; }
if (message.length()>0) { html_code += "<tr><td>"+message+"</td></tr>"; }
if (date.length()>0) { html_code += "<tr><td><i>"+date+"</i></td></tr>"; }
html_code += "</table>";       

HTMLComponent html = new HTMLComponent(null);
html.setBodyText(html_code);

回答1:


Just incase if you are looking for a more "elegant" solution, i found a handy resource online. I am posting here for reference purposes, but HtmlComponent does the job.

    import com.sun.lwuit.Font;

/** A class supporting word wrap for MIDP. */

public class WordWrap {

Font font;
int width;
String txt;
int pos;

/**
* Initializes the WordWrap object with the given Font, the text string
* to be wrapped, and the target width.
*
* @param font: The Font to be used to calculate the character widths.
* @param txt: The text string to be wrapped.
* @param width: The line width.
*/

public WordWrap (Font font, String txt, int width) {

this.font = font;
this.txt = txt;
this.width = width;
}

/**
* returns the next line break position. If no text is left, -1 is returned.
*/

public int next () {

int i = pos;
int len = txt.length ();

if (pos >= len) return -1;

int start = pos;

while (true) {
while (i < len && txt.charAt (i) > ' ')
i++;

int w = font.stringWidth (txt.substring (start, i));
if (pos == start) {
if (w > width) {
while (font.stringWidth (txt.substring (start, --i)) > width)
{ }
pos = i;
break;
}
}

if (w <= width) pos = i;

if (w > width || i >= len || txt.charAt(i) == '\n') break;
i++;
}

return pos >= len ? pos : ++pos;
}

}




import com.sun.lwuit.Button;
import com.sun.lwuit.Component;
import com.sun.lwuit.Container;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.FocusListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.plaf.Style;

/**
 *
 * @author rubycube
 */
public class WrapList extends Container {

    private Button hiddenButton;
    private int id;

    public WrapList(String text, int containerID) {
        id = containerID;
        this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        this.setFocusable(false);
        final Style thisContainerStyle = this.getStyle();
        Border thisContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
        thisContainerStyle.setBorder(thisContainerBorder);

        hiddenButton = new Button(" ");
        hiddenButton.setPreferredSize(new Dimension(1, 1));
        Style style = hiddenButton.getStyle();
        style.setBgTransparency(0, false);
        style.setBorder(Border.createEmpty());

        FocusListener hiddenButtonFL = new FocusListener() {

            public void focusGained(Component cmp) {

                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xff6600);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgColor(0xff9900);
                parentContainerStyle.setBgTransparency(50);
                parentContainer.repaint();
            }

            public void focusLost(Component cmp) {
                WrapList parentContainer = ((WrapList) (cmp.getParent()));
                Border parentContainerBorder = Border.createRoundBorder(20, 20, 0xcccccc);
                Style parentContainerStyle = parentContainer.getStyle();
                parentContainerStyle.setBorder(parentContainerBorder);
                parentContainerStyle.setBgTransparency(0);
                parentContainer.repaint();
            }
        };
        hiddenButton.addFocusListener(hiddenButtonFL);

        Label l = new Label(text);
        l.setSelectedStyle(thisContainerStyle);
        //l.setUnselectedStyle(thisContainerStyle);
        WordWrap ww = new WordWrap(l.getStyle().getFont(), text, (Display.getInstance().getDisplayWidth() - 10));
        int si = 0;
        int ei = 0;
        while (true) {
            int np = ww.next();
            if (np == -1) {
                break;
            } else {
                si = ei;
                ei = np;
            }
            String lineText = text.substring(si, ei);
            Label line = new Label(lineText);
            line.setEndsWith3Points(false);
            this.addComponent(line);
        }
        this.addComponent(hiddenButton);
    }

    public void addActionListener(ActionListener actionlistener) {
        hiddenButton.addActionListener(actionlistener);
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }
}


来源:https://stackoverflow.com/questions/11405422/wrap-label-text-in-j2me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!