How to get class attributes by using a custom object's accessors when the object is selected in a JComboBox? - arraylist

I need help with using JComboBox: I have an ArrayList which I load into my JComboBox.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class JFrameOrder extends JFrame {
ArrayList <Customer> cust = new ArrayList<Customer>();
public JFrameOrder() {
initComponents();
Inventory.createCustomers();
cust = Inventory.customerList();
jCboCustName.addItem("");
for (Customer c : cust){
jCboCustName.addItem(c);
}
The Inventory class has a method which creates the ArrayList - customer list.
The Customer class has two fields: int custID and String custName.
It also has accessors and mutators.
I can get the JComboBox to show the name by Overriding ToString.
I want to update a JLabel to show the corresponding custID based on the selection.
If the jCboCustName.getSelectedItem() returns an object, shouldn't I be able to use the object's accessors and mutators? After all I did load the element in as an Object. Please help.
Thanks in advance.

Related

Nulls and Empty collections are stored in the database of my custom mapper

I have a custom ObjectMapper that deserializes Product and Order objects. It works fine but it stores and returns nulls in the Mongodb database. Here is my configuration. In my implementation of the custom serializers, I return an empty list/map for empty collections.
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.stereotype.Component;
import java.text.DateFormat;
import java.util.Locale;
#Component("retailObjectMapper")
public class RetailObjectMapper extends ObjectMapper {
public RetailObjectMapper() {
setDateFormat(DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
DateFormat.DEFAULT, Locale.US));
SimpleModule module = new SimpleModule();
module.addDeserializer(Product.class,new ProductDeserializer(Product.class));
module.addDeserializer(Order.class,new OrderDeserializer(Order.class));
setSerializationInclusion(JsonInclude.Include.NON_NULL);
setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
registerModule(module);
}
}
Unfortuantely, the fields not populated return null and empty collections also return null. I have no idea why this is happening, help please.

Populating a single Listview with multiple custom array of objects in Xamarin

I am new to xamarin/Mobile development. I got a task to create a single listview which should populate the objects of two different classes using xamarin.forms.
`Class A
{
string PendingRequestID;
string PendingRequestStatus;
string PendingRequest;
}
Class B
{
String CompletedRequestId;
String ApprovedByUsername;
DateTime CompletedTime
}
`
above are the two different entities and I need to populate the list of objects of both in single list view. Each object is having its own separate UI layout.
How can i specify multiple ItemSource in a ListView in xamarin.forms?
Please help me.
Just create a property of type ObservableCollection<object> for ItemsSource on ListView and use a data template selector in order to provide item-type based template(s) to ListView.
An example can be found here
EDIT - 1 : Sample code
class MyDataTemplateSelector : Xamarin.Forms.DataTemplateSelector
{
public MyDataTemplateSelector()
{
// Retain instances!
this._typeADataTemplate = new DataTemplate(typeof(TypeAViewCell));
this._typeBDataTemplate = new DataTemplate(typeof(TypeBViewCell));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
if (item == null)
return null;
return (item is A) ? this._typeADataTemplate : this._typeBDataTemplate;
}
private readonly DataTemplate _typeADataTemplate;
private readonly DataTemplate _typeBDataTemplate;
}
You can't assign multiple types for ItemSource. Therefore, one way is to use a third class as a base class and then derive your A and B from it. Then use this C type as ItemSource.
Class C{
...
}
Class A : C
{
string PendingRequestID;
string PendingRequestStatus;
string PendingRequest;
}
Class B : C
{
String CompletedRequestId;
String ApprovedByUsername;
DateTime CompletedTime
}
Note that you will then need to check for the right subtype during runtime whenever you click on an Item in the list.
Hope it helps!

Convert ArrayList to Observable List for JavaFX program?

I am a semi-beginner Java programmer, learning Java FX from a variety of sources. In my program, I would like to create a ComboBox, and populate the choices with the toString() output of a series of objects from an ArrayList. Here, pizza toppings are defined as an object. They are created and stored in a PizzaMgr object, basically a glorified wrapper for an ArrayList:
public class Topping{
private String name;
public Topping(String a){
this.name=a;
}
public String toString(){
return this.name;
}
}
//=================================================
import java.util.ArrayList;
public class PizzaMgr{
private ArrayList<Topping> OrderedToppings;
public PizzaMgr(){
OrderedToppings = new ArrayList<Topping>();
OrderedToppings.add(new Topping("Pepperoni"));
OrderedToppings.add(new Topping("Mushrooms"));
OrderedToppings.add(new Topping("Onions"));
}
public ArrayList<Topping> getList(){
return OrderedToppings;
}
}
So far, so good. But the hitch I hit is when I want a ComboBox to list all of those items in the PizzaMgr's ArrayList. Ideally, I'd like to use this ComboBox constructor:
ComboBox<T>(ObservableList<T> items)
The problem? How to extract all the ArrayList items into an Observable List? I've been reading up on Arraylists, Observable Lists, interfaces in general, but I can't figure out how to get this to work. I've read that an ArrayList is a Collection, and Observable List can be an interface to a Collection, so I thought I was home free. However, when I try to implement the ComboBox constructor:
import javafx.scene.control.*;
public class Menu{
public static void main(String[] args){
PizzaMgr m = new PizzaMgr();
ComboBox<Topping> topMenu = new ComboBox<Topping>(m.getList());
}
}
I get the compiler error:
Menu.java:18: error: incompatible types: ArrayList<Topping> cannot be converted to ObservableList<Topping>
ComboBox<Topping> topMenu = new ComboBox<Topping>(m.getList());
^
So obviously my ArrayList isn't seen as an Observable List.
What stumps me is: How can I present my ArrayList to the ComboBox constructor, making it seem like an Observable List? Is it a syntax slight-of-hand? Or do I have to convert the ArrayList into another data structure in advance?
Many thanks,
-RAO
ObservableList is a sub interface (specialized version of) List. (ObservableList adds functionality for observing changes to the list.) ArrayList is a particular implementation of List, but is not an implementation of ObservableList. Hence you can pass an ArrayList anywhere a List is expected, but you cannot pass an ArrayList if an ObservableList is expected.
As something of an aside, note it's not really recommended to expose the implementation type, but you should really just expose the interface type:
import java.util.List;
import java.util.ArrayList;
public class PizzaMgr{
private List<Topping> orderedToppings;
public PizzaMgr(){
orderedToppings = new ArrayList<Topping>();
orderedToppings.add(new Topping("Pepperoni"));
orderedToppings.add(new Topping("Mushrooms"));
orderedToppings.add(new Topping("Onions"));
}
public List<Topping> getList(){
return orderedToppings;
}
}
To create an ObservableList from a regular List, you can use
ComboBox<Topping> topMenu
= new ComboBox<Topping>(FXCollections.observableList(m.getList()));
which creates a new ObservableList that "wraps" the array list you provide. I.e. calling get(index) or add(item) on the observable list simply delegates to the list you provide.
You could also do
ComboBox<Topping> topMenu
= new ComboBox<Topping>(FXCollections.observableArrayList(m.getList()));
which would create a new observable list and copy all the elements from the list you provide into it. So subsequently manipulating one list would not change the other.
To create an observableList you need to, first of all, have a predefined list eg an arrayList. Pass the list to an instance of observableList as below...
//Assuming you had this for a list
List<String> list = new ArrayList<String>();
//Pass it over in this manner
ObservableList<String> observableList = FXCollections.observableList(list);
//Then add a listener
observableList.addListener (new ListChangeListener() {
//override the method here
}
Hope it will help...
ArrayList<Site> sites = new ArrayList < Site > ();
sites.add(new Site("Google", "http://www.google.com"));
ObservableList<Site> siteList = FXCollections.observableArrayList(sites);
final ComboBox comboBox = new ComboBox(siteList);
Button btn = new Button();
btn.setText("Read comboBox");
btn.setOnAction(new EventHandler < ActionEvent > () {
#Override
public void handle(ActionEvent event) {
label.setText("selected: " + comboBox.getValue());
Site site = (Site) comboBox.getSelectionModel().getSelectedItem();
System.out.println(site.name);
}
});

What's different between Arraylist and Arrayadapter

look at this code and tell if i use
Arraylist whats happen ..i want to know whats a diffrent between ArrayList and Arrayadaptor?
public class structNote {
public String title;
public String description;
public boolean done;
}
public class AdapteNote extends ArrayAdapter<structNote> {
public AdapteNote(ArrayList<structNote> array) {
super(G.context, R.layout.adaptornote, array);
}
ArrayList is list which holds list of int, String...
ArrayAdapter is adapter, which is used for Listing items in screen, like for ListView. It has more functions for that, if item removed in background, ArrayAdapter will remove it as well if you call adapter.notifyDatasetChanged(), it updates screen. You can not use ArrayList as adapter for Listing items on screen, but you can use it for holding list of items in memory.
Picture below ArrayAdapter holds items inside ListView. You can not use ArrayList for such listing.

Add a series of instances of a Class to ArrayList variable

This code seems to match pretty close to an example I found on online, but its not right, and I can't find an example that matches what I'm trying to do. Maybe I just don't have the parameters and fields setup correctly; or maybe my approach is wrong.
package vibrationSimulator;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import sineWaveAnimation.SineDisplay;
import audioOutput.JFrame_sliderSine;
public class VibrationSimulator {
private static List<MachineParameters> machineParameterArray;
private static int ia;
private static MachineParameters machineParameter_1;
public static void main(String[] args) {
Start();
}
public static void Start() {
ArrayList<MachineParameters> machineParameterArray = new ArrayList<MachineParameters>();
MachineParameters machineParameter_1 = new MachineParameters();
machineParameter_1.frame.setVisible(true);
}
public static void Process() {
// machineParameterArray.add(machineParameter_1);
if (MachineParameters.isGoDone()) {
// machineParameterArray[ia] = machineParameter_1;
MachineParameters machineParameter_1 = new MachineParameters();
machineParameter_1.frame.setVisible(true);
} else {
StartAnimations();
}
}
I have MachineParameters Class whith a simple GUI for inputting the values. Start creates the first instance of MachineParameters and Process is supposed to add that instance to the ArrayList when it is called from the MachineParameter Class. Process creates another instance if GoDone is true or starts and animation if false. The ArrayList will be used to set the parameters for the animation. The true/false is set by the Submit or Done buttons on the GUI.
When I add the Remmed out line:
// machineParameterArray.add(machineParameter_1);
machineParameters_1 doesn't refer to the instance.
Also, all instances have the same name; I'm not sure how to increment a variable name - doesn't seem correct.
Laugh at me if you want, but I will get this right eventually; and then I'll know how to do it.
Remove ArrayList from the constructor. Apparently it creates a local variable instead of an instance.
So:
machineParameterArray = new ArrayList();