I would like to separate the internationalization key from normal text in a labeled javafx element using fxml. The element looks like this:
<Button text="%select..." onMouseClicked="#selectDataLocation" />
And the properties file contains the entry:
select=Select
The resulting text of the button should be Select..., but I get the exception javafx.fxml.LoadException: Resource "select..." not found.
Is there a way to terminate the internationalization key, so that the following text is interpreted as normal text?
Edit:
I'm aware that I could use something like this as a work around:
public class Controller {
#FXML private Button selectButton;
#FXML
private void initialize() {
selectButton.setText(selectButton.getText() + "...");
}
}
But I think that this would be an overkill, just to add some dots to an internationalized string.
Related
Assume I have a class written as below
public MyClass{
public void method2(){
}
public void method1(){
}
}
but I would like to see method1() appearing first and method2() appearing second.
Is there anyway to do that with intellij without manually copy pasting? I cant find any tool inside method summary window.
If you only want to move the method up/down you can simply select it and usectrl + shift + (up/down) in order to move it as wished.
Is there a way in Xamarin's WebView that allows me to attach javascript events to my html elements and call C# method.
I could easily do this in Android by using JavaScriptInterface
<video width="320" height="240" controls="controls" poster='poster.gif'
onclick="window.JSInterface.startVideo('file:///sdcard/test.3gp');"
How would I manage to this in Xamarin
Create a JavaScript Interface Class
Create a C# class that contains methods to be called from JavaScript.
If you are targeting Android API level 17 or later, this
JavaScript-to-C# interface class must annotate each
JavaScript-callable method with [JavascriptInterface] and [Export]
as shown in the following example. If you are targeting Android API
Level 16 or earlier, this interface class must implement
Java.Lang.IRunnable as explained in Android API Level 16 and
Earlier (later in this recipe):
Create a C# class that is derived from Java.Lang.Object. In the following example, we name our class MyJSInterface and implement a
method to display a toast when it is called from JavaScript:
public class MyJSInterface : Java.Lang.Object
{
Context context;
public MyJSInterface (Context context)
{
this.context = context;
}
public void ShowToast ()
{
Toast.MakeText (context, "Hello from C#", ToastLength.Short).Show ();
}
}
Annotate each method that is to be exposed to JavaScript with [Export] and [JavascriptInterface] (see IJavascriptInterface
for more information about the JavascriptInterface annotation). In
the following example, the ShowToast method is annotated so that it
can be called from JavaScript. Note that you must include the
Java.Interop and Android.Webkit using statements as shown in this
example:
using Java.Interop;
using Android.Webkit;
...
[Export]
[JavascriptInterface]
public void ShowToast ()
{
Toast.MakeText(context, "Hello from C#", ToastLength.Short).Show();
}
Add a project reference to Mono.Android.Export (so you can use the [Export] annotation):
1.In Visual Studio, right-click References in the Solution Explorer and select Add Reference.... In Xamarin Studio,
right-click References in the Solution Pad and select Edit
References....
2.In the search field, enter Mono.Android.Export. When you have located it, enable the check mark next to it and click OK.
Refer :
http://dotnetthoughts.net/how-to-invoke-c-from-javascript-in-android/
https://developer.xamarin.com/recipes/android/controls/webview/call_csharp_from_javascript/
https://developer.xamarin.com/samples/monodroid/WebViewJavaScriptInterface/
https://developer.xamarin.com/api/type/Android.Webkit.JavascriptInterface/
I am trying to write a JSF custom component that dynamically chooses creates and renders an existing composite component. So far everthing is working fine except for passing attributes to the composite.
This is my custom component class (error handling etc. stripped for better reading):
#FacesComponent(createTag = true)
public class ChooseEZComp extends UIComponentBase {
#Override
public void encodeBegin(FacesContext context) throws IOException {
Object value = getAttributes().get("value");
String ezCompName = value.getClass().getSimpleName().toLowerCase();
// ezCompName is something like "freelink" or "treenode"
Map<String, Object> params = new HashMap<>();
params.put("node", value);
// log.debug(params.get("node").getClass().getName()) -> yields correct class name
ViewDeclarationLanguage viewDeclarationLanguage = context
.getApplication()
.getViewHandler()
.getViewDeclarationLanguage(context, context.getViewRoot().getViewId());
UIComponent component = viewDeclarationLanguage
.createComponent(context,
"http://xmlns.jcp.org/jsf/composite/ezcomp",
ezCompName,
params);
component.encodeAll(context);
}
}
A composite component (I have several of them), that gets choosen and rendered by this class:
<cc:interface>
<cc:attribute name="node" required="true"/>
</cc:interface>
<cc:implementation>
<h:outputText value="The class of node is: #{cc.attrs.node.class.name}"/>
</cc:implementation>
This is how I use the tag in my JSF page:
<test:chooseEZComp value="#{treeView.selectedNode.data}"/>
So "value" is always guaranteed not of type java.lang.String (it's some JPA #Entity).
But nevertheless the result output in the JSF page is always:
The class of node is: java.lang.String
Where am I wrong? Isn't it possible to pass something other than String as parameter to an composite?
I am runnind wildfly-8.2.0-final with Java EE 7 (and Primefaces 5 but which is not used here)
Any hints welcome!
Edit: of course I also tried to force the type of the attribute in the cc:interface
<cc:interface>
<cc:attribute name="node" required="true" type="some.package.type"/>
</cc:interface>
But this consequently resulted in a IllegalArgument Exception:
IllegalArgumentException: Cannot convert ... of type class java.lang.String to class
Turns out I misunderstood the API ... Map<String, Object> in the signature made me think I can pass an object. But the Javadoc is more precise about this:
attributes - any name=value pairs that would otherwise have been given on the markup that would cause the creation of this component [..]
So you do not pass the value into createComponent but rather the expression that was used to calculate the value for the specified attribute or property name:
Map<String, Object> attributes = new HashMap<>();
ValueExpression valueExpression = getValueExpression("value");
attributes.put("node", valueExpression.getExpressionString());
Funny side read:
to find a solution I debugged through the jsf-imp-2.2.8-jbossorg and stumbled upon the code to create the component. Basically what it does is:
create a JSF xhtml file in the temp folder and use OutputStreamWriter#append to write a JSF page with exactly one tag in it (the one you want to create)
loop through all attributes and write them as attribues into the tag
Save the file and feed it to the DefaultFaceletFactory#createFacelet
create a naming container and make it parent of the generated facelet (apply)
use findComponent on the naming container to get hold of the generated tag and return it
At least after finding this it's clear why you need to pass in the value expression rather than the value itself.
I need to define Wicket validation messages in .properties file of a page for a reusable panel. I will give an example (code snippets below):
MyPage class contains MyPanel with a fragment MyFragment in which there is a component called MyComponent (TextField of a type BigDecimal).
I need to define 3 instances of reusable MyPanel on the MyPage and I need to define a validation key for the MyComponent in MyPage (because they are used in a different context).
class MyPage extends WebPage {
public void onInitialize() {
super.onInitialize();
add(new MyPanel("fuelConsumption");
add(new MyPanel("populationGrowth");
add(new MyPanel("averageGrade");
}
}
class MyPanel extends Panel {
public void onInitialize() {
super.onInitialize();
Fragment fragment = new MyFragment("fragment");
add(fragment);
}
class MyFragment extends Fragment {
public void onInitialize() {
super.onInitialize();
add(new MyComponent("component");
}
}
class MyComponent extends TextField<BigDecimal> {
}
}
So, I need to add a validation messages for all 3 usages of the MyComponent field of type BigDecimal into the MyPage.properties, something like:
fuelConsumption.fragment.component.IConverter.BigDecimal=Fuel consumption must be a decimal
populationGrowth.fragment.component.IConverter.BigDecimal=Please check the population growth format
averageGrade.fragment.component.IConverter.BigDecimal=This is not an average grade!
I deliberately use different kinds of validation messages, I don't want to use a single one with configurable parameter
When I add MyPanel.properties and put a line like this:
component.IConverter.BigDecimal=Component is not a valid BigDecimal.
it works fine, but I really want to specify it outside the reusable panel (so that the panel may be used in other ways and meanings).
How would I do that?
When I try to add the lines above, it does not work, nor in any way I tried, like omitting the "fragment" or "component" from the properties string. Nothing helps.
Thanks for any suggestions!
Your solution looks good to me.
Enable debug logging for org.apache.wicket.resource.loader.ComponentStringResourceLoader to see which keys are searched.
So I've been practising with JavaFX for a while now and while I love it, I find that my application is growing in size- especially in terms of the number of lines of codes for my FXML Controllers.
Right now a typical package for each scene in my application looks like this:
MyFXML.fxml
MyFXMLController.java
MyDataModel.java
For example, I have a form that takes in some information from textfields, comboboxes and radio buttons. When a button is pressed the form information is saved to a database and is also updated immediately in the tableview.
The tableview also allows deleting and updating of the information displayed.
With only a few components (approx. 10) I have a Controller class file that is about 550 lines long with about a 100 of it taken up by the injections (#FXML) and imports and growing!
My application would ideally have multiple of these different kinds of forms and a controller for each. The database queries are all different so it's not quote possible to abstract them out yet. Also, event listeners for the tableview generally require longer code in javafx than other components.
I just feel like there's a better approach to GUI building in javafx than what I'm doing and was wondering if there was some kind of reference I could look up?
I've read up on other stackoverflow answers on the Single Responsibility Principle. If the code below is any hint, my application simply creates a new staff member and allows updating the information. So I'm confused as to whether my class is too file or is this normal for GUI programming?
I'm not asking for coding help, I'm looking for recommendations on how I can improve GUI programming in javafx.
Thank you!
--
FWIW, here's what a sample controller file looks like
package myApp.staff;
//30 something lines of imports...
public class NewStaffMemberController implements Initializable {
//80 something lines of private variables and #FXML injections
public void setConn(Connection aConn) {
conn = aConn;
wrapGenderRadioButtons();
populateDates();
populateStaffTypeComboBox();
populateDepartmentComboBox();
populateStaffTable();
}
private void wrapGenderRadioButtons() {
//4 lines
}
private void populateDates() {
//25 lines
}
private void populateStaffTypeComboBox() {
//20 lines
}
private void populateDepartmentComboBox() {
//22 lines
}
private void populateStaffTable(){
//longest at 100 lines. This code also adds the event listener for the tableview- makes it quite long!
}
private void editSelectedTableRow(Staff selectedstaff){
//4 lines
}
#FXML
private void selectedRadioBtnAction() {
//1 lines
}
#FXML
private void handleYearComboBoxAction() {
//1 lines
}
#FXML
private void handleMonthComboBoxAction() {
//1 lines
}
#FXML
private void handleDayComboBoxAction() {
//1 lines
}
#FXML
private void staffTypeComboBoxAction() {
//1 lines
}
#FXML
private void departmentComboBoxAction() {
//1 lines
}
#FXML
private void btnGenerateStaffId() {
//36 lines
}
#FXML
private void btnSaveInformation(){
13 lines
}
private Boolean validateData() {
//43 lines
}
private void assignStaffId() {
//12 lines
}
private void insertIntoDatabase() {
//35 lines
}
private void updateDatabase(){
//35 lines
}
#Override
public void initialize(URL url, ResourceBundle rb) {
}
}
Well, I think you don't have choice to inject your fxml fields you need. But maybe if you have number of injections like that just in one controller, you should maybe do a better conception of your app, by doing sub controllers working each other together and don't have everything in just one controller. One view doesn't mean one controller. You can have one view and multiple controller with there own view