How to place a Button in the center of a row? - java-me

I want to place a Button in the center of a row of a Container , the Button is the only component of the Container. How to achieve that ?

Container c = new Container(new FlowLayout(Component.CENTER));
Button b = new Button();
c.addComponent(b);

Just set the BorderLayout to Container. Then add the Button into Container and set the constraints to CENTER.
For example,
Container c = new Container(new BorderLayout());
Button btn = new Button("Sample");
c.addComponent(BorderLayout.CENTER, btn);

Related

JAVAFX - Adding Nodes To TitledPane's Header

I want to add nodes into the TitledPane's header. The only way I found to do it is using setGraphic() method.
The problem of using that way is I cannot place them neatly. Here is what I get:
The Codes
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
The Goal
I want to put the Delete button to the right, that's why I am using Borderpane. So the BorderPane need to fill the entire of the TitledPane's header width minus the space for small triangle
How can it be done inside setGraphic() method? Or are there any better way to achieve it?
To fix your problem try setting the width of the BorderPane to the width of the scene minus the arrow like this.
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
AnchorPane par = new AnchorPane(box);
tpane.setGraphic(par);
VBox vbox = new VBox();
vbox.getChildren().add(tpane);
box.setPrefWidth(scene.getWidth()-30);//You can adjust the number to fit your needs and change scene to whatever the name of your scene is
I hope this helped
Well, it seems that support for javafx users is not as massive as the mainstream framework.
After some tries by the suggestion from #Austin, I found the way to do what I want, and this is also give dynamic sizing.
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
TitledPane tpane = new TitledPane();
tpane.setContent(new Text("Content"));
tpane.setExpanded(false);
Button b = new Button("Delete");
BorderPane box = new BorderPane();
box.setLeft(new Text("1 "));
box.setCenter(new Text("Single Pane"));
box.setRight(b);
tpane.setGraphic(box);
box.prefWidthProperty().bind(scene.widthProperty().subtract(35));
VBox vbox = new VBox();
vbox.getChildren().add(tpane);

How to create an image-button

I create an image-button using below code:
ImageBrush btnBrush1 = new ImageBrush();
btnBrush1.Stretch = Stretch.Uniform;
btnBrush1.ImageSource = new BitmapImage(new Uri("ms-appx:/Images/icon_LogIn.png"));
btnLogIn.Background = btnBrush1;
The problem:
1) When using a mouse hovering the img-button, it turn grey background and icon disappear ( when not hovering the img-button, this image button is visible.
I want this image-button visible when hovering it or pressing it.
Thanks
You need to set Image as Content rather than setting ImageBrush as Background.
Try this
Image img=new Image();
img.Source=new BitmapImage(new Uri("/Images/icon_LogIn.jpg",UriKind.RelativeOrAbsolute));
btnLogIn.Content = img;

How to set tabs position in JavaFX

I have this JavaFX code with tabs. Can you tell me how I can set the position of the tabs panel to be always on the left side of the main stage:
VBox stackedTitledPanes = createStackedTitledPanes();
ScrollPane scroll = makeScrollable(stackedTitledPanes);
TabPane tabPane = new TabPane();
BorderPane mainPane = new BorderPane();
tabPane.setStyle("-fx-font-size: 12pt;"); // Set global size for the font
// Create Tabs
Tab tabA = new Tab();
tabA.setText("Main Component");
tabA.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabA_stack = new StackPane();
tabA_stack.setAlignment(Pos.CENTER);
tabA_stack.getChildren().add(scroll);
tabA.setContent(tabA_stack);
tabPane.getTabs().add(tabA);
Tab tabB = new Tab();
tabB.setText("Second Component");
tabB.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabB_stack = new StackPane();
tabB_stack.setAlignment(Pos.CENTER);
tabB_stack.getChildren().add(new Label("Label#Tab B"));
tabB.setContent(tabB_stack);
tabPane.getTabs().add(tabB);
Tab tabC = new Tab();
tabC.setText("Last Component");
tabC.setStyle("-fx-font-size: 12pt;"); // Set size of the tab name
// Add something in Tab
StackPane tabC_vBox = new StackPane();
tabC_vBox.setAlignment(Pos.CENTER);
tabC_vBox.getChildren().add(new Label("Label#Tab C"));
tabC.setContent(tabC_vBox);
tabPane.getTabs().add(tabC);
mainPane.setCenter(tabPane);
mainPane.setPrefSize(395, 580);
mainPane.setLayoutX(850);
mainPane.setLayoutY(32);
scroll.setPrefSize(395, 580);
scroll.setLayoutX(850);
scroll.setLayoutY(32);
root.getChildren().add(mainPane);
Update
Sounds like you want to change your stackedTitledPane from a VBox to a BorderPane.
So this code:
VBox stackedTitledPanes = new VBox();
becomes:
BorderPane stackedTitledPanes = new BorderPane();
Then when you want to add your nodes, you specify which section you want to put them (I put a label in the center pane as a filler):
stackedTitledPanes.setLeft(mainPane);
stackedTitledPanes.setCenter(new Label("Main Content"));
Old answer
This will put the tabs on the left (I'm assuming this is what you're looking for):
tabPane.setSide(Side.LEFT);

LWUIT Form with multiple Components

I've added multiple components to my LWUIT Form one by one,but the problem is i am not able to display those added components one by one as like i appended in my code,i am able to display date and my image on a single row(side by side)some times title and date on a single row,I am getting the details from Rss File.
How to display those components like i added in my code one by one, but not 2 components in a single row?
thanks....
Here my code:
Label pubDate = new Label(detailNews.getPubDate().substring(0, 16));
Label title=new Label();
title.setText(detailNews.getTitle());
title.startTicker();
pubDate.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL));
Image geImage = detailNews.geImage();
Label icon=new Label(geImage);
form2.addComponent(title);
form2.addComponent(pubDate);
textarea.setText(detailNews.getDescription());
textarea.requestFocus();
form2.addComponent(icon);
form2.addComponent(textarea);
form2.show();
My idea is:
You can create a Container with a BoxLayoutY, and add this TextArea and the icon to the Container. Next, add this Container to the Form. Something like:
Label pubDate = new Label(detailNews.getPubDate().substring(0, 16));
Label title=new Label();
title.setText(detailNews.getTitle());
title.startTicker();
pubDate.getStyle().setFont(Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL));
Image geImage = detailNews.geImage();
Label icon=new Label(geImage);
Container container = new Container(new BoxLayout(BoxLAyout.Y_AXIS));
container.addComponent(title);
container.addComponent(pubDate);
container.addComponent(icon);
container.addComponent(textarea);
form2.addComponent(container);
textarea.setText(detailNews.getDescription());
textarea.requestFocus();
form2.show();

J2ME LWUIT - Error showing outside screen labels on scroll

I'm working on a J2ME application using LWUIT.
I need to show 10 values with a Label each one.
Something like this:
Label1 Value1
Label2 Value2
Label3 Value3
Label4 Value4
............
LabelN ValueN
I'm using 1 Container for each "row" and one big Container for each "row container"
My problem is with the "rows" outside the screen. The last 4 pairs of Label+value are not showing when I use the scroll
I don't know why.
Can someone solve this?
This is my code:
this.setLayout(new BorderLayout());
PromotionMonitorDTO promotionMonitorDTO = Cloud.getPromotionMonitor();
Utils utils = new Utils();
Font f = Font.getBitmapFont("movSmall");
Container cellContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Container rowContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
//FIRST PAIR///////////////////////
String stringValue = utils.calendarToShorString(promotionMonitorDTO.getLastUpdate());
Label valor = new Label(LanguageManager.getText("LastUpdate"));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
valor = new Label(LanguageManager.getText(stringValue));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
cellContainer.addComponent(rowContainer);
rowContainer = new Container(new BoxLayout(BoxLayout.X_AXIS));
//SECOND PAIR///////////////////////
stringValue = String.valueOf(promotionMonitorDTO.getInitialTarget());
valor = new Label(LanguageManager.getText("InitialTarget"));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
valor = new Label(LanguageManager.getText(stringValue));
valor.getStyle().setFont(f);
rowContainer.addComponent(valor);
cellContainer.addComponent(rowContainer);
////////8 MORE PAIRS////////////////////
this.addComponent(BorderLayout.NORTH, cellContainer);
Finally I solved.
I removed the second Container, I changed the Layout of the form to BoxLayout(BoxLayout.Y_AXIS) and I added all the "row containers" to the form,
With those changes, I have the same Graphic funcionality and the scroll is working.
I have to remove the question? o leave it for others?
Maybe you must "freeze" the Form and make it not scrollable. Set the Form not scrollable using Form.setScrollable(false) and make the center/north Container of your BorderLayoutscrollable. Try to do that.

Resources