I have a control that must be highly customizable, and that means being able to use an image for the control background. To that end I need to know how I can, in code, set the CSS Style to point to the image specified by the user.
I have the following (which didn't work, I get a warning about "unknown protocol: c" (which I don't even know what that means)):
BG = //The CSS String
"-fx-background-position : 50% 50%;\n" +
"-fx-background-repeat : no-repeat;\n" +
"-fx-background-size : contain;\n" +
"-fx-background-image : url(\"" + GS.bgImage.getAbsolutePath() + "\");\n";
BG += "-fx-border-width : " + GS.borderWidth + ";\n" //For adding the Border
+ "-fx-border-color : " + GS.borderColor.toString();
this.setStyle(BG);
GS is a class I constructed from which the control reads the information to know what to make itself look like. GS.bgImage is the background image control is trying to use as it's background. So... what am I doing wrong here? Should i not be using .getAbsolutePath()? Is it something else?
Okay so it turns out trying to do it with File.getPath() or File.getAbsolutePath() was the issue. Turns out to make it work in code you need to use File.toURI().toURL().
So the proper method would be:
BG = //The CSS String, you need to wrap with a Try/Catch to capture a MalformedURLException, but I'm too lazy to do that here...
"-fx-background-position : 50% 50%;\n" +
"-fx-background-repeat : no-repeat;\n" +
"-fx-background-size : contain;\n" +
"-fx-background-image : url(\"" + GS.bgImage.toURI().toURL + "\");\n";
BG += "-fx-border-width : " + GS.borderWidth + ";\n" //For adding the Border
+ "-fx-border-color : " + GS.borderColor.toString();
Related
I am a new programmer in javafx.
Using java FX in IDE I want to make a student information application.
I want to print the input data into the text area but it prints out in the console now.
(After clicking display button I want to show all the input data in textarea)
please help me out to solve this problem.
I am attaching my code below for your suggestion..
Main.java
Just place
textArea.setText("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
into the action handler.
And remove the imports
import javax.swing.JTextArea;
import java.awt.*;
They are not JavaFX.
You are printing your result into the standard output stream (System.out). I assume you want to display what is printed by:
System.out.println("Name: " + t1.getText() +"\nAddress: " + t2.getText()
+"\nProvince: " + t3.getText() +"\nCity: " + t4.getText()
+"\nPostal Code: " + t5.getText() +"\nPhone Number: " + t6.getText()
+"\nEmail: " + t7.getText() + "\nCourses: "+comboBox.getItems()
+ "\nActivities: "+c1.getText() + "\n"+c2.getText());
Please visit the documentation of the TextArea class. Printing your result into the standard output stream has nothing to do with printing your result in your TextArea. Simply use a normal String that contains your result and set the text of your TextArea accordingly.
this is the code I used for the restaurant bill. I want the outcome like a real recipe, but the problem is the text I get is next to each other instead of underneath. This is the outcome..
Can someone help me with this?
BonField.setText("\tKassabon\n" +
"Klantnummer:" + klant +
"\n=================================================================\t " +
"\n======================================================\t " +
"\nBtw 6%:\t\t\t\t\t" + BTW +
"\nSub Totaal:\t\t\t\t\t" + SubTotaal +
"\nTotaal bedrag:\t\t\t\t\t" + Totaal +
"\nContant betaald:\t\t\t\t\t" + Totaal +
"\n======================================================\t " +
"\nDatum: " + Tdate.format(timer.getTime()) +
"\t\tTijd: " + tTime.format(timer.getTime()) +
"\n\n\tBedankt voor uw bezoek, graag tot ziens\n" );
First things first: Never format your textbox or any other visual element by it content directly in final builds of your applications. Although it is understandable for debug usage and stuff, you'll probably need to handle auto adjustment things within final builds.
Secondly: Too few info to help you, bro. Think and express properly when asking questions or you'll keep having your questions underrated.
Good luck with your work!
I'm showing a google map inside a BrowserField.
This is the relevant code:
private String setUpHtmlString(Coordinates coordinates){
StringBuffer mapString = new StringBuffer();
mapString.append("" +
"<!DOCTYPE html> " +
"<html> " +
" <head> " +
" <meta http-equiv='content-type' content='text/html; charset=UTF-8' /> " +
" <title>Google Maps Multiple Markers</title> " +
" <script src='http://maps.google.com/maps/api/js?sensor=false' type='text/javascript'></script>" +
" </head> " +
" <body> " +
" <div id='map' style='width: 500px; height: 600px;'></div> " +
" <script type='text/javascript'> " +
" var locations = [ ");
for (int i = 0; i < _placesStringArray.length; i++) {
Address address = ((Place)_hashTablePlaces.get(_placesStringArray[i])).getAddress();
mapString.append("['"+address.getDescription()+"', "+address.getLatitude()+", "+address.getLongitude()+", "+i+"]");
if(i<_placesStringArray.length - 1)
mapString.append(",");
}
mapString.append("];");
mapString.append(
" var map = new google.maps.Map(document.getElementById('map'), { " +
" zoom: 15, " +
" center: new google.maps.LatLng(-25.290646, -57.584080), " +
" mapTypeId: google.maps.MapTypeId.ROADMAP }); " +
" var infowindow = new google.maps.InfoWindow(); " +
" var marker, i; " +
" for (i = 0; i < locations.length; i++) { " +
" marker = new google.maps.Marker({ " +
"icon:'https://maps.google.com/mapfiles/kml/shapes/schools_maps.png', "+
" position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); " +
" google.maps.event.addListener(marker, 'click', (function(marker, i) { " +
" return function() { " +
" infowindow.setContent(locations[i][0]); " +
" infowindow.open(map, marker); " +
" } " +
" })(marker, i)); } " +
" </script>" +
" </body>" +
"</html>");
return mapString.toString();
}
As you can see, the icon is pointing to an external url, but how should a write the path to a image file inside the img folder
of my app.
I tried to reference it in many ways, like these:
"icon:'local:///assets/images/marker.png'
"icon:'resources/images/marker.png'
with no success.
Thanks in advance.
I won't advice you to hardcode the html inside the .java files, because the code is not readable and also hard to find for future modifications. Inline javascript is already a bad thing, but putting everything into a string in java code is a step beyond in terms of coupling.
Option #1: the cleanest way
Instead, you can have a resource html file and load it using a "local:///" URL, as the BrowserField demo shows: Display HTML content from a resource in your application
In the same directory you can place javascript and css files, as you would do in a regular static web site. So theoretically you could also place image resource files there and reference them from html or js files. The URLs don't need the full path (e.g.: instead of local:///assets/images/marker.png you would have local:///marker.png).
If you need to insert dynamic content into the html, then you can always insert placeholders into a template html resource file, and then do a string.replace from java code to replace the placeholders with the dynamic fields.
This way you are addressing modifiability and separation of concerns.
Option #2: the dirty hack
I've shown you why your code was dirty, but as so many things in life it can still get worse. You can add a new layer of unmodifiability by encoding your image file to a base64 string, and setting a "data://" url to the image in markup or javascript. It might be acceptable for you if the icon is really small and you know in advance you are not going to change it frequently, but be aware base64 strings can grow really large.
Option #3 (not tested)
This is also kind of a hack. Assuming you can open your image using Class.getResourceAsStream, you could instantiate a ProtocolController in java code, then call setNavigationRequestHandler to set a handler that intercepts the kind of requests you are interested in, and pass the content loaded using Class.getResourceAsStream.
Bonus option: (not tested)
And here's a link to a BB forum post, where a guy shows you can also reference the image using a URL starting with "cod:///".
I've some customized models into Alfresco and I need to extract the aspect information and the content from Repository.
I need, passing the keywords and the model name (it's an aspect), to extract content or the aspects associated to the model.
search/{keywords}?model={model?}
this is the javascript I'm using to extract the content passing the model
var docs = search.luceneSearch("#kd\\:commonname_content_type_tag:\"" + model + "\"");
How can I concatenate two aspects properties?
I did it into Java but the syntax in Javascript seems quite different:
queryString = "+TYPE:\"" + Constants.createQNameString(CommonAspects.NAMESPACE_KD_CONTENT_MODEL, DrugModel.TYPE_SUPPLIER) + "\" ";
queryString += "+#kd\\:SupplierID:" + drugBrandNameBean.getSupplierID();
String supplier = contentQuery.getUUID(queryString);
Another question, how can I process the Javascript docs? Can I access to my aspects?
I tried something like that but it didn't works:
var docs = search.luceneSearch("#kd\\:commonname_content_type_tag:\"" + model + "\"");
for (var i=0; i<docs.length; i++) {
log += "Searching " + commonName + " - Name: " + docs[i].name + "\tPath: " + docs[i].displayPath;
log += "\tType: " + docs[i].commonname_content_type_tag + "\r\n";
}
The rows extracted are correct but the commonname_content_type_tag properties is always not defined:
Searching acarbose - Name: exenatide - Contraindication Path: /Company Home/CommonName Type: undefined
Thanks for the help!
Andrea
Try something like that:
docs[i].properties["kd:commonname_content_type_tag"]
I have following lines of code that just don't want to be wrapped:
tran = TransactionFactory.generateDoubleEntryTransaction(cAcc, acc, qTran.amount, qTran.date, qTran.memo, qTran.payee, qTran.number);
logger.info("Different currencies, Credit: " + entry.getCreditAmount() + " " + creditCurrency.getSymbol() + " -> " + entry.getDebitAmount() + " " + debitCurrency.getSymbol());
Both lines are 150 respective 190 chars long. In my Code formatting settings the maximum line length is set to 100, the Line wrapping settings for Function Calls are all set to "Wrap when necessary". In the preview pane at least it looks like right. Just it doesn't wrap.
I'm using Eclipse 3.6.2 here.
You have to press CTRL+SHIFT+F