I have guidance text in my app, which I put it in the label and is child of scrollpane. But the text is too long and I cannot by the scroll bar to get all text in the label. Any idea how to set the measure of scrollbar of scrollpane to get whole text in the label.
I have used JavaFX Scene Builder.
I think this is for sample code in text wrapping using scroll pane.Just refer this.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package comboboxeditable;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
*
* #author reegan
*/
public class TextWrapping extends Application {
Node sub;
#Override
public void start(Stage primaryStage) {
ScrollPane root = new ScrollPane();
Scene scene = new Scene(root, 300, 250);
Text text = new Text("The look and feel of JavaFX applications "
+ "can be customized. Cascading Style Sheets (CSS) separate "
+ "appearance and style from implementation so that developers can "
+ "concentrate on coding. Graphic designers can easily "
+ "customize the appearance and style of the application "
+ "through the CSS. If you have a web design background,"
+ " or if you would like to separate the user interface (UI) "
+ "and the back-end logic, then you can develop the presentation"
+ " aspects of the UI in the FXML scripting language and use Java "
+ "code for the application logic. If you prefer to design UIs "
+ "without writing code, then use JavaFX Scene Builder. As you design the UI, "
+ "Scene Builder creates FXML markup that can be ported to an Integrated Development "
+ "Environment (IDE) so that developers can add the business logic.");
text.wrappingWidthProperty().bind(scene.widthProperty());
root.setFitToWidth(true);
root.setContent(text);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Related
Can anyone tell me why sometimes JavaFX displays the content of a TextField with a blur effect on it? It seems to be random and occurs in any of my TextFields. Please see the image attached.
Focusing on the intermittent rendering artifact mentioned here, the 2 glyph looks like it's been rendered twice, with one copy shifted horizontally relative to the other. Such apparently random anomalies are notoriously difficult to identify. Myriad causes may include incorrect synchronization, improper layout, defects in the host platform's rendering pipeline, etc. For reference, the example below may allow you to test on disparate platforms.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* #see https://stackoverflow.com/a/53989899/230513
*/
public class TextFieldTest extends Application {
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TextFieldTest");
BorderPane root = new BorderPane();
root.setCenter(createContent());
root.setBottom(createVersion());
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
private Node createContent() {
HBox row1 = new HBox(4);
Label channelsLabel = new Label("Channels:");
TextField channelsText = new TextField("2");
channelsText.setPrefWidth(32);
Label separatorLabel = new Label("Separator:");
TextField separatorText = new TextField("!");
separatorText.setPrefWidth(32);
row1.setPadding(new Insets(8));
row1.getChildren().addAll(
channelsLabel, channelsText, separatorLabel, separatorText);
HBox row2 = new HBox(4, new Label("Label:"), new TextField());
row2.setPadding(new Insets(8));
return new VBox(row1, row2);
}
private Label createVersion() {
Label label = new Label(
System.getProperty("os.name") + " v"
+ System.getProperty("os.version") + "; Java v"
+ System.getProperty("java.version"));
label.setPadding(new Insets(8));
return label;
}
public static void main(String[] args) {
launch(args);
}
}
As shown in the Modena example, an intentional blur effect indicates that the text field is focused:
The detail that gives rise to the blurred effect in your image is a compound border, seen below at 2x:
Comparable effects are seen here for buttons (top row) and default buttons (bottom row):
I am using NetBeans IDE 8.2 for JavaFX. I already know that in order to change the position of a button I need to use setLayoutX/Y. I have tried this, and there is no effect on the buttons. Here is my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication2;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author coolDawg1234
*/
public class JavaFXApplication2 extends Application {
#Override
public void start(Stage primaryStage) {
String x = "1";
String y = "0";
Button btn1 = new Button(x);
btn1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.print(x);
}
});
Button btn2 = new Button(y);
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.print(y);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn1);
btn1.setLayoutX(250);
btn1.setLayoutY(220);
root.getChildren().add(btn2);
btn1.setLayoutX(200);
btn1.setLayoutY(200);
Scene scene = new Scene(root, 1000, 1000);
primaryStage.setTitle("WHAT\'s GOOD MY MANS");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Netbeans gives me 0 errors for this, and everything other than the position of the buttons looks fine to me.
Please help me find my problem.
The container for your buttons is a StackPane. StackPane is an implementation of Pane such that it will, by default, layout its children at the center of itself. Therefore, whenever the scene needs to perform a layout, StackPane will set the layoutX and layoutY values (therefore overwriting whatever you had set) in its layoutChildren() method based on its own layout strategy. This behavior happens for most, if not all, subclasses of Pane.
If you need to manually position your child nodes, you need to use the generic Pane container. You can either choose to subclass it and provide your own layout strategy/logic, or simply set layoutX and layoutY values on the child nodes directly.
If you need the layout strategy provided by StackPane, but you would want it to be positioned slightly different from the default position, then you may be looking for translateXProperty() and translateYProperty().
30 root.getChildren().add(btn1);
31 btn1.setLayoutX(250);
32 btn1.setLayoutY(220);
33 root.getChildren().add(btn2);
34 btn1.setLayoutX(200);
35 btn1.setLayoutY(200);
Just take the right variable (consistent) btn1 gets two coordinates for the same direktion (250 for X in row 31, and 200 for X in row 34), so that button which has no directly set coordinates, has in this layout the coordinates (0,0).
I'm new to Vaadin and am still learning. Here I am trying to get a basic Vaadin project to compile. I want it to and display a modal window when the UI runs, but am having trouble. Here is what I have so far:
CaptchaUI.java -
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;
public abstract class CaptchaUI extends UI {
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
protected void init(VaadinRequest request) {
addWindow(new CaptchaWindow());
}
}
CaptchaWindow.java -
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
public class CaptchaWindow extends Window {
/**
*
*/
private static final long serialVersionUID = 1L;
public CaptchaWindow() {
// Some other UI content
setContent(new Label("Here's my UI"));
// Create a sub-window and set the content
Window subWindow = new Window("Sub Window");
VerticalLayout subContent = new VerticalLayout();
subContent.setMargin(true);
subWindow.setContent(subContent);
// Put some components in it
subContent.addComponent(new Label("Label"));
subContent.addComponent(new Button("Button"));
// Center it in the browser window
subWindow.center();
// Open it in the UI
addWindow(subWindow);
}
}
Could someone give me some help or recommendation to get it to display?
Thanks so much.
According to Vaadin docs it is just as easy as setting
setModal(true)
on the sub window to make it modal.
Please note that the modal feature of Vaadin is just a client side restriction. Modifying the HTML with debug tools in the browser could still make it possible to click buttons in the background.
In my application I want to have the same menu item appear on both the system menu as well as a context menu. For example, the Copy menu item. In the context menu I also like to show the accelerator key (the same as on the system menu).
Here's where the problem occurs: if you set the accelerator on both menus, both menu items get fired. This is, of course, not what I want…
Is there a way to prevent this? My current work-around is to just not show the accelerators for the context menu, but this isn't really what I want either.
So is there any way of having the accelerator show in the menu but not fire?
Sample code with a context menu and system menu.
If you press CMD/CTRL-C it will fire both handlers :-(
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test_menu;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author Hayo Baan
*/
public class Test_Menu extends Application {
#Override
public void start(Stage primaryStage) {
// Event handler
EventHandler<ActionEvent> eventHandler = (ActionEvent event) -> {
System.out.println("Got event from " + ((MenuItem) event.getSource()).getText());
event.consume();
};
// The system menubar
MenuBar menuBar = new MenuBar();
menuBar.setUseSystemMenuBar(true);
// Edit menu with Copy item
Menu editMenu = new Menu("Edit");
menuBar.getMenus().add(editMenu);
MenuItem editCopy = new MenuItem("Edit Copy");
editCopy.setAccelerator(KeyCombination.keyCombination("Shortcut+C"));
editMenu.getItems().add(editCopy);
editCopy.setOnAction(eventHandler);
// Context menu with copy item
ContextMenu contextMenu = new ContextMenu();
MenuItem contextCopy = new MenuItem("Context Copy");
contextCopy.setAccelerator(KeyCombination.keyCombination("Shortcut+C"));
contextMenu.getItems().add(contextCopy);
contextCopy.setOnAction(eventHandler);
Label label = new Label("Say 'Hello World'");
VBox root = new VBox();
root.getChildren().addAll(menuBar, label);
label.setContextMenu(contextMenu);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
What you have is two instances of EventHandlers that react on the same key combination. You can circumvent your issue by defining the EventHandler once, use if for both menu types and consume the event:
EventHandler evtHandler = event -> {
System.out.println("Execute copy event");
event.consume();
};
...
editCopy.setOnAction(evtHandler);
...
contextCopy.setOnAction(evtHandler);
I created a Label in JavaFX which has a contains a lot of text.
Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
+ "software to find both the C-Mark and monthly attendance "
+ "of students. Inorder to use the features of this software,"
+ " user has to create an account for him first. Then he should "
+ "login using the username and password. He will be able to "
+ "perform all the operations then. Further details are mentioned"
+ " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);
In this code setWrapText(true) is not working. Why? How can I make it work?
Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
+ "software to find both the C-Mark and monthly attendance "
+ "of students. Inorder to use the features of this software,"
+ " user has to create an account for him first. Then he should "
+ "login using the username and password. He will be able to "
+ "perform all the operations then. Further details are mentioned"
+ " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);
Here is an SSCCE:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.text.TextAlignment;
import javafx.stage.Stage;
public class WrappedLabelExample extends Application {
#Override
public void start(Stage primaryStage) {
Label l1 = new Label("\t\tC-Mark and Attendance Calculator is a simple "
+ "software to find both the C-Mark and monthly attendance "
+ "of students. Inorder to use the features of this software,"
+ " user has to create an account for him first. Then he should "
+ "login using the username and password. He will be able to "
+ "perform all the operations then. Further details are mentioned"
+ " in the 'HELP' section in the user home page.");
l1.setWrapText(true);
l1.setTextAlignment(TextAlignment.JUSTIFY);
StackPane root = new StackPane(l1);
root.setPadding(new Insets(10));
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
which results in
I encountered the same problem in Scene Builder and the other solutions didn't work.
I finally got it working by setting the "Min Height" to USE_PREF_SIZE and keeping the "Pref Height" at USE_COMPUTED_SIZE.
This gets translated to FXML with the minHeight property set to -Infinity.
While the above solutions can work, one particular problem I ran into, is that if a Label is placed inside of a VBox, it will not wrap despite having the Labels max width set and wrapText is set to true. In this case, the Label must ALSO have its pref and minimum height set in order to begin wrapping, which can be annoying if you have multiple labels in the VBox and are trying to get them to work.
private VBox myCode() {
VBox container = new VBox();
int maxWidth = 200;
container.setMaxWidth(maxWidth);
Label one = new Label("This is very long");
one.setMaxWidth(maxWidth);
one.setPrefWidth(maxWidth);
one.setWrapText(true);
// Tailored to the text
one.setMinHeight(100);
one.setPrefHeight(100);
Label two = new Label("This is also very long");
two.setMaxWidth(maxWidth);
two.setPrefWidth(maxWidth);
two.setWrapText(true);
// Tailored to the text
two.setMinHeight(200);
two.setPrefHeight(200);
container.getChildren().addAll(one, two);
return container;
}
To avoid annoyances involved with this, you use a TextFlow object instead of a label, and place one Text object into the TextFlow as it will wrap the text without needing to worry about what it's height should be.
private static TextFlow aLabel(String... pString) {
TextFlow textFlowLabel = new TextFlow();
textFlowLabel.setMaxWidth(MAX_WIDTH);
textFlowLabel.setPrefWidth(MAX_WIDTH);
textFlowLabel.setTextAlignment(TextAlignment.JUSTIFY);
for (String aString : pString) {
textFlowLabel.getChildren().add(new Text(aString));
}
return textFlowLabel;
}
private VBox myCode() {
VBox container = new VBox();
int maxWidth = 200;
container.setMaxWidth(maxWidth);
TextFlow labelOne = aLabel("This is very long");
TextFlow labelTwo = aLabel("This is also very long");
container.getChildren().addAll(labelOne, labelTwo);
return container;
}