java-melwuitlwuit-textarea

How to place the TextArea text in its center?


I have a Dialog and a TextArea. The TextArea component has an alignment set to Component.CENTER. I created a method named affiche() which displays the Dialog :

public class Alert extends Dialog {
    private TextArea chp;
    private Command[] comms;
    public Alert(String text, Command[] comms)
    {
        super();
        this.comms = comms;
        setAutoDispose(true);
        for (int c=0; c<comms.length; c++)
            addCommand(comms[c]);
        chp = new TextArea(text);
        chp.setAlignment(Component.CENTER);
        chp.setEditable(false);
        chp.getSelectedStyle().setBorder(null);
        chp.getUnselectedStyle().setBorder(null);
        chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
        chp.setRowsGap(3);
    }
    public Command affiche()
    {
        return show(null, chp, comms);
    }
}

My problem is that the text is displayed at the top of the TextArea when calling the affiche() method from another Form.

So how to display the text in the center of the TextArea ? I mean by "center" the center in the width and the center in the hight. I already set the horizontal alignement to center by the code chp.setAlignment(Component.CENTER); , so I want to know how to set the vertical alignement ?


Solution

  • I found the solution without using the DefaultLookAndFeel class. Here it is :

    public class Alert extends Dialog {
        private TextArea chp;
        private Command[] comms;
        public Alert(String text, Command[] comms)
        {
            super();
            this.comms = comms;
            setAutoDispose(true);
            for (int c=0; c<comms.length; c++)
                addCommand(comms[c]);
    
            chp = new TextArea();
            chp.setAlignment(Component.CENTER);
            chp.setEditable(false);
            chp.getSelectedStyle().setBorder(null);
            chp.getUnselectedStyle().setBorder(null);
            chp.getSelectedStyle().setBgColor(this.getStyle().getBgColor());
            chp.getUnselectedStyle().setBgColor(this.getStyle().getBgColor());
            while (text.substring(0, (text.length()/2)+1).length() < chp.getMaxSize()/2)
            {
               text = " ".concat(text);
            }
            chp.setText(text);
        }
        public Command affiche()
        {
            return show(null, chp, comms);
        }
    }
    

    So I just added the while code. And it works !