ControlsFx Decoration on a Label - javafx

I would like to apply ControlsFx decorations in a TableCell and as such would like to apply them to a Label.
This following does NOT apply the decoration to the Label. Should it?
import org.controlsfx.control.decoration.Decorator;
import org.controlsfx.control.decoration.GraphicDecoration;
import org.controlsfx.validation.decoration.GraphicValidationDecoration;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class LabelDecoration extends Application {
private static final Image REQUIRED_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/required-indicator.png").toExternalForm()); //$NON-NLS-1$
#Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("Test");
Node requiredDecoration = new ImageView( REQUIRED_IMAGE );
Decorator.addDecoration( label, new GraphicDecoration( requiredDecoration, Pos.TOP_LEFT ));
primaryStage.setScene( new Scene( label, 100, 100 ));
primaryStage.show();
}
public static void main(String[] args) {
launch( args );
}
}

The Decorator tries to install a DecorationPane into the scene, which does not exist yet in your case.
Wrap the line Decorator.addDecoration(...) in a Platform.runLater(...) and it will work.

Related

How can I scroll anchorPane in JAVAFX?

hi i write code like this in Below and how can i add Scroll in my project,**Note **The line shape added in the image is added by code and is not in scene builder space
`package schedul;
import edu.classes.drawInschedulClass;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(text);
Scene scene=new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}`
How can i add scroll!??? Please Click to show pic
when i put ScrollPane in myCode,unfortunatly dose not have good performance and myscroll dosnt cover all my Line
this Code is Below
package schedul;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class scene11 extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
AnchorPane root=FXMLLoader.
<AnchorPane>load(getClass().getResource("/schedul/scene1.fxml"));
Line line=new Line();
Text text=new Text();
line.setStartX(50);
line.setStartY(50);
line.setEndX(800);
line.setEndY(50);
root.getChildren().add(line);
HBox content = new HBox(root);
ScrollPane scroller = new ScrollPane(content);
scroller.setFitToWidth(true);
Scene scene= new Scene(new BorderPane(scroller, null, null, null,
null));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I expected the scroll to be created automatically
I took your sample code and removed the irrelevant bits.
All you need to do to make an AnchorPane scrollable is put it in a ScrollPane. This is demonstrated by the sample below. The same will work for any other Pane type.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
public class ScrollApp extends Application {
#Override
public void start(Stage stage) {
stage.setScene(
new Scene(
new ScrollPane(
new AnchorPane(
new Line(50, 50, 800, 50)
)
)
)
);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
After the application launches, by default, it will size to fit all of the content, so you won't see the scroll bars. If you make the window smaller horizontally, you will see the horizontal scrollbar, and similarly for the vertical direction.

Java fx event handler

Why my code is not running? How am I going to display an image on the interface when clicked "add image"?
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class GUIProject extends Application{
/**
* #param args the command line arguments
*/
public void start (Stage stage) throws Exception
{
Pane bPane = new Pane();
Button btOK = new Button("OK");
OKAction OKact = new OKAction();
btOK.setOnAction(OKact);
Button addImage = new Button("Add Image");
isAdd addimage = new isAdd();
addImage.setOnAction(addimage);
//bPane.getChildren().add(btOK);
bPane.getChildren().add(addImage);
Scene scene = new Scene(bPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// TODO code application logic here
Application.launch(args);
}
}
import java.io.FileInputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
public class Pane extends StackPane {
public void addImage() throws Exception
{
Image logo = new Image(new FileInputStream("logo.jpg"));
ImageView logoView = new ImageView(logo);
this.getChildren().add(logoView);
}
}
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
class isAdd implements EventHandler<ActionEvent>{
public void handle(ActionEvent event)
{
try {
Pane.displayImage();
}
catch (Exception ex) {
System.out.println("Error");;
}
}
}
It keep showing static method cannot with non-static method, how can I solve that?
Is there any website can learn javafx event handler?

Adding buttons to Hbox

Dipping my toes for the first time in javaFX and I'm having some problems with adding buttons to a Hbox. It seems like the box doesn't like the type buttons being added. Have no clue why, so just cheking if this has happened to anyone else.
Button knapp1 = new Button("Alphabetical");
Button knapp2 = new Button("Frequency");
HBox hbox = new HBox();
hbox.getChildren().addAll(knapp1, knapp2);
It's the "addAll" method that's the problem, it gives the error
The method addAll(int, Collection<? extends Node> in the type List
<Node> is not applicable for the arguments (Button, Buttons) )
Thanks in advance for what may be a stupid question.
Here is a runnable example as said in the comments check your imports
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
Button knapp1 = new Button("Alphabetical");
Button knapp2 = new Button("Frequency");
HBox hbox = new HBox();
hbox.getChildren().addAll(knapp1, knapp2);
Scene scene = new Scene(hbox);
stage = new Stage();
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) { launch(args); }
}
When run with these imports
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.awt.*;
I get the same error as you remove the import java.awt.*; and replace with import javafx.scene.control.Button;

Snapshot Image can't be used as stage icon

I'm trying to set an image from a stage snapshot as the stage icon.
The following code demonstrates it:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import java.io.IOException;
public class IconTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(new StackPane(new ImageView(generateIcon(32))));
primaryStage.setScene(scene);
primaryStage.getIcons().setAll(generateIcon(32));
primaryStage.show();
}
private static Image generateIcon(int dimension) throws IOException {
Pane root = new Pane();
root.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
Scene scene = new Scene(root, dimension, dimension);
return scene.snapshot(new WritableImage(dimension, dimension));
}
}
Result:
As you can see, the stage icon is not a red square. Why is that? Loading from a file always works, generating the image using AWT works, too.
Workaround:
BufferedImage bimg = new BufferedImage(dimension, dimension, BufferedImage.TYPE_INT_ARGB);
SwingFXUtils.fromFXImage(scene.snapshot(new WritableImage(dimension, dimension)), bimg);
SwingFXUtils.toFXImage(bimg, new WritableImage(dimension, dimension));

Create a webview from a button in JavaFX

I have the following simple code where I have an horizontal block where there are two buttons, the remain part is empty. Now, I would like to view a Webview inside the free part after the user press the "current" button. I already did something similar but I wasn't able to manage the layout because it was a Jframe + JavaFX. I thought to re-build totally in JavaFx , for this reason I want to put the webview in a jfxPanel(I think it's the best solution, any other suggest is really appreciate). I should see the webview when I press the button , in this case current, for this reason I create an handle. So, How can I do it?
Thanks in advance
import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class Launcher extends Application {
public static void main(String[] args) {
launch(Launcher.class, args);
}
Button buttonCurrent;
static HBox web;
static MyBrowser browser_lau;
public void start(Stage stage) {
BorderPane border = new BorderPane();
HBox hbox = addHBox();
border.setTop(hbox);
Scene scene = new Scene(border);
stage.setScene(scene);
stage.setTitle("Layout Sample");
stage.show();
buttonCurrent.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
final JFXPanel jfxPanel = new JFXPanel();
//border.setStyle("-fx-background-color: #ff0000;");
}
});
}
public HBox addHBox() {
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10); // Gap between nodes
hbox.setStyle("-fx-background-color: #336699;");
buttonCurrent = new Button("Current");
buttonCurrent.setPrefSize(100, 20);
Button buttonProjected = new Button("Projected");
buttonProjected.setPrefSize(100, 20);
hbox.getChildren().addAll(buttonCurrent, buttonProjected);
return hbox;
}
}
}
Here is some sample code to get you started, it creates a couple of WebViews and toggles between them depending upon a radio button selection.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class CountThePeople extends Application {
private static final String CURRENT_URL =
"http://www.census.gov/popclock/";
private static final String PROJECTED_URL =
"http://www.sciencedaily.com/releases/2015/08/150810110634.htm";
#Override
public void start(final Stage stage) throws Exception {
WebView current = new WebView();
current.getEngine().load(CURRENT_URL);
WebView projected = new WebView();
projected.getEngine().load(PROJECTED_URL);
StackPane viewHolder = new StackPane();
ToggleGroup choice = new ToggleGroup();
RadioButton currentRadio = new RadioButton("Current");
currentRadio.setToggleGroup(choice);
RadioButton projectedRadio = new RadioButton("Projected");
projectedRadio.setToggleGroup(choice);
choice.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
if (projectedRadio == newValue) {
viewHolder.getChildren().setAll(projected);
} else {
viewHolder.getChildren().setAll(current);
}
});
choice.selectToggle(currentRadio);
VBox layout = new VBox(10, currentRadio, projectedRadio, viewHolder);
layout.setPadding(new Insets(10));
stage.setTitle("World Population");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}

Resources