Tooltip using html javafx - java

I want to add a tooltip on a checkbox of javafx. I want to make it using java code. This tooltip text depends on some object properties. My problem is that i want to have some(not all) words bold inside the tooltip. Is there any way to do it? As far as i googled i didn't found a solution or a way to do it using for example html.
It would be a charm if i could do something like this:
PaggingTest paggingTest = new PaggingTest();
Tooltip tooltip = new Tooltip(
"<b>AlgorithmType:</b> " + paggingTest.getAlgorithmType()
+ "<br/><b>Memory Pages:</b> " + paggingTest.getMemoryPages()
+ "<br/><b>Program Pages:</b> " + paggingTest.getProgramPages()
+ "<br/><b>Sample Count:</b> " + paggingTest.getSampleCount()
+ "<br/><b>Distribution Type:</b> " + paggingTest.getDistributionType());
CheckBox checkBox = new CheckBox("Test");
checkBox.setTooltip(tooltip);
testFlowPane.getChildren().add(checkBox);

You could try this:
WebView web = new WebView();
WebEngine webEngine = web.getEngine();
webEngine.loadContent
(
"<b>AlgorithmType:</b> " + paggingTest.getAlgorithmType()
+ "<br/><b>Memory Pages:</b> " + paggingTest.getMemoryPages()
+ "<br/><b>Program Pages:</b> " + paggingTest.getProgramPages()
+ "<br/><b>Sample Count:</b> " + paggingTest.getSampleCount()
+ "<br/><b>Distribution Type:</b> " + paggingTest.getDistributionType()
);
Tooltip tip = new Tooltip();
tip.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
tip.setGraphic(web);
You may have to apply some css styling to the webview to get it to blend in with your tip.

Related

I can't set a string in a TextView in an Android app (using Java)

I'm building a simple calculator Android app in Java that will receive 2 numbers as inputs and when the user presses one of the 4 action buttons (+, -, *, /) the exercise and it's solution will appear in the bottom of the screen inside a TextView in this format:
{num1} {action} {num2} = {solution}
I tried to declare a string and form the exercise's string in it and in the end I used "setText" to change the TextView but instead of showing the full exercise when I run the app it shows something like "androidx.appcompat.widget.AppCom".
Here is an example for the string I form when the user clicks on the + button:
exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Does anybody know what the issue may be?
You should call getText() befor calling toString():
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Change it to like this.
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

JAVA GWT DialogBox how to add new line char

I am having an issue trying to add a new line to a DialogBox. I have tried the normal "\n" and also tried putting my lines inside of HTML style tags. Neither of these attempts worked. Help is appreciated.
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\t\t" + blink.getMeterId() + "\n X coordinate:\t\t" + blink.getXCoord() + "\n Y coordinate:\t\t" + blink
.getYCoord());
Issue resolved via using setHTML to set the contents of the DialogBox
dialog.setHTML("<p> Meter ID: " + blink.getMeterId() + "</p> <p> X coordinate:\t\t" + blink.getXCoord() + "</p> <p> Y coordinate:\t\t" + blink
.getYCoord() + "</p>");
note this is using <p></p> for new paragraph but <br> and other html will work also
just use another \ as an escape character in a string:
new line: \\n
DialogBox dialog = new DialogBox();
dialog.setText(" Meter ID:\\t\\t" + blink.getMeterId() + "\\n X coordinate:\\t\\t" + blink.getXCoord() + "\\n Y coordinate:\\t\\t" + blink
.getYCoord());

JavaScript output to a String

Is it possible to fetch javascript output to a string in java. I am working with Selenium WebDriver and I expect all child nodes to be listed for which I want to make use of javascript. I am making use of JavascriptExecutor functionality. I want something like this;
JavascriptExecutor js = (JavascriptExecutor)LaunchBrowserTest.driver;
List<String> s = (List<String>)(js.executeScript(" var text = 'aa'; "
+ "var list = document.getElementById('jstree'); "
+ "var anchorlist = document.getElementsByTagName('a'); "
+ "for( i = 0; i < anchorlist.length; i++ ) "
+ "{ "
+ "text = text + anchorlist[i].innerHTML; "
+ "};"
+ "console.log(text);"));
System.out.println("String Array: " + s );
Is there any way I could expect the text outputted and captured into String 's' in java ?
After I get the list, I wanted them to be used in Selenium to click on the nodes as below
WebElement element = driver.findElement(By.linkText(s[i])); where i < s.length
The present output shows me String Array: []
Please suggest me changes/links to get the functionality working.
Answer:
JavascriptExecutor js = (JavascriptExecutor)LaunchBrowserTest.driver;
List<String> s = (List<String>)(js.executeScript(" var text = 'aa'; "
+ "var list = document.getElementById('jstree'); "
+ "var anchorlist = document.getElementsByTagName('a'); "
+ "for( i = 0; i < anchorlist.length; i++ ) "
+ "{ "
+ "text = text + anchorlist[i].innerHTML; "
+ "};"
+ "return text;"));
System.out.println("String Array: " + s );
If you look through the documentation of Selenium JavascriptExecutor and look for the executeScript() method, you will see that your Javascript has to return something in
order to catch it up in Java itself.
Here you can see what kind of data type you will get in Java if your JS script returns
anything.
I hope this helps you out, good luck.

BlackBerry - How to reference a image located at the res/img folder of a project in a BrowserField for GoogleMap

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:///".

How to make only part of the text bold

Can you tell me how I can make only part of the text bold:
TextArea dataPane = new TextArea();
dataPane.appendText("Product Version: " + "1.0");
dataPane.appendText("\nJava: " + "7");
dataPane.appendText("\nRuntime: " + "7");
dataPane.appendText("\nSystem: " + "Linux");
dataPane.appendText("\nUser directory: " + "/home/dir");
I want to make only these strings bold: Product Version, Java, Runtime, System, User directory? What will be the easiest way to do this?
UPDATE
I also tested this code:
TextArea dataPane = new TextArea();
dataPane.setPrefRowCount(10);
Text wd = new Text("Version");
wd.setFont(Font.font ("Verdana", FontWeight.BOLD, 20));
dataPane.appendText(wd + "1.0");
dataPane.appendText("\nJava: " + "7");
dataPane.appendText("\nRuntime: " + "7");
dataPane.appendText("\nSystem: " + "Linux");
dataPane.appendText("\nUser directory: " + "/home/dir");
I get this: Text#149e84241.0
The text is not properly formatted.
You could use an editor pane with the content type set to html instead of a text area. I threw an editor pane into a scratch project really quick and just used the default variable names and object properties. Here's how I produced what I believe you're looking for:
jEditorPane1.setContentType("text/html"); //by default this is text/plain
//use margin-top and margin-bottom to prevent gaps between paragraphs
//or actually customize them to create space if you desire so
jEditorPane1.setText("<p style='margin-top:0pt;'><b>Product Version:</b> 1.0</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>Java:</b> 7</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>Runtime:</b> 7</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>System:</b> Linux</p>" +
"<p style='margin-top:0pt; margin-bottom:0pt;'><b>User directory:</b> /home/dir</p>");

Categories

Resources