Html source in javafx HTMLEditor - java

Is there a way to use javaFx HTMLEditor in source mode, like when you select show source code in firefox?
I need this because I want to load strings which include xml tags in the editor and the wigdet is not showing them.

Not HTMLEditor nor Webengine or Webview contains a method like your needs. The only thing I've found is something to display the html in a different textarea. Maybe this will help you.
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventType;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.HTMLEditor;
import javafx.stage.Stage;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
HTMLEditor editor = new HTMLEditor();
editor.setHtmlText("<html>"
+ "<head>"
+ "<title>A Test</title>"
+ "</head>"
+ "<body>This is just a Test</body>"
+ "</html>");
TextArea area = new TextArea();
area.setText(editor.getHtmlText());
editor.addEventHandler(EventType.ROOT, (Event event) -> {
area.setText(editor.getHtmlText());
});
VBox root = new VBox();
VBox.setVgrow(area, Priority.ALWAYS);
root.getChildren().addAll(editor, area);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Related

Stage styling in javafx

How to apply StageStyle.utility and StageStyle.undecorated to the same stage.
I am using this for the internal window/ popup.
Or if I have to follow any other solution please do stuggest.
Thanks
Your problem :
Application two styles StageStyle.UTILITYand StageStyle.UNDECORATED for the same stage.
Because
you asked for suggestion
to solve your problem.
Suggestion :
I suggest to use in-sideFX/Undecorator
(Downlaod the jar file and add it into your project) then we will make some modifications to keep your need ,I tried to create this sample of code for creating a Popup window inside parent window (Owner window) :
package javafxpopup;
import insidefx.undecorator.Undecorator;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
/**
*
* #author Menai Ala Eddine
*/
public class Undecorated extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Click");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Stage popStage = new Stage();
popStage.initStyle(StageStyle.TRANSPARENT);
StackPane root = new StackPane();
root.getChildren().add(new Label("I'm popup window"));
applyModification(popStage, root);
popStage.show();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root,500,500));
primaryStage.setTitle("Hello World!");
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
private void applyModification(Stage primaryStage, StackPane root) {
Undecorator undecorator = new Undecorator(primaryStage, root);
undecorator.getStylesheets().add("/skin/undecorator.css");
Scene scene = new Scene(undecorator, 300, 250);
primaryStage.setScene(scene);
scene.setFill(null);
Node stageMenu = undecorator.lookup("#StageMenu");
stageMenu.setVisible(false);
Node maximize = undecorator.lookup(".decoration-button-maximize");
maximize.setVisible(false);
Node manimize = undecorator.lookup(".decoration-button-minimize");
manimize.setVisible(false);
Node restore = undecorator.lookup(".decoration-button-fullscreen");
restore.setVisible(false);
}
}
By clicking the button the pop window will showing into owner window :
As you see StageStyle.UNDECORATED and StageStyle.UTILITY in the same time.
PS: it is a suggestion you can find many solutions.

Javafx 8 Font converts text to upper case A's

I have a problem using custom Fonts in Javafx 8.
Whenever I try to display a text it gets convertet to upper case A's
my code goes as follows:
package main;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Main extends Application {
private Canvas can;
private GraphicsContext gc;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 800, 400);
stage.setTitle("Test");
can = new Canvas(scene.getWidth(), scene.getHeight());
gc = can.getGraphicsContext2D();
root.getChildren().add(can);
stage.setResizable(false);
stage.setScene(scene);
stage.show();
InputStream is = Main.class.getResourceAsStream("Font/SSF4ABUKET.ttf");
Font font = Font.loadFont(is, 30);
gc.setFont(font);
gc.setFill(Color.RED);
gc.fillText("This is a test", 10, 200);
}
}
If I try this Font in a normal Text Editor (e.g. Open Office) it works perfectly fine.
Thanks for your help in advance.

JavaFX - multiple font sizes in the same Hyperlink

Is this possible? I could just create two Hyperlinks with different font styles, but it looks a little odd when hovering over one doesn't underline both lines of text.
I'd like a Hyperlink with a smaller "caption" text beneath it, clicking either part of the text should do the same thing.
Is this what you are looking for?
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication77 extends Application
{
#Override
public void start(Stage primaryStage)
{
Hyperlink link = new Hyperlink();
link.setText("http://example.com");
link.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
System.out.println("This link is clicked");
}
});
Hyperlink link2 = new Hyperlink();
link2.setText("http://example.com");
link2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent e) {
System.out.println("This link is clicked");
}
});
link.setOnMouseEntered((event) -> {
link2.underlineProperty().setValue(Boolean.TRUE);
});
link.setOnMouseExited((event) -> {
link2.underlineProperty().setValue(Boolean.FALSE);
});
VBox vbox = new VBox();
vbox.getChildren().addAll(link, link2);
StackPane root = new StackPane();
root.getChildren().add(vbox);
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);
}
}
This is one way. You can probably bind the properties bidirectional.

Open PDF file with associated program in JavaFX

I want my java application such that if user chooses to click on a button the PDF opens using the default PDF reader that is installed in the computer.
The PDF which i want to be opened is present in same package "application".
The code which I am using is
package application;
import java.io.File;
import javafx.application.Application;
import javafx.application.HostServices;
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.FileChooser;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(final Stage primaryStage) {
Button btn = new Button();
btn.setText("Load PDF");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
File pdfFile = new File("computer_graphics_tutorial.pdf");
getHostServices().showDocument(pdfFile.toURI().toString());
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If the PDF file is in the same package as the caller file (as you state), then
getHostServices().showDocument(getClass()
.getResource("computer_graphics_tutorial.pdf").toString());
should solve the problem.
The getResource method can be used really flexibly to locate files. Here is a small description how to use it: JavaFX resource handling: Load HTML files in WebView.

Make TextField/TextArea caret visible in JavaFX GUI

I was wondering how to make the caret visible and/or flash whilst using a TextField or TextArea. The ones I have created in my GUI work as the letters appear when typed but there is no visible caret.
I've looked through the TextField documentation but none are about making it visible or not. I expected to find something along the lines "setCaretVisible(Boolean);"
Do I have to make it visible via CSS? If so, any suggestions would be most welcome!
Please see code that I've quickly put together to illustrate the problem:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Test extends Application {
public static void main(String[] arguments) { launch(arguments); }
#Override public void start(Stage stage) {
Scene scene = new Scene(new Group());
scene.setRoot(new BuildLayout(stage));
stage.setTitle("Application Name");
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.show();
}
}
final class BuildLayout extends BorderPane {
protected BuildLayout(Stage stage) {
TabPane tabpane = new TabPane();
Tab tab = new Tab();
tab.setGraphic(new Text("Video Browser"));
tab.setClosable(false);
tab.setContent(new Input(1));
tabpane.getTabs().addAll(tab);
setTop(new Toolbar(stage));
setCenter(tabpane);
}
}
final class Input extends VBox {
Input(int id) {
HBox hbox = new HBox();
TextField videoTitle = new TextField("video_title");
TextArea description = new TextArea( "video_description");
Button share = new Button("share");
Button unshare = new Button("unshare");
setPadding(new Insets(5,10,10,5));
setAlignment(Pos.TOP_CENTER);
hbox.getChildren().addAll(videoTitle, new Text(" Creator: " + " Date: " + " Views: " + " "));
if(true) {
videoTitle.setEditable(true);
description.setEditable(true);
if(true) {
hbox.getChildren().add(share);
} else { hbox.getChildren().add(unshare); }
}
getChildren().addAll(hbox, description);
}
}
final class Toolbar extends HBox {
protected Toolbar(Stage stage) {
Button close = new Button("close");
close.setOnAction((ActionEvent e) -> { stage.close(); });
setAlignment(Pos.CENTER_LEFT);
getChildren().addAll(close);
}
}
Many thanks,
This problem only occurs on Mac and not Windows (untested Linux). The fix is to downgrade to a JavaFX 2.2 compliant maximised window as the setMaximized() from JavaFX 8 also isn't Mac compatible.
Modifying the start method with some code found here:
#Override public void start(Stage stage) {
Scene scene = new Scene(new Group());
scene.setRoot(new BuildLayout(stage));
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setTitle("Application Name");
stage.setScene(scene);
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
stage.show();
}
Produces a fullscreen application with visible and flashing caret.

Categories