How to align RIGHT the content of TextArea in LWUIT? - java-me

I want to align the text in a TextArea to the right. I tried the following code:
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(RIGHT);
form.addComponent(textArea);
The result was just moving the scroll to left,
But the text is still not aligned RIGHT,
check the image below:
So how to align the content to the RIGHT ?

It may sound crazy for the first instance :) but setting the alignment to TextArea.LEFT solved the issue and now it's RIGHT aligned !
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setRTL(true);
textArea.setAlignment(TextArea.LEFT);
form.addComponent(textArea);
Setting it to LEFT makes the displayed text RIGHT aligned !
Or by removing the textArea.setRTL(true) which is mirroring the display
Form form = new Form();
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setAlignment(TextArea.RIGHT);
form.addComponent(textArea);
For those who are interested in more complicated details when it's set to RTL:
the paint method of TextArea class is
public void paint(Graphics g) {
UIManager.getInstance().getLookAndFeel().drawTextArea(g, this);
}
And drawTextArea method in DefaultLookAndFeel is as follows:
int align = ta.getAbsoluteAlignment();
// remaining code is here in initial source
switch(align) {
case Component.RIGHT:
x = ta.getX() + ta.getWidth() - rightPadding - f.stringWidth(displayText);
break;
// remaining code is here in initial source
}
g.drawString(displayText, x, y);
Unfortunately TextArea.RIGHT value is 3
But when calling ta.getAbsoluteAlignment() it returns 1 (despite that the object's alignment is set by code to TextArea.RIGHT !!)
Meanwhile TextArea.Left value is 1
That's why it matched the value in the switch and was aligned to RIGHT
BTW, if you set
textArea.setAlignment(Component.RIGHT);
it will also be wrong, because Component.RIGHT outside the paint method has the value 3 not 1 !

You only have to write 'TextArea.RIGHT' instead of 'RIGHT'
textArea.setAlignment(TextArea.RIGHT);

You can use the following line:
TextArea textArea = new TextArea("Some Arabic text ...");
textArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Related

How to set Dialog text position from code?

Good day all,
I have a simple Dialog started after click button, I post my code:
Dialog dialog;
super();
dialog = new Dialog("Dialog example");
dialog.addText(strFmt("Text to show"));
dialog.addText(strfmt("SecondText to show"));
dialog.run();
I will show a Dialog window loollike this :
It's possible to set the position from code the Text: Text to show ?
For example, if I want to centered position the second text how should I do?
I tried to put blanks in the code:
dialog.addText(strfmt(" Text to show"));
But nothing changes, and this I think not good method.
I saw any suggestions on Web but or I do not use well or is not suitable for me: Example-suggestions.
Exist a method to do what I want?
Thanks for help,
enjoy!
You can center the text using the form control:
Dialog dialog = new Dialog("Dialog example");
DialogText t1 = dialog.addText(strFmt("Text to show"));
DialogText t2 = dialog.addText(strfmt("SecondText to show"));
FormStaticTextControl c1 = t1.control();
c1.widthMode(FormWidth::ColumnWidth);
c1.alignment(FormAlignment::Center);
dialog.run();
The first control is now centered (to the surrounding group).
You have to give it ColumnWidth, otherwise the control would have the minimum size and the centering would have no effect.

How can I add button to top right corner of a Dialog In libgdx?

I want to add close button to top right corner of a dialog box.
I tried using setbounds with addactor and just add and setposition with setsize and addactor, but nothing works. I know that dialog works with table layout, it has a table for content and for buttons. I don't want to use this layout and put the button outside this layout like on the border of the dialog.
How can I do it?
This is how it should be:
The easiest solution I could come up with now, is to use negative padding for your button to move it "outside" of it's cell.
Button closeButton = new TextButton("X", skin, "default");
getTitleTable().add(closeButton).size(60, 40).padRight(-30).padTop(-20);
With this padding hack you have the problem, that the button will be outside of your Dialog, and by default, Window checks the bounds of your window when it performs Actor.hit(...) evaluation.
We need to disable clipping for that reason, but the rendering of the window depends on it. That's why we use another hack to enable it, just for the rendering:
#Override
public void draw(Batch batch, float parentAlpha) {
setClip(true);
super.draw(batch, parentAlpha);
setClip(false);
}
Do this:
private Stage stage;
private Window window;
private Table table;
#Override
public void show() {
table = new Table();
table.setSize(Gdx.graphics.getWidth() / 2
, Gdx.graphics.getHeight() / 5);
window = new Window("", skin);
window.setSize(table.getWidth(), table.getHeight());
Button btnWindow = new Button(skin, "close");
btnWindow.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
window.setVisible(false);
}
});
window.addActor(btnWindow);
btnWindow.setSize(50, 50);
btnWindow.setPosition(window.getWidth() - btnWindow.getWidth()
, window.getHeight() - btnWindow.getHeight());
table.addActor(window);
window.setModal(true);
table.setPosition(Gdx.graphics.getWidth() / 2 - window.getWidth() / 2
, Gdx.graphics.getHeight() / 2 - window.getHeight() / 2 +
100);
window.addAction(Actions.sequence(Actions.alpha(0)
, Actions.fadeIn(.1f)
, Actions.moveTo(+50, +50, 1)));
stage.addActor(table);
}
I had a similar problem. After a bit of searching this thread helped me.
Basically to tell the alignment of the actors inside a table, and to tell the alignment of the table itself are two separate things. Setting the alignment of the table top top-left would produce the desired behavior.
table = new Table();
table.setFillParent(true);
table.setSkin(usedSkin);
table.setDebug(true);
table.top().left();
stage.addActor(table);
table.add(exitBtn);

Align textView next to another one programatically

I am programmatically creating a text view and trying to place another textView right next to the first one. But I am unable to do it.
Here is the code I have written,
//1st textview
TextView itemText = new TextView(context);
itemText.setText(mItemText);
Typeface itemFont = Typeface.createFromAsset(context.getAssets(), "fonts/" + "Roboto" + ".ttf");
itemText.setTypeface(itemFont,Typeface.BOLD);
itemText.setPadding(0, padding, 0, 0);
itemText.setId(10);
RelativeLayout.LayoutParams itemTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
itemText.setTextSize(font_size);
itemText.setLayoutParams(itemTextParams);
//2nd text view
TextView seperator = new TextView(context);
seperator.setText(mSeperator);
seperator.setPadding(0,padding,0,0);
seperator.setTypeface(null,Typeface.BOLD);
RelativeLayout.LayoutParams seperatorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
seperatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId());
seperatorParams.addRule(RelativeLayout.CENTER_VERTICAL);
seperator.setLayoutParams(seperatorParams);
seperatorParams.addRule(RelativeLayout.CENTER_HORIZONTAL) works, but when I use seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId()) , the text is not shown.
Can anyone point out where I am going wrong? Or is there any other way to do this?
The width of itemTextParams is set to MATCH_PARENT, so there's no space to put anything to the right of it. Change it to WRAP_CONTENT or define a width value.

Eclipse rcp layout issue

I am facing an issue with editor and its contents layout. Its following: there is an editor with print preview functionality, main part of the editor is taken by paper clips PrintPreview component, on the left side there is a composite with some buttons (see screenshot 1).
As you can see PrintPreview is located inside ScrolledComposite.
The problem is: when I am resizing window or views under editor, the bottom scrollbar moves under views and thus disappears. There is no way to scroll horizontally afterwards (no editor scrollbar appears). see screenshot2 (bottom scrollbar is getting hidden).
It starting to get hidden, when editor height is less then height of the right composite ( there are more widgets below the last button, I've just erased them)
If I am shrinking the window, then right composite never loses its width and always visible (see screenshot 3 - It does not allow to resize more than that).
The composites are created in the following way:
#Override
public void createPartControl(final Composite parent) {
GridLayout gridLayout = new GridLayout(2, false);
parent.setLayout(gridLayout);
// Scroll area for the print area
scroll = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
final GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.widthHint = 1;
gridData.heightHint = 1;
scroll.setLayoutData(gridData);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
// Button area
final Composite buttonArea = new Composite(parent, SWT.FLAT);
final GridData layoutData = new GridData(SWT.FILL, SWT.TOP, false, false);
buttonArea.setLayoutData(layoutData);
gridLayout = new GridLayout(1, false);
buttonArea.setLayout(gridLayout);
The question is: how not to prevent disappearing of the bottom scrollbar? Can I have the same behavior as width the width of the right composite - It not possible to resize less then right composite height? Or how can I show scrollbar on the whole editor if some minimum height limit is exceeded?
I've tried different things on different composites, like setting minimumHeight and heightHint, putting buttonsArea into the scrolledcomposite (just for fun) etc. But nothing have helped me.
Does anybody knows fast solution? Thank you.
See this SO post: Prevent SWT scrolledComposite from eating part of it's children
You need to listen for width changes on the content composite
(mParent), compute the minimum height again given the new content
width, and call setMinHeight() on the scrolled composite with new
height.
If the above post is not as useful as it seems then share some minimal compiling and executable code to replicate the problem. Also, mention the eclipse/swt version, OS and JDK version, it helps in analyzing the problem.
Demo code for resize event and output !!
Code :
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class ScrolledCompositeTest
{
public static void main (String [] args)
{
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
GridLayout layout = new GridLayout();
layout.numColumns = 4;
// set the minimum width and height of the scrolled content - method 2
final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
sc2.setExpandHorizontal(true);
sc2.setExpandVertical(true);
final Composite c2 = new Composite(sc2, SWT.NONE);
sc2.setContent(c2);
layout = new GridLayout();
layout.numColumns = 7;
c2.setLayout(layout);
Button b2 = new Button (c2, SWT.PUSH);
b2.setText("first button");
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Button add = new Button (shell, SWT.PUSH);
add.setText("add children");
final int[] index = new int[]{0};
add.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
index[0]++;
Button button = new Button(c2, SWT.PUSH);
button.setText("button "+index[0]);
sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));
c2.layout();
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
Output:
Actual Size
Vertical Resize
Horizontal Resize

LWUIT - show TextField blinking Cursor, even if the field is empty

I have a TextField in a Form. This TextField should have focus by default,
which works fine. Now I'd like the user to be aware of that and show him,
that he's inside the TextField - so the TextField cursor should be shown
and blink.
I only found drawTextFieldCursor in DefaultLookAndFeel. but I have
absolutely no idea how to apply this to my TextField.
Any help - and code would be appreciated!
Here's a sample. I still don't have it working.
public void search2() {
searchForm = new Form();
TextField searchArea = new TextField();
searchForm.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
searchForm.addComponent(searchArea);
searchArea.requestFocus();
int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
searchArea.keyPressed(i);
searchArea.keyReleased(i);
searchForm.show();
}
What happens is: the TextField is focused, it can be directly edited, the "Abc" mode is shown, but what I really want is to show the user the cursor, so he KNOWS he's inside the TextField. This is not happening... if someone could show me some working code for that...
You want the text field to be in editing mode not for the text field cursor to show (which happens when editing). Use requestFocus() to make sure the text field has focus then use something like:
int i = Display.getInstance().getKeyCode(Display.GAME_FIRE);
tf.keyPressed(i);
tf.keyReleased(i);
The drawTextFieldCursor method has a Graphics parameter , so the way you can draw your cursor is :
derive TextField
in its public void paint(Graphics g) you call drawTextFieldCursor after setting the drawing methods of the TextField's paint Graphic.

Resources