I am new to javafx.
I am trying to build a messenger where the chatting panel show messages.
I want to align the messages like my own messages are on left and other messages are on right.
To display the message i am using TextFlow where I add Text. But the alignment isnt working.
TextFLow tf=new TextFlow();
Text t1= new Text("Hi");
Text t2= new Text("Hello");
t1.setTextAlignment(TextAlignment.RIGHT);
t2.setTextAlignment(TextAlignment.LEFT);
tf.getChildren().addAll(t1,t2);
But the alignment not working. Both the text are on left.
What should I do?
You can set the TextAlignement from the TextFlow but, i don't think we could have two alignment in the same container, however, you can use a tricky way to skirt this problem by using Labels & VBox:
private VBox Chat(){
VBox chat = new VBox();
chat.setPrefSize(400, 400);
chat.setStyle("-fx-background-color:#333333;");
Label txt1 = new Label("Text1");
txt1.setTextFill(Color.WHITE);
txt1.setPrefWidth(400);
txt1.setAlignment(Pos.CENTER_LEFT);
Label txt2 = new Label("Text2");
txt2.setTextFill(Color.WHITE);
txt2.setPrefWidth(400);
txt2.setAlignment(Pos.CENTER_RIGHT);
chat.getChildren().addAll(txt1,txt2);
return chat;
}
Why Label ? This node work in the same way as the Text node but the advantage is that wet can resize the background.
Why VBox ? is optional, even if i think TextFlow is better suited to textual nodes. It is also for the positioning, VBox is more suited for classified items from top to bottom chatting panel. Good luck !
Related
I have a couple of buttons and want to collect them in a HBox and set them in a vertical row. The goal is something similar to this formation: http://vandelaydesign.com/images/navi/vertical.gif
Button addButton = new Button("Add Expense");
addButton.setOnAction(new AddExpenseGUI(rootStage, data));
Button editButton = new Button("Edit");
addButton.setOnAction(new EditButtonListener());
I tried multiple things. This was one idea of me, but I just get a horizontal formation:
HBox a = new HBox();
a.getChildren().addALL(addButton, editButton);
grid.add(a,0,0);
Any ideas?
Use a VBox and skin your buttons. See these:
Skinning JavaFX Applications with CSS
Styling FX Buttons with CSS
The H in HBox stands for Horizontal so the Pane you are looking for is VBox (for Vertical)
See:
http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm#CHDGHCDG
I have a FlowPane that I would like to go to a new line every time I add a new child (another entry of text)
Text text1 = new Text("Here is some text");
Text text2 = new Text("This should be on a new line");
flowPane.getChildren().add(text1);
flowPane.getChildren().add(text2); //should go to the next line
This just keeps appending the text next to each other like so:
"Here is some textThis should be on a new line"
Does anyone out there know of a way to fix this? It seems like it should be a straight forward part of the API, but I can't find anything relevant to this problem.
From the api: A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width.. So basically, by default, all your components will be added in the same row until there's not enough space available.
You want to either change the orientation, or use another layout.
I want to have a menu for my program. And I like the standard Menu look and all, but I want to place a "logout-button" on the far right side of the menu-bar.. is it possible to place it there WITHOUT having to fill up the whole menu-bar with entries?
Sincerely
Yes you can. Use the HBox#setHgrow();. This javadoc page also has an example how to use it in "Optional Layout Constraints" section. Following is taken from javadoc.
For example, if an hbox needs the TextField to be allocated all extra space:
HBox hbox = new HBox();
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));
Briefly speaking, set Priority.ALWAYS for the button (or any control) just before the "logout-button" in a HBox. More advanced example is here: Using Built-In Layout Panes : Example 1-4
I want to know how to create a label that contains two icons, one on each side and set it as the title bar for the form element (LWUIT widgets).
Form has a function to get a titleArea then you can put some components what you want.
Form f = new Form();
Container c = f.getTitleArea();
Label iconLabel1 = new Label("leftIcon");//using Image
Label iconLabel2 = new Label("rightIcon");//using Image
c.addComponent(BorderLayout.WEST, iconLabel1);
c.addComponent(BorderLayout.EAST, iconLabel2);
You can just add a component to the north portion of the screen which is the recommended way that will work properly and won't break for newer versions of LWUIT/Codename One.
When you don't set a title it will just work and you can give it the Title UIID. LWUIT 1.5 and newer have a TitleArea container but I suggest you stay away from it since CodenameOne customizes it quite allot for iOS/Android 4.x etc.
Use the setTitleComponent(Label title) method.
EDIT :
Derive the Label class and implement the paint method where you can use the Graphics method to draw Images and texts. Also set the label's text position to Label.CENTER .
I want to place the components A and B over component with List. I need that would text of list will be to visible. I can not find which layout can do it.
How this behavior is in lwuit? What solutions exist?
The question is somewhat unclear, jmunoz answer is correct if you want component's A and B to reside at the bottom of the screen and the list to scroll above. However from the drawing it seems you want an "always on top" effect which you can achieve either via a glass pane (for non-interactive components) or via the LayeredLayout class.
This is actually quite simple using the following:
myForm.setLayout(new LayeredLayout());
myForm.setScrollable(false);
// will occupy the entire area of the form but should be scrollable
myForm.addComponent(componentUnderneath);
Container south = new Container(new BorderLayout());
myForm.addComponent(south);
south.addComponent(BorderLayout.SOUTH, whateverYouWantToPlaceOnTopInTheSouth);
You must do the following:
The Form must not do scroll. Use Form.setScrollable(false). Set the layout of the ´Form´ to BORDER_LAYOUT, myForm.setLayout(new BorderLayout()) . Ok in BorderLayoutyou can put the components in the Form as you want.
Put the Listcomponent in the center of the BorderLayout with myForm.addComponent(BorderLayout.CENTER, List) and the other two elements in the south of the layout using
Container southContainer = new Container();
southContainer.addComponent(A);
southContainer.addComponent(B);
myForm.addComponent(BorderLayout.SOUTH, southContainer)
With this you can get a scrollable Listand two elements always visible.