I currently have a problem with setting a CheckBoxTableCell from the FXML. I tried to convert this code to FXML:
tableCol.setCellValueFactory(new PropertyValueFactory<Product, Boolean>("property"));
tableCol.setCellFactory(CheckBoxTableCell.forTableColumn(toStockCol));
where 'property' is just some attribute of the 'Product' Class (from type 'boolean'). This code works fine. I now try to set this in the FXML, like this:
<TableColumn text="Some Col">
<cellValueFactory><PropertyValueFactory property="property" /></cellValueFactory>
<cellFactory><CheckBoxTableCell editable="true" /></cellFactory>
</TableColumn>
This doesn't work, I get the following error (which is a FXML LoadExeption):
Caused by: java.lang.IllegalArgumentException: Unable to coerce CheckBoxTableCell#24d62d1e[styleClass=cell indexed-cell table-cell check-box-table-cell]'null' to interface javafx.util.Callback.
at com.sun.javafx.fxml.BeanAdapter.coerce(BeanAdapter.java:495)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:258)
at com.sun.javafx.fxml.BeanAdapter.put(BeanAdapter.java:54)
at javafx.fxml.FXMLLoader$PropertyElement.set(FXMLLoader.java:1409)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:786)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2827)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2536)
... 42 more
I can not figure out what I am doing wrong. Also, in my opinion there is little to none documentation about how to set a CheckBox in a TableView with FXML.
Note: I would like to set this from the FXML, because it seems to be the spot for this. I know this can be done with the FXML controller. Also, I am just curious.
Any help is greatly appreciated!
Unfortunately CheckBoxTableCell is not a factory, and there is none available in the JavaFX package. You have to write your own factory.
public class CheckBoxTableCellFactory<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
public TableCell<S, T> call(TableColumn<S, T> param) {
return new CheckBoxTableCell<S,T>();
}
}
Then you can define your table column in the FXML file as:
<TableColumn text="Some Col">
<cellValueFactory><PropertyValueFactory property="property" /> </cellValueFactory>
<cellFactory><CheckBoxTableCellFactory /></cellFactory>
</TableColumn>
DonĀ“t forget to include the CheckBoxTableCellFactory or else to declare the full path like org.my.CheckBoxTableCellFactory or the loader will give you a not found exception.
If you like to manage all in FXML here is how to do it.
Don't forget to add <?language javascript?> to FXML header, but keep in mind that JavaScript engine Nashorn is deprecated since Java 11.
<TableColumn text="Married" fx:id="married">
<cellValueFactory>
<PropertyValueFactory property="married" />
</cellValueFactory>
</TableColumn>
<fx:script>
var myCellValue =
javafx.scene.control.cell.CheckBoxTableCell.forTableColumn( married );
married.setCellFactory(myCellValue);
</fx:script>
Your data model should be exposed as below.
private final SimpleBooleanProperty married = new SimpleBooleanProperty( false );
public SimpleBooleanProperty marriedProperty(){
return married;
}
Related
I'm trying to make use of the #onclick when developing a jenkins plugin but nothing happens when the checkbox which has this attribute is clicked (same with #onchange).
I have a checkbox for each job implemented as a ListViewColumn with the corresponding column.jelly file
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<td>
<f:checkbox name="selected" onclick="${it.itClicked()}" onchange="${it.itClicked()}" />
</td>
</j:jelly>
and the method itClicked() resides in the corresponding java class of this jelly file:
class CheckboxTemplate extends ListViewColumn {
...
public void itClicked() {
System.out.println("it clicked!");
}
...
#Extension
public static class DescriptorImpl extends ListViewColumnDescriptor {
...
}
But this apparently is not working...
I've made another plugin before which called a method in the java class from the predefined object "it" from the jelly file and it worked fine, so having the method defined in the working place is not the problem (I assume).
Anyone who has any idea of why itClicked() is not executed when the checkbox is checked/changed?
If there is information that I missted posting, tell me, and I'll come back with it asap!
I managed to solve this by adding
<st:bind var="myItem" value="${it}"/>
in my jelly file and changing #clicked to
onclick="myItem.mark('${job.fullName}')"
where mark() was defined in the backend with the particular annotation
#JavaScriptMethod
public void mark(String job) {
// do what you need to do
}
job if of course a predefined variable in the column.jelly file which is not required for this to work, I only passed it to map the checkbox with the corresponding job name.
Hope this is useful for others who struggle with the documentation (and with the nonexisting complete examples) on these topics.
I've been searching a lot in the internet to solve this problem and couldn't find decent solution. this is the problem:(this is a WPF app, in VS2010)
Assume i have this classes:
public class Airplane
{
public string AirplaneName{get;set;}
public ObservableCollection<People> myPeopleList {get;set;}
}
public class People
{
public string Name{get;set;}
public string SeatNumber{get;set;}
}
Now I want to create an instance of Airplane class including the people list in the file App.xaml. That is, to create my own resource (Application.Resources). I tried this but I get an exception:
<lcl:AirPlaneCollection x:Key="apCollection" >
<lcl:Airplane Name="airbus-780" myPeopleList="{StaticResource pList}" >
</lcl:AirPlaneCollection>
<lcl:PeopleCollection x:Key="pList">
<lcl:People Name="name1" SeatNumber="1"/>
<lcl:People Name="name2" SeatNumber="2"/>
</lcl:PeopleCollection>
AirPlaneCollection and PeopleCollection is just simple class that inherits from ObservableCollection<Airplane> and ObservableCollection<Poeple>
when i try to use that resource (Application.Current.Resources["appCollection"] it compile but i get an exception at run time.
my TreeView DataTemplate is performing well because if i instantiate the classes in the code-behind as regular lists(Add ect.) it works flawlessly..what am i doing wrong??
thank in advanced..
found the solution at last!!
in the Application.Resources the "pList" needs to be declare above "apCollection".
probably the declaration order is important..
[This is .Net 3.5.] I have seen a lot of examples that say, do it this way, so I must just be missing something:
I have a project with a Resources.resx file. In the Resources file are Strings that are Public. One of them is "Cancel", shown in this snippet from Resources.Designer.cs:
namespace WFT.PumpSvc.Bench.Properties {
public class Resources {
public static string Cancel {
get {
return ResourceManager.GetString("Cancel", resourceCulture);
}
}
...
Next, I have some xaml that has
xmlns:strings="clr-namespace:WFT.PumpSvc.Bench.Properties"
and
<wft:TouchButton Name="closeButton">"{x:Static strings:Resources.Cancel}"</wft:TouchButton>
(TouchButton inherits from Button.)
Instead of "Cancel" showing on my button, I see {x:Static strings:Resources.Cancel}. Is it obvious what I have missed that isn't finding the string?
You can't write a markup extension directly in an element (at least not in this form), it must be in an attribute:
<wft:TouchButton Name="closeButton" Content="{x:Static strings:Resources.Cancel}"></wft:TouchButton>
You can also write it like this:
<wft:TouchButton Name="closeButton">
<x:Static Member="strings:Resources.Cancel" />
</wft:TouchButton>
You made it a string, it won't be parsed like this, use element-syntax:
<wft:TouchButton Name="closeButton">
<x:Static Member="strings:Resources.Cancel"/>
</wft:TouchButton>
I have a Windows Phone page code that is shared by multiple applications.
At the top of the page, I show the title of the application, like so:
Is it possible to make the text be bound to the application title as defined in the application's assembly?
I realise that I could do this by code by reading the title in the assembly and then doing something like:
this.ApplicationTitle.Text = title;
I was hoping that the title as defined in the assembly could be accessed with some magic like:
Text={assembly title}" directly from within the xaml.
Thanks
Create a property called ApplicationTitle that returns that name of the application like the following, and then bind to it in XAML:
public string ApplicationTitle
{
get { return System.Reflection.Assembly.GetExecutingAssembly().GetName().Name; }
}
(You can use a relative binding source if you can't or don't want to use the data context.)
edit:
I just realized that my method involved security considerations since GetName is a method that is [Security Critical]. And I got a MethodAccessException stating: Attempt to access the method failed: System.Reflection.Assembly.GetName()
So here's another way to get the assembly name and return it in a property by using the AssemblyTitle attribute.
public string ApplicationTitle
{
get
{
System.Reflection.AssemblyTitleAttribute ata =
System.Reflection.Assembly.GetExecutingAssembly().GetCustomAttributes(
typeof(System.Reflection.AssemblyTitleAttribute), false)[0] as System.Reflection.AssemblyTitleAttribute;
return ata.Title;
}
}
To bind in XAML, you can use this:
Text="{Binding ElementName=LayoutRoot, Path=Parent.ApplicationTitle}"
I just started using PrimeFaces and cannot figure what is wrong with my code. It is exactly the same as the show case sample with the exception of the bean names. I looked at this site for answers without success.
PrimeFaces:
<p:calendar value="#{securityForecastReturnBean.date}"
mode="inline" onSelectUpdate="inputsGrowl"
selectListener="#{securityForecastReturnBean.handleDateSelect}"
required="true" />
Java Bean:
#Component
#Scope("request")
#ManagedBean
public class SecurityForecastReturnBean {
public void handleDateSelect(DateSelectEvent event) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected",
format.format(event.getDate())));
}
}
It should be really straightforward yet it is saying that my bean does not have this property??
Here is the exact error message:
javax.servlet.ServletException: /security_page.xhtml: The class 'com.ls.forecast.webui.beans.SecurityForecastReturnBean' does not have the property 'handleDateSelect'.
javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:546)
com.sun.faces.application.view.JspViewHandlingStrategy.executePageToBuildView(JspViewHandlingStrategy.java:363)
com.sun.faces.application.view.JspViewHandlingStrategy.buildView(JspViewHandlingStrategy.java:154)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
I am using PrimeFaces 2.2.1.
Any help would be greatly appreciated!
I figured out my issue. It had to do with using the wrong namespace. I had to use http://primefaces.org/ui instead of the old one.
This looks like it might be an error in the documentation:
From the Primefaces Guide 2.2:
Ajax Selection
Calendar supports instant ajax selection which means whenever a date is selected a server side
selectListener can be invoked with an org.primefaces.event.DateSelectEvent instance as a
parameter. Optional onSelectUpdate option allows updating other component(s) on page.
<p:calendar value="#{calendarBean.date}" onSelectUpdate="messages"
selectListener="#{calendarBean.handleDateSelect}" />
<p:messages id="messages" />
public void handleDateSelect(DateSelectEvent event) {
Date date = event.getDate();
//Add facesmessage
}
Programatically it seems to expect a property on the managed bean. You can try changing it to this and see if it makes a difference #{calendarBean.handleDateSelect()}. Are you absolutely sure that you are not referencing handleDateSelect elsewhere in the markup?
The source code example given in the PrimeFaces Showcase is wrong. The method should not be taking org.primefaces.event.SelectEvent, it should be taking org.primefaces.event.DateSelectEvent. A quick change to the source code example now looks like this:
public void handleDateSelect(DateSelectEvent event) {
FacesContext facesContext = FacesContext.getCurrentInstance();
SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(event.getDate())));
}