How to add a List to HBox in JavaFX? - list

Am trying to read some values from database and display it in a List.
list = new List();
list.setSize(30, 280);
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/project?"
+ "user=root&password=virus");
statement = connect.createStatement();
preparedStatement = connect
.prepareStatement("select subname from subject");
rs=preparedStatement.executeQuery();
while (rs.next()) {
subject = rs.getString("subname");
list.add(subject);
}
}
The import statement for List is-
import java.awt.List;
So I think List control is not available in JavaFX.
I tried to place the List in HBox -
hb.getChildren().addAll(addSubName, list, b2);
Then I got an error -
method addAll in interface ObservableList<E> cannot be applied to given types;
required: Node[]
found: TextField,List,Button
reason: varargs mismatch; List cannot be converted to Node
where E is a type-variable:
E extends Object declared in interface ObservableList
Is this error correctable ? If not, tell me about a control that can be used as a substitute for List in JavaFX.

Related

Doctrine 2 nested set - retrieve full tree in single query

I'm using stof/StofDoctrineExtensionsBundle (Bundle wrapper for Atlantic18/DoctrineExtensions) to implement a Nested Set (tree) entity. The entity is configured and working, but I can't figure out how to retrieve all root notes with all of their children (full trees) in a single query. I currently have the full collection returning however it lazy loads all children, meaning a large number of queries is performed.
Thanks for any help.
You can just use childrenHierarchy() method for whole tree retrieve:
$tree = $repo->childrenHierarchy();
Found a solution.
retrieve full list of node objects:
$repo = $this->getDoctrine()->getManager()->getRepository('NestedEntity');
$nodes = $repo->getChildren();
build tree with your nodes.
$tree = $repo->getRepoUtils()->buildTreeArray($nodes);
buildTreeArray method accepts array of node-arrays, so you must implement ArrayAccess interface in your Entity. Also it puts all children in __children key of node-array.
/**
* #Gedmo\Tree(type="nested")
* #ORM\Table(name="nested_entity")
* #ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
*/
class NestedEntity implements \ArrayAccess
{
public function offsetExists($offset)
{
return property_exists($this, $offset);
}
public function &offsetGet($offset)
{
return $this->$offset;
}
public function offsetSet($offset, $value)
{
$this->$offset = $value;
}
public function offsetUnset($offset)
{
$this->$offset = null;
}
protected $__children = [];
...

How to create list from another typed list with 'List.from'?

Simple dart code:
class User {
String name;
User(this.name);
}
main() {
List<User> users = [new User('Freewind')];
var list = new List.from(users);
print(list.first.name); // ***
}
Notice the line ends with '// *'.
My IDEA editor doesn't recognize list.first as a User, since it can't do the autocompletion when I typed '.name'.
So I have to declare the type:
List<User> list = new List.from(users);
It works but I want to know if there is any other way to let compiler know list has type List<User>?
I tried:
var list = new List<User>.from(users);
Which has wrong syntax.
This one works for me in DartEditor (no error/warning/hint) and of course executes successfully
var list = new List<User>.from(users);

Qjson handling an returned array of ojbects

I'm using Qjson to parse a json object that is returned from a web service. I'm stuck on handling an array of complex ojects.
At the first level the web service returns a map consisting of "error", "id", and "return". If there are no errors I can get the first level value by using
nestedMap = m_jsonObject["result"].toMap();
group = new Group();
group->Caption = nestedMap["Caption"].toString();
group->CollectionCount = nestedMap["CollectionCount"].toInt();
I can even get a date item value that is at the second level using
group->ModifiedOn = nestedMap["ModifiedOn"].toMap()["Value"].toDateTime();
I have an object called "Elements" that consists of 29 key-value pairs. The web service is returning an array of these "Elements" and I am unable to find the right way to parse it. In the header file the container for the elements is defined as
QList<GroupElement> Elements;
The line
group->Elements = nestedMap["Elements"].toList();
causes the compiler to throw an error 'error: no match for 'operator=' in '((MyClass*)this)->MyClass::group->Group::Elements = QVariant::toMap() const()'
I would like to learn the correct syntax to put this element into the class.
Update: I wrote another function to convert the QVariantMap object to a
first:
The group-> Elements object was changed to a
class ParentClass{
QList<SharedDataPointer<Address> > Elements;
other class memmbers...
};
Second:
A method to convert the QMap object to an Address object was created
QSharedDataPointer<Address>
API_1_6::mapToAddress(QVariantMap o)
{
QSharedDataPointer<Address> address (new Address());
address-> FirstName = o["FirstName"].toString();
address->LastName = o["LastName"].toString();
address->CompanyName = o["CompanyName"].toString();
address->Street = o["Street"].toString();
address->Street2 = o["Street2"].toString();
address->City = o["City"].toString();
address->Zip = o["Zip"].toString();
address-> State = o["State"].toString();
address->Country = o["Country"].toString();
address->Phone = o["Phone"].toString();
address->Phone2 = o["Phone2"].toString();
address-> Fax = o["Fax"].toString();
address-> Url = o["Url"].toString();
address->Email = o["Email"].toString();
address->Other = o["Other"].toString();
return address;
}
third: In the code, foreach is used to walk through the list and create and store the new objects
// get the list of the elements
elementsList = nestedMap["Elements"].toList();
// Add the element, converted to the new type, to the Elements object of the'parent' class
foreach(QVariant qElement, elementsList){
group-> Elements.append(mapToAddress(qElement))
}

scala list type mismatch

So I had list of tuples like this:
val rooms = List(("Hi", "mom"),("hi", "dad"))
val foo = rooms.map(arg =>{
var fields = List
( new JField("greeting",arg._1),
new JField("recipient",arg._2))
new JObject(fields)})
And there was much happiness in the land, but when I changed list of room like so:
case class Room(greeting:String, recipient:String)
val rooms = List(Room("Hi", "mom"),Room("hi", "dad"))
val foo = rooms.map(arg =>{
var fields = List
( new JField("greeting",arg.greeting),
new JField("recipient",arg.recipient))
new JObject(fields)})
I get:
[error] <file>: type mismatch;
[error] found : scala.collection.immutable.List.type (with underlying type object List)
[error] required: List[blueeyes.json.JsonAST.JValue]
[error] new JArray(fields)
So it appears that the list is now of Object instead of JField as it was before, why is that?
It works if you don't detach the List from its (:
var fields = List(
new JField("greeting", arg.greeting),
new JField("recipient", arg.recipient))
Basically, it's parsing like this:
var fields = List // assign the List companion object
(new JField("greeting", arg.greeting), // construct a tuple with two items
new JField("recipient", arg.recipient)) // ...but don't use or assign it
new JObject(fields) // Make JObject containing the type
The error comes because the JObject constructor expects a JValue but you are passing it fields which has type List.type.

How to get the first member of the related collection in JPQL

I have Product table which has a related table Images with a relation 1:M.
Class Product {
private Integer productId;
private String productName;
....
....
....
private List<Image> productImageList;
....
....
....
}
Class Image{
private Integer imageId;
private String imageName;
}
Class ProductLite{
private Integer productId;
private String productName;
private String imageName;
}
I am trying a JPQL query where I want to query to fetch products and the first image from the productImageList and returning a ProductLite object using the new constructor.
#TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public List<ProductLite> getAllProductLite() {
Query q = em.createQuery("SELECT NEW com.mycomp.application.entity.ProductLite(p.productId, p.productName, p.productImageList.get(0).getImageName())"
+ " from Product p"
+ " ORDER by p.productName");
List<ProductLite> prods = q.getResultList();
return prods;
}
But for some reason I am not able to get it to work. I get a NoViableException. So I tried moving the logic of getting the first image (getImage() method) to the Product Entity so in the query I could just call the getImage(). Even that does not seem to work.
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [SELECT NEW com.meera.application.entity.ProductLite(distinct p.productId, p.productName, p.getImage()) from Product p, IN(p.productImageList) pil where p.category.categoryCode = :categoryCode ORDER by p.productName ], line 1, column 52: unexpected token [distinct].
Internal Exception: NoViableAltException(23#[452:1: constructorItem returns [Object node] : (n= scalarExpression | n= aggregateExpression );])
Any help is appreciated.
First, you cannot call methods in entity class from your JP QL query. Second, to use the order of entities in list, you need persisted order.
To create column for order to the join table between image and product, you have to add
#OrderColumn-annotation to the productImageList. For example:
#OrderColumn(name = "myimage_order")
//or dont't define name and let it default to productImageList_order
#OneToMany
private List<Image> productImageList;
Then you have to modify query to use that order to choose only first image:
SELECT NEW com.mycomp.application.entity.ProductLite(
p.productId, p.productName, pil.imageName)
FROM Product p JOIN p.productImageList pil
WHERE INDEX(pil) = 0
ORDER by p.productName