public class Picture extends Application {
#Override
public void start(Stage primaryStage){
Pane pane = new HBox(100);
pane.setPadding(new Insets(5,5,5,5));
Next I am trying to upload an image form class package
I have copied the image file from my laptop into the
same package as where this class is (Picture)
Image image = new Image("https://
picturethismaths.files.wordpress.com/2016/03/fig6bigfor
blog.png?w=419&h=364");
Next I am trying to upload an image from online
/*Image image2 = new Image(getClass().getResourceAsStream("Image image =
new Image(getClass().getResourceAsStream(\"pic.png\"));\n" +
" title.setImage(image);"));*/
/* This is the ERROR it gives me ----->> Caused by:
java.lang.IllegalArgumentException: Invalid URL or resource
not found
at javafx.scene.image.Image.validateUrl(Image.java:983)*/
pane.getChildren().add(new ImageView(image));
ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);
ImageView imageView3 = new ImageView(image);
imageView3.setRotate(90);
pane.getChildren().add(imageView3);
Scene scene= new Scene(pane);
primaryStage.setTitle("ShowImage");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args){
Application.launch(args);
}
}
I know this question been before, but believe me I have tried all the solutions given in the answers any nothing worked.
Please only ones who want to help write, no Jokers!
If the image you want to load is in the same directory as the class then try
Image image2 = new Image(getClass().getResourceAsStream("pic.png"));
Related
I have not worked much with gui before, but I am trying to learn it by building a javafx application using SceneBuilder. Now I have trouble getting ImageView to resize when I resize the window.
I am using a gridPane, and i have placed a ImageView on a anchorPane inside one of the routes in the gridPane. The gridPane is changing size when I change the size of my window when i run the application, but the ImageView does not change size. I want the Imageview to change size correspondingly when i change the size of the window when i run the application.
have tried to read similar issues here on the forum, but have not found a solution that I can use, Does anyone have a good way to do this?
Really appreciate all answers.
You can use binding for simplicity, here the full example code
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage){
GridPane gridPane = new GridPane();
AnchorPane anchorPane = new AnchorPane();
// Set image with url
Image image = new Image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR-524e2cCaY9TUn8vglN3nqqOlT0jIJIIty-Z81S49q9Np6Yws");
ImageView imageView = new ImageView(image);
// Bind the imageView width property to gridPane width property
// So, if width of gridPane change, the width of imageView automatically will be change
imageView.fitWidthProperty().bind(gridPane.widthProperty());
// Make the ratio same with original image
imageView.setPreserveRatio(true);
anchorPane.getChildren().add(imageView);
gridPane.getChildren().add(anchorPane);
Scene scene = new Scene(gridPane, 600, 400);
primaryStage.setTitle("Hello World");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
If I load an audio file from a package in the project and start it via IDE then the method setOnEndOfMedia() is working properly. But when I export my project to a runnable jar file (Eclipse library handling = extract) it is not reacting at all. It is reacting when loading the file from a default directory but I want to load it as a ressource from my package. Here's a sample code:
public class MediaTest extends Application
{
public static void main(String[] args)
{
launch();
}
#Override
public void start(Stage primaryStage)
{
Button button1 = new Button("from outside");
Button button2 = new Button("from package");
primaryStage.setScene(new Scene(new HBox(button1, button2)));
button1.setOnAction(e ->
{
Media media = new Media("file:/C:/sample.wav"); //use any audio
MediaPlayer player = new MediaPlayer(media);
player.setOnEndOfMedia(() -> System.out.println("End of Media"));
player.play();
});
button2.setOnAction(e ->
{
Media media = new Media(ClassLoader.getSystemResource("sounds/sample.wav").toExternalForm()); //use any audio
MediaPlayer player = new MediaPlayer(media);
player.setOnEndOfMedia(() -> System.out.println("End of Media"));
player.play();
});
primaryStage.show();
}
}
I'm trying to use EasyUploads-addon for Vaadin, but can't get the image shown.
When I click addon's "Choose File"-button, it will ask me to open an file. I choose some .png -file from my images and addon shows the information of the picture beneath it.
Then I press my "Upload"-button, which is currently needed just to show the uploaded image at first.
private void showUploadedImage() {
Object value = upload.getValue();
byte[] data = (byte[]) value;
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
private static final long serialVersionUID = 1L;
#Override
public InputStream getStream() {
return new ByteArrayInputStream(data);
}
}, "filename.png");
image.setVisible(true);
image.setCaption("Uploaded image");
image.setSource(resource);
}
upload = EasyUpload's component which is used to choose file
image = Embedded component that I have drawn with the designer to my layout.
But when I look the page with browser the image is not shown. The image is just shown as there is no image at all, just caption will be shown.
HTML-code of the image from the page's source:
<img src="null">
This might be really trivial case, but all the examples that I found was over 3-4 years old and didn't look helpful.
Can someone tell me, how it should be done?
You need to call a setter on UploadField class:
uploadField.setFieldType(FieldType.BYTE_ARRAY);
Working SSCCE:
#Override
protected void init(VaadinRequest request) {
setContent(layout);
final UploadField uploadField = new UploadField();
uploadField.setAcceptFilter(".png");
uploadField.setFieldType(FieldType.BYTE_ARRAY);
uploadField.addListener(new Listener(){
#Override
public void componentEvent(Event event)
{
showUploadedImage(uploadField);
}
});
layout.addComponent(uploadField);
layout.addComponent(image);
}
private void showUploadedImage(UploadField upload) {
Object value = upload.getValue();
final byte[] data = (byte[]) value;
StreamResource resource = new StreamResource(
new StreamResource.StreamSource() {
#Override
public InputStream getStream() {
return new ByteArrayInputStream(data);
}
}, "filename.png");
image.setSource(resource);
}
Produces (after choosing a .png file):
Of course you may want to change type of uploadField listener to more specific one.
Tested on Java 8, Vaadin 7.4.1, Eclipse Luna.
I want to code a TextField component with icon.
So the behavior is as follow:
If the TextField contains an empty string, I use "lens.png".
Otherwise, i use "cross.png".
using the JavaFX Scene Builder, I added a TextFiled and an ImageView in the stack pane.
My code is the following:
#FXML
private TextField textSearch;
#FXML
private ImageView imageView;
final Image lensIcon = new Image("/issue/images/lens.png");
final Image crossIcon = new Image("/issue/images/cross.png");
//initialize () method
textSearch.textProperty().addListener(obs -> {
final String text = textSearch.getText();
Image icon = (text==null || text.isEmpty()) ? lensIcon : crossIcon;
imageView.setImage(icon);
imageView.setMouseTransparent(icon == lensIcon);
}
);
imageView.setOnMouseClicked(evt -> textSearch.setText(null));
my issue is the following:
How to prevent writing caracters below the icon (ImageView). the following figure illustrate my issue.
ControlsFX is an JavaFX API that supplies a ton of advanced controls UI that didn't come with JavaFX out of the box.
ControlsFX - http://fxexperience.com/controlsfx/
FontAwesomeFX supplies hundreds of icons (such as a cross in your case above)
FontAwesomeFX - https://bitbucket.org/Jerady/fontawesomefx/downloads/
Here is a demo solution to your problem after importing both these fantastic APIs
public class TextFields_Demo extends Application {
private Parent createContent() {
Pane root = new Pane();
CustomTextField customTextField = new CustomTextField();
FontAwesomeIconView icon = new FontAwesomeIconView(FontAwesomeIcon.CLOSE);
customTextField.setRight(icon);
root.getChildren().add(customTextField);
return root;
}
#Override
public void start(Stage primaryStage) {
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
i have a problem with javafx' ImageView.
I try to load a simple jpg, as an image and add it to an ImageView.
But Javafx can't finde the resouce.
Image image = new Image("image.jpg");
ImageView iv1_1 = new ImageView();
iv1_1.setImage(image);
the exception i get is this one:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
it is crashing in the line, where the image is created.
Here is a picture of the Project:
enter link description here
(I am not allowed to post pictures.)
Use getClass().getResource("image.jpg)
Please find below example to load image using JavaFX.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LoadImage extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Load Image");
StackPane sp = new StackPane();
Image img = new Image("javafx.jpg");
ImageView imgView = new ImageView(img);
sp.getChildren().add(imgView);
//Adding HBox to the scene
Scene scene = new Scene(sp);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Create one source folder with name Image in your project and add your image to that folder otherwise you can directly load image from external URL like following.
Image img = new Image(
"http://mikecann.co.uk/wp-content/uploads/2009/12/javafx_logo_color_1.jpg"
);
this worked for me
Image image = null;
try {
image = new Image(new FileInputStream("image.jpg"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ImageView view = new ImageView();
view.setImage(image);