Multi-line text in SWT / WindowBuilder?

Is there a way to make the text of the SWT button span two lines, either manually in the SWT code, or using the WindowBuilder GUI? I have it: alt text

I want something like this (digitally changing the image to achieve this): alt text

Is it possible? How?

+3
source share
6 answers

The only way I can do this is through JNI / JNA. Please note: if you take this road, you will violate the independence of the Java platform by invoking your own platform-specific functions.

For Windows, take a look at the SetWindowText API call . Suppose you have code similar to:

Button btn = new Button(shell, SWT.PUSH);
btn.setText("Hello world!");

btn.handle. JNA- - :

final User32 lib = (User32) Native.loadLibrary("user32", User32.class);
final HWND hWnd = new W32API.HWND(new W32API.UINT_PTR(btn.handle)
        .toPointer());
final String text = "Hello\nworld!"; // or "Hello\r\nworld!"?
lib.SetWindowText(hWnd, text.toCharArray());

( java.lang.UnsatisfiedLinkError SetWindowText(), ), .

0

, SWT.WRAP "\n" , .

....
Button check = new Button(composite, SWT.WRAP | SWT.PUSH );
check.setText("Multi\n Line\n Button \n Text");
....

, java- button.setText(..) , , , SWT.WRAP ('\n' , SWT.WRAP ).

, .

enter image description here

+6

, , , . , 2D-. . , , , , , JNI.

+4

:

final int style = OS.GetWindowLong(button.handle, OS.GWL_STYLE);
OS.SetWindowLong(button.handle, OS.GWL_STYLE, style | OS.BS_MULTILINE);
button.setText("line 1\nline 2");

org.eclipse.swt.internal.win32.OS . , SWT API, . Windows API, , SWT.

, computeSize() .


: , computeSize() GWL

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.internal.win32.OS;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;


public class MultilineButton extends Button {

    public MultilineButton(Composite parent, int style) {
        super(parent, style);
        final int gwlStyle = OS.GetWindowLong(this.handle, OS.GWL_STYLE);
        OS.SetWindowLong(this.handle, OS.GWL_STYLE, gwlStyle | OS.BS_MULTILINE);
    }

    @Override
    protected void checkSubclass() {
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        final Point size = super.computeSize(wHint, hHint, changed);
        final GC gc = new GC(this);

        final String multiLineText = this.getText();
        final Point multiLineTextSize = gc.textExtent(multiLineText, SWT.DRAW_DELIMITER);

        final String flatText = multiLineText.replace('\n', ' ');
        final Point flatTextSize = gc.textExtent(flatText);

        gc.dispose();

        size.x -= flatTextSize.x - multiLineTextSize.x;
        size.y += multiLineTextSize.y - flatTextSize.y;

        return size;
    }
}

enter image description here

+4

'\n', , .

+1

, SWT. , , , , .

button.setText("<html>My<br>Text<br>");

swing HTML.

, , , , , , . .

+1

Source: https://habr.com/ru/post/1782457/


All Articles