I have banner an I wont to put it in my javaFX application.
And when user click on the image open default browser.
try {
String path = "http://developer.am/webservice/banner728x90.gif";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
label = new JLabel(new ImageIcon(image));
} catch (Exception exp) {
exp.printStackTrace();
}
also I am trying to convert above code from awt in JavaFX
Lets see. First the ingredients:
Image
Button
ImageView
Open Link in System Browser with JavaFX
Putting this together:
String path = "http://...";
String pathToOpen = "http://...";
Image image = new Image(path);
ImageView imageView = new ImageView(image);
Button button = new Button("clickMe!", imageView);
button.setOnAction(ev -> getHostServices().showDocument(pathToOpen));
Related
I have this simple structure
Now I should load 11.png to ImageView from URI.
How do I do this?
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(new File("What should be here???")));
I think you Are New To Android?
Its Better To move the Image File to Drawable Folder and and Set Image View From the Drawable. You Can Use BatchDrawableImport Plugin in Android Studio to Import multiple Drawable Files
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.yourImageName);
Or Move the File to Assets Folder and Using Assets Manager You Can Achieve the Solution
AssetManager manager = getAssets();
// Read a Bitmap from Assets
try {
InputStream open = manager.open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
ImageView view = (ImageView) findViewById(R.id.ImageView01);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Below is the code I use to view image and caption text together:
try
{
Scanner scanner = new Scanner( new File("data.txt") );
String text = scanner.useDelimiter("ENDOFFILE").next();
scanner.close(); // Put this call in a finally block
System.out.println(text);
String[] data = text.split("\n");
for(int i=0;i<data.length;i++)
{
if(i%2==0)
{
System.out.println("This is "+data[i]);
Image img = new Image(new File(data[i]).toURI().toString(), 100, 0, false, false);
ImageView selectedImage = new ImageView();
selectedImage.setImage(img);
v.getChildren().addAll(selectedImage);
}
else
{
Text t = new Text(data[i]);
t.setFont(Font.font("Tahoma", FontWeight.NORMAL, 14));
v.getChildren().add(t);
}
}
}
catch(Exception E)
{
E.printStackTrace();
}
root2.getChildren().add(v);
Scene scene = new Scene(root2, 1000, 1000);
stage.setScene(scene);
stage.show();
}
data[i] for the image is Udiagram.jpg which is present within the same directory as the program. No exceptions occur but the image is not displayed, it shows empty with only text shown as follows: Text goes here.
The file data.txt is as follows:
UDiagram.jpg
Hit and Run
ENDOFFILE
What could be the reason for not displaying the image but only the text?
I believe you just need to concatenate the file and folder name to the beginning of your String.
Image image = new Image("file:FOLDER_NAME/imageName.gif");
ImageView imageView = new ImageView();
imageView.setImage(image);
imageView.setFitWidth(100);
imageView.setPreserveRatio(true);
imageView.setSmooth(true);
I have an application with 2 stages, I dont want users to close the 2nd stage, just iconify it.
At present I am using the oncloseRequest handler to minimize the window -
secondaryStage.setOnCloseRequest(event -> {
secondaryStage.setIconified(true);
event.consume();
});
I would like to show an icon in the system tray when a user closes the window. And the user should be able to reopen the window from the tray.
Also, how do I ensure when the primary stage is closed, the 2nd stage also closes?
Set the following property in start method
Platform.setImplicitExit(false);
Then set the on close event
secondaryStage.setOnCloseRequest(event -> {
// Your code here
});
To make a system tray try following code:
Original document link: https://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
final PopupMenu popup = new PopupMenu();
URL url = System.class.getResource("/images/new.png");
Image image = Toolkit.getDefaultToolkit().getImage(url);
final TrayIcon trayIcon = new TrayIcon(image);
final SystemTray tray = SystemTray.getSystemTray();
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
Menu displayMenu = new Menu("Display");
MenuItem errorItem = new MenuItem("Error");
MenuItem warningItem = new MenuItem("Warning");
MenuItem infoItem = new MenuItem("Info");
MenuItem noneItem = new MenuItem("None");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(cb1);
popup.add(cb2);
popup.addSeparator();
popup.add(displayMenu);
displayMenu.add(errorItem);
displayMenu.add(warningItem);
displayMenu.add(infoItem);
displayMenu.add(noneItem);
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("TrayIcon could not be added.");
}
Example system tray image:
To invoke method of Javafx from awt event handler you may follw the following way:
yourAwtObject.addActionListener(e -> {
Platform.runLater(() -> primaryStage.show());
});
Also, how do I ensure when the primary stage is closed, the 2nd stage
also closes?
You can use something like this to close the secondary stage when the primary stage is closed:
primaryStage.setOnCloseRequest((WindowEvent we) -> {
secondaryStage.close();
}
I try to make a slide frame looks like powerpoint using imageview and tilepane.
Let's suppose we are in powerpoint. In photo, there are blue background panel(tilepane) and slide(imageview).
I want make a mark(line) that is red arrow pointed in photo, when I locate mouse cursor where between slide5 and slide6. Can I make?
And how to get the each hgap area's information?
ex) Which hgap clicked?
I can get the each slide(imageview) value, but I can't find how to get the hgap's information.
This is my partial code.
String[] imageName = {"slide1.png", "slide2.png", "slide3.png", "slide4.png",
"slide5.png", "slide6.png", "slide7.png", "slide8.png"};
Image img = null;
for (int i = 0; i < imageName.length; i++) {
try {
img = new Image(getClass().getResourceAsStream(imageName[i]));
} catch (Exception e) {
System.out.println("exception : " + e);
}
ImageView imageview = new ImageView(img);
imageview.setUserData(imageName[i]);
imageview.setFitWidth(widthImageView);
imageview.setFitHeight(heightImageView);
HBox hbox= new HBox();
hbox.getChildren().add(imageview);
tile1.getChildren().add(hbox); }
The hgap and vgap parameters are used for calculating the space between the nodes. You can't put a node in there. You should evaluate your requirements and consider using a different layout like e. g. GridPane.
I found some basic example how to print web page with JavaFX but I'm looking for examplewhich can print the complete Scene with app content. Is there any example?
You can convert your scene to an image using
WritableImage snapshot = scene.snapshot(null);
This will return a WritableImage, which can be converted to an Image File or BufferedImage, and print using the Printing API of JavaFX8 (there are not lot of examples available for this, but the new API has quite a resemblance to the old Printing API, so it won't be a problem)
Converting WritableImage to Png File
WritableImage snapshot = scene.snapshot(null);
File file = new File("image.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
Converting WritableImage to BufferedImage (used for printing)
WritableImage snapshot = scene.snapshot(null);
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snapshot, null);
For just a small example of how to print an Image using java, please go through
Proper way of printing a BufferedImage in Java
How to print image in java