Having image display based upon passed criteria using JavaFX and FXML - java

I have an app that I want to have an ImageView element in the FXML file display either an image, or a blank section dependant on user input. I was thinking either to somehow surround the FXML imageview element in an if/else block, or to pass either the image filepath, but both of these feel "off" (the former adding back end loging to front end code, the latter having no image path if set to not display and having to handle that somehow).
Would anyone know of a better way to perform an operation like what I'm describing above?

control are initialized in fxml are static, so it not possible to be created imageview directly...
you can use another way i think its work...
1.take a anchor pane,vbox,hbox etc any container.
2.and set image on runtime in this container.
3.like example below.
AnchorPane pne = new AnchorPane();
if(flag==1)
pne.getChildren.add(new ImageView(new Image(getClass().getResourceAsStream("image.png"))));

Related

How to add a image to a Layout in Vaadin (Spring)

I would like to add an image to my web app, but I have some problems.
private void newImage(){
Image image = new Image("src/main/resources/images/sponsor.png");
absoluteLayout.addComponent(image);
}
I did something like that and it doesn't work. I searched for something about FileResource, but I still don't know how to do this.
My image file is placed in the directory src/main/resources/images:
I have also read the documentation site and it's still not working: https://vaadin.com/components/vaadin-image.
The existence of an absolute layout component and a Image class with a constructor taking only one argument implies that you're using Vaadin 8 (or older) whereas the documentation you linked to is for Vaadin 10 and newer.
As can be seen from the screenshot, the constructor parameter that you're passing the image path to is named caption. This is thus a text that is shown next to the image rather than a reference to the image data itself.
You need to also assign a Resource to the image, either as a second constructor parameter or using the separate setSource method. With your file in src/main/resources, the easiest way is to use a ClassResource that loads the data from the classpath.
Taken together, a working example would thus be something like this:
Image image = new Image();
image.setSource(new ClassResource("/images/sponsor.png"));

setImageDrawable vs setBackground for placeholder

I am trying to find the right approach in setting the placeholder image/resource (VectorDrawable resource in my case) for an ImageView that gets set, with the target image bitmap when it is available.
There are two approaches I saw in guides. I can either set a background using the background property on XML or programmatically using setBackground(). Then, I would set the target image using setImageDrawable().
The second approach is to use setImageDrawable() for the placeholder and later use setImageDrawable() again for the target image.
Both approaches work but I have noticed some UI lag when using the first approach. I am not sure if it is caused by the approach or not.
So, I want to know. What is the correct approach in using placeholders and why?
Thank you.
The second approach is always the best for this task.
But still, if you want to go with the first approach then the correct way to use it is like this
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable();
} else {
setBackground();
}
when load image in ImageView changed src value, it's mean not changing background.
change background is expensive for android, It gets worse when we want to load a lot of images (like ListView, RecyclerView, ...).
for having better performance should be changed image src, not changing image background.
for changing image src you can use
image.setImageResource()
or
image.setImageDrawable()
change background suitable for a single image
In addition, all image loader support placeholder if using that's the same doesn't need set manually.

Vaadin 10 Grid add custom image icon

I am using a Vaadin 10 (Flow) Grid to stream data to new rows, and I am trying to add a custom image icon to my TemplateRenderer.
I have a TemplateRenderer setup like this:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which displays my numbers correctly in the grid cell like this:
I would like to have my image rendered inside the same cell, to the left of the numbers.
This was my (very crude) attempt to import it using html:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\"><img src=\"i.imgur.com/3LQBglR.png\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which give's me this:
I think that I might have to wrap the image and numbers into a HorizontalLayout with the image being a separate component - I think I could handle that, but I am unsure of how to do the actual rendering of the image. I can probably accomplish it simply with the internal Vaadin Icons set, but I need to use my own little images.. all icons that I am going to use will be at or less than 25 x 25px.
Thank you so much in advance!
I think you're on the right track, but there's one small detail that causes you trouble. The URL i.imgur.com/3LQBglR.png doesn't define a protocol, which means that the entire string will be treated as a path relative to the location of the hosting page. It will thus try to find a file in a directory named i.imgur.com on your own server.
To fix this, you need to also include the protocol in the image URL, i.e. https://i.imgur.com/3LQBglR.png.
I can also offer a small suggestion for how to make the code easier to read. HTML also supports using ' for enclosing attribute values. This is more convenient to use from Java string since you don't need to use \ to escape ' characters. Your template string could thus be "<div class$='[[item.class]]'><img src='https://i.imgur.com/3LQBglR.png'>[[item.size]]</div>".

How to make an image Viewer in swing?

I am trying to make an image Viewer like the one shown in the figure below:-
Before i can start i have following questions in mind :-
How would i check for the number of images in the target folder so that i can iterate and include all the images in my app.
Secondly,i am thiking to scale the images down to 75x75 .But what i can't think is that how will i slide the images as scrollbar is moved
To be specific,what is the appropriate container for those 75x75 images queue and how that queue is shifted to left or right?(I already know how to get current scrollbar value and add event listeners on it to respond)
To check the number of images in the target folder you can use the File class.
As for the container you might need to create the animation your self. There is no a container ready for doing so.
This site ( and book ) has some ideas about it. I don't know how out-dated it might be though
http://filthyrichclients.org/
Unless I am misunderstanding, making a scrolling list of images is quite simple.
First, create a JList with a datamodel that allows images.
A great example is shown here:
Java drag and drop images in a list
Second, add the JList to a JScrollPane.
The scaling aspect can be easily performed using Scalr:
http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/

How to keep the bottom of an SWT Browser widget visible when appending to the HTML content?

I'm viewing HTML in an SWT Browser widget. I am appending logging messages to the end of the content and would like to keep the bottom visible all the time. Currently, whenever I append text to the content, I first set the new text:
browser.setText(content);
And then I scroll down the Browser widget via JavaScript:
browser.execute("window.scrollTo(0,100000);");
The problem with this is that when I set the text, the widget switches to the top again before scrolling down, so when I append lots of messages quickly, the browser widget is showing the top part most of the time, occasionally flickering when switching to the bottom. This makes it impossible to follow what is being logged at the bottom.
I am aware that I could use a tree viewer and get all the convenience of the Eclipse platform, but there is a Swing version of the app too and both should use the same HTML with CSS presentation.
Ideally I'd like to avoid embedding a Swing component, but if there is one that would allow this, I'd be happy to hear about it. I have tried it with a JEditorPane inside a JScrollPane, appending to the content via the editor kit's read method:
editorPane.getEditorKit().read(/*...*/);
And then scrolling down like this:
editorPane.setCaretPosition(editorPane.getDocument().getLength());
This works very smoothly for the standalone Swing app, but embedded in Eclipse it flickers and does not keep up with fast updates of the HTML content.
Right now the only way I can make this work smoothly inside Eclipse is prepending to the Browser widget's content instead of appending, but I'd really prefer adding new messages at the bottom, not at the top.
Rewriting the whole HTML content every time seems unnecessarily busy-work, and there may not be a way to prevent some browsers from scrolling to the top each time you redraw the entire page. Especially if you allow the logs you show to get very long, this will get slower and slower as the log gets longer.
A better solution might be to use JavaScript to append to the page. You're already using
browser.execute()
How about something like this (assuming "itemID" is the ID of the DIV containing the content):
String newContent = newContent.replaceAll("\n", "<br>").replaceAll("'", "\\\\'");
browser.execute("document.getElementById(\"itemID\").innerHTML += '"
newContent + "'");
You have to do the replaceAll() and you may need a couple more transformations, depending on your log content. I've noticed that browser.execute() doesn't like it if the script contains newlines, for example, and single quotes in your string needed to be quoted as I show above.
I would have just added this as a comment, but it wouldn't let me (not enough reputation). You can ship XUL in a nonstandard location on the mac, by setting a system property.
System.setProperty("org.eclipse.swt.browser.XULRunnerPath", "/fubar/xul/Versions/1.9.0.7/");

Categories