i have a Jlist filled with a hashmap of -Integer,"Obra"- objects, and i have a method that receive a list of obras, i'm trying to get all the selectd obras in that Jlist and trying to pass as parameter to my method, but i keep getting a error that the types mismatch
here's my code
public class Emprestimo {
private Usuario u;
private Calendar dataRealizacao;
private double total = 0;
public Emprestimo(Usuario u){
this.u = u;
dataRealizacao = Calendar.getInstance();
}
public void realizaEmprestimo(Obra... o){
try{...do stuff
anotherclass
emp = new Emprestimo(user);
emp.realizaEmprestimo(JlistObra.getSelectedValuesList()); // here's where i get my error
i have tried to do a cast like this
(emp.realizaEmprestimo((Obra)JlistObra.getSelectedValuesList());
and this
(emp.realizaEmprestimo((Obra[])JlistObra.getSelectedValuesList());
but it doesn't work
and i have done:
emp.realizaEmprestimo((Obra) JlistObra.getSelectedValue());
which works, but with just one select element in my Jlist
i made it work, not the best way possible, but a solutaion is a solution:
int[] indices = JlistObra.getSelectedIndices();
for (int i : indices){
emp.realizaEmprestimo((Obra)JlistObra.getModel().getElementAt(i));
}
Related
I'm dealing with something that seems to be a trivial task but haven't found a solution: How can I access the text on a RichSelectOneChoice? I've only found the values with richSelectOneChoice.getValue() and valueChangeEvent.getNewValue()
But, how is it possible to access the actual text?
My last attempt was this:
private RichSelectOneChoice selClaim;
public void claimTypeVCL(ValueChangeEvent ve){
Map s = selClaim.getAttributes();
Object ss = s.get(ve.getNewValue());
System.out.println(ss);
}
At the moment the console output is null for the corresponding value, no matter what the choice is.
The ADF component bound to the RichSelectOneChoice object is created as a component with inner elements.
I've also tried the solution proposed by Frank Nimphius here https://community.oracle.com/thread/1050821 with the proper object type (RichSelectOneChoice) but the if clause doesn't execute because the children are not instanceof RichSelectOneChoice as suggested but rather javax.faces.component.UISelectItem and this class doesn't include the getLabel() method and running the code actually throws a wide range of errors related either to casting an object to the target type or null pointers when trying to access the label.
Solved it using the UISelectionItem object and its getItemValue() and getItemLabel() methods instead of getLabel() or getValue(), the latter of which was available but didn't render the expected result.
The working code looks like this:
public String selectedOptionStr;
public void socClaimTypeVCL(ValueChangeEvent ve){
selectedOptionStr = "";
RichSelectOneChoice sct = (RichSelectOneChoice)ve.getSource();
List childList = sct.getChildren();
for (int i = 0; i < childList.size(); i++) {
if (childList.get(i) instanceof javax.faces.component.UISelectItem) {
javax.faces.component.UISelectItem csi = (javax.faces.component.UISelectItem) childList.get(i);
if (csi.getItemValue().toString() == ve.getNewValue().toString()) {
selectedOptionStr = csi.getItemLabel();
}
}
}
}
I have a class which holds the string I want to display and an id for that item.
ref class ListBoxItem {
private:
int id;
String ^ name;
public:
ListBoxItem(int id, const char * name) { this->id = id; this->name = gcnew System::String(name); }
virtual String ^ ToString() new { return name; }
};
And I add each item to the ListBox like this:
for(list<string>::iterator i = listItems.begin(); i != listItems.end(); i++)
listBoxItems->Items->Add(gcnew ListBoxItem(2, (*i).c_str()));
This will produce a ListBox with the correct number of items, but all the items are called "ListBoxItem".
Instead, I want the ListBox to display the string which is produced when the ToString method is invoked on ListBoxItem.
You didn't say whether you were using WinForms or WPF, but I believe this answer is valid for either.
(Note: There is a class named ListBoxItem in the framework. You might want to pick a different class name.)
I believe the issue is here:
virtual String ^ ToString() new { return name; }
^^^
This means you're creating a brand new ToString method, which doesn't have anything to do with the Object.ToString method. When the ListBox calls ToString, it doesn't have your class definition, so it just calls Object.ToString(), which you haven't changed.
Switch it to this, and you should be good:
virtual String ^ ToString() override { return name; }
^^^^^^^^
I am trying to generate a Name based on type of an object. In my system, I have,
class Employee {}
Class ContractEmp:Employee{}
class Manager:Employee{}
I am trying to generate name which looks like ContractEmp1 Where 1 will come from incrementer. I am trying to use Generics.
Any Help
Thank you,
With an extension method you could do something like this:
public static class NameExtension
{
private static Dictionary<string, int> counters = new Dictionary<string, int>();
public static string MakeUpName<T>(this T #object)
{
var t = typeof(T);
if ( ! counters.ContainsKey(t.FullName))
counters[t.FullName] = 0;
return t.Name + counters[t.FullName]++;
}
}
Test:
[TestFixture]
class NameTest
{
[Test]
public void test()
{
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
Console.WriteLine(new NameTest().MakeUpName());
}
}
Output:
NameTest0
NameTest1
NameTest2
NameTest3
You can use a private static int in the Employee class which gets incremented on each constructor call. Combining this number with the typeof(this).Name value you can generate the names as described. Do note that the counter will count for all Employee extending classes so if you want an consecutive list of numbers for each Employee extending class, a specific counter should be implemented for every extending class. Also, the counters will be set to zero each time the application restarts.
public Class ContractEmp:Employee{
private static int counter = 1;
private String name = "";
public ContractEmp() {
name = typeof(this).Name + counter++;
}
}
Something like this should work!
Is there a way to store an identifier of a model object or the model object itself in a JavaFX 2 TreeItem<String>? There is just Value to store the text...
I'm populating a TreeView from a list of model objects, and need to find it when the user clicks a node. I'm used to work with Value and Text in .NET Windows Forms or HTML and I am afraid I cannot adapt this way of thinking to JavaFX...
You can use any objects with TreeView, they just have to override toString() for presenting or extend javafx.scene.Node
E.g. for next class:
private static class MyObject {
private final String value;
public MyObject(String st) { value = st; }
public String toString() { return "MyObject{" + "value=" + value + '}'; }
}
TreeView should be created next way:
TreeView<MyObject> treeView = new TreeView<MyObject>();
TreeItem<MyObject> treeRoot = new TreeItem<MyObject>(new MyObject("Root node"));
treeView.setRoot(treeRoot);
I have the same issue as the OP. In addition I want to bind the value displayed in the TreeItem to a property of the object. This isn't complete, but I'm experimenting with the following helper class, where I'm passing in the "user object" (or item) to be referenced in the TreeItem, and a valueProperty (which, in my case, is a property of the item) to be bound to the TreeItem.value.
final class BoundTreeItem<B, T> extends TreeItem<T> {
public BoundTreeItem(B item, Property<T> valueProperty) {
this(item, valueProperty, null);
}
public BoundTreeItem(B item, Property<T> valueProperty, Node graphic) {
super(null, graphic);
itemProperty.set(item);
this.valueProperty().bindBidirectional(valueProperty);
}
public ObjectProperty<B> itemProperty() {
return itemProperty;
}
public B getItem() {
return itemProperty.get();
}
private ObjectProperty<B> itemProperty = new SimpleObjectProperty<>();
}
I have been playing with this for a few hours and I'm stuck. I'm trying to save a list of Favorite objects in the NSUserDefaults using Monotouch. I believe that I am on the right track but I just can't quite get it... here are my model objects:
public class Favorite {
public Favorite (){}
public string Description {get;set;}
public Song Song {get;set;}
}
public class Song {
public Song (){}
public string Name {get;set;}
public string Artist {get;set;}
}
Next, I want to save a list of Favorites that the user has selected. From what I have read, I can use an NSArray to save a list of items in the NSUserDefaults. So how do I go from a List of Favorites to an NSArray of Favorites... I haven't been able to find any documentation on this. Here is my Settings wrapper:
public class Settings {
private static string _favoritesKey = "favorites";
public static IList<Favorite> Favorites {get;set;}
public static void Add(Favorite favorite){
Favorites.Add(favorite);
}
public static void Remove(Favorite favorite){
Favorites.Remove(favorite);
}
public static void Read()
{
var tempFavorites = (NSArray)NSUserDefaults.StandardUserDefaults[_favoritesKey];
if(tempFavorites == null){
Favorites = new List<Favorite>();
}
else {
for(uint i=0;i<tempFavorites.Count;i++){
var fav = tempFavorites.ValueAt(i); //returns IntPtr
// do something to convert to Favorite
// Favorites.Add(converted_favorite);
}
}
}
public static void Write()
{
var tempArray = Favorites.ToArray();
// convert to NSObject[]
NSUserDefaults.StandardUserDefaults[_favoritesKey] = NSArray.FromNSObjects(converted_array);
NSUserDefaults.StandardUserDefaults.Synchronize();
}
}
Am I on the right track? It looks like all I need to do is figure out how to convert to and from NSObjects. Also, if I am saving these custom objects in NSUserDefaults, do they need to be serializable? Much thanks!
If you want to do this, you would need your Favorite class to be a NSObject with native storage that you synchronize with the [Connect] attribute, something like this:
[Register]
public class Favorite : NSObject {
[Connect]
public string Description {
get {
return (string) this.GetNativeField ("Description");
}
set {
this.SetNativeField ("Description", new NSString (value));
}
}
}
You would do the same for your Song class. You can only store native classes in the NSStandardUserDefaults object store.
An alternative would be what Jason suggested and just serialize to a string and then store that as a NSString.
I would try serializing them and then convert to NSString.
Editing, found the way around it:
var Chosenkey = new object[] { NSUserDefaults.StandardUserDefaults.StringForKey("FooIdentifier").ToString() };
var DefValueToSet = new object[] { "Foo" };
var newInfo = NSDictionary.FromObjectsAndKeys(Chosenkey,DefValueToSet);
And then we register this NSDictionary to user defaults and we're done.
I am trying to serialize and then convert the result to a NSstring, but I am still getting the nullexception when trying to create the dictionary:
This is my declaration:
string llalalala = "Thisisatest";
NSString test = SerializeToString(llalalala);
NSDictionary dictionary = new NSDictionary(test, test,null);
This is my method to serialise:
//Method to serialize objects
public NSString SerializeToString(object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
using (StringWriter writer = new StringWriter())
{
serializer.Serialize(writer, obj);
try
{
return (NSString)writer.ToString();
}
catch (Exception ex)
{
return new NSString("");
}
}
}
am I doing anything wrong? the NSstrings are not empty so I have no clue what´s going on here..