I need to build a dynamic dataTable, which carry an object from the database with the following structure:
I need to build a dynamic dataTable, which carry an object from the database with the following structure:
Object:
Name = Ana
Cpf = 12364547
I need this data to be displayed in two columns, for example:
column 1
name
cpf
column 2
Ana
12364547
I have the following code:
public void criarTabelaDinamica(Object obj){
fc = FacesContext.getCurrentInstance();
this.tableMensagem = (HtmlDataTable) fc.getApplication().createComponent(HtmlDataTable.COMPONENT_TYPE);
HtmlColumn column1 = new HtmlColumn();
tableMensagem.getChildren().add(column1);
HtmlColumn column2 = new HtmlColumn();
tableMensagem.getChildren().add(column2);
tableMensagem.setValue(obj); //how to put the items in the two columns of the object? Here is my question!!!
}
note: The object varies on the amount of items in my example are 2, you may have 5 for example.
Thanks!!!
Related
I'm designing a project about cataloging something. In this project user must be able to create his own table as he wish. Therefore I do not have any static class and instance of it.
I'm creating a diaglog pane and I can create textfields for user inputs according to column names of database table dynamically but how can i add those user's inputs into the tableView ?
As I can add any String input into the ListView can I add user String inputs into tableView columns?
ListView<String> listView = new ListView();
public ObservableList<String> listCatalogNames = FXCollections.observableArrayList();
listCatalogNames.add("Books");
More details with an example;
There is listview that contains all catalog names and according to lisview selection tableview will be created dynamically center of borderpane.
User have books(name, author, page) and movies(name, year, director, genree) catalogs.
Lets say user selected movies and tableView appeared with 4 columns and clicked add button. Diaglog pane created with 4 textfield. I built everything until that point but I cannot add user's input into the tableView because i dont have any static class for Movies or Books etc.
Is there any way to create dynamic class ?
Please give me an idea and help me about that situation.
here is the github link of our project
Just use String[] storing the Strings for every column of a row (or a similar data structure) as item type for the TableView. It should be simple enough to create a String[] from the inputs and add it to this TableView:
static TableView<String[]> createTable(String... columnNames) {
TableView<String[]> table = new TableView<>();
for (int i = 0; i < columnNames.length; i++) {
final int index = i;
TableColumn<String[], String> column = new TableColumn<>(columnNames[i]);
column.setCellValueFactory(cd -> new SimpleStringProperty(cd.getValue()[index]));
table.getColumns().add(column);
}
return table;
}
Adding a String[] userInputs to a TableView<String[]> table would be done like this:
table.getItems().add(userInputs);
A similar issue (creating a TableView based on the metadata of a ResultSet) can be found here: How to fill up a TableView with database data
Easiest solution that comes to my mind is to make use of polymorphism. You can create a super class of both Book and Movie, let's call it Item. Then you can declare your table to contain Item and cast to one of the concrete classes when you need to.
I am trying to load use this query and the values into a map
LinkedHashMap<SCRegionPriority, String> hash = new LinkedHashMap<>();
pstmt=("SELECT a.Id,a.RegionName,ISNULL(b.Priority,0) as priority
FROM dbo.region a left join dbo.SCMap b on a.Id = b.RegionId
and a.CountryId = b.CountryId and b.SCId=1 where a.CountryId ='1'")
ResultSet rs = pstmt.executeQuery();
con.commit();
while (rs.next()) {
SCPri c = new SCPri();
c.setRegionid(rs.getInt(1));
c.setRegionname(rs.getString(2));
c.setPriority(rs.getInt(3));
hash.put(c, String.valueOf(rs.getInt(3)));
}
the query gives me the following values:
1,0|2,1|3,1|4,2|...
And then I am trying to load this into the vaadin grid like the following:
List<HashMap<SCPri, String>> rows = new ArrayList<>();
LinkedHashMap<SCPri, String> fakeBean1 = subdao1.getSCMap(subcontractor.getId(),subcontractor.getCountryId());
rows.add(fakeBean1);
Grid<HashMap<SCPri, String>> grid2 = new Grid<>();
grid2.setItems(rows);
for (Map.Entry<SCPri, String> entry : s.entrySet()) {
grid2.addColumn(h -> h.get(entry.getKey().getRegionname())).setCaption(entry.getKey().getRegionname());
}
addComponents(grid2);
I am not able to load the grid with the columns dynamically generated and one editable row of with values underneath those columns.
I am trying to make the grid look like the following:
r1|r2|r3|r4
0 |1 |1 |2
I tried to follow the following two links but failed to get it working:
How to fill Vaadin Grid with Map items type
https://vaadin.com/forum/thread/16038356/grid-8-without-bean-class
How can I achieve this?
A single row with empty cells implies that the value provider callback for each cell returns null (or "").
In your case, the value provider callback is h -> h.get(entry.getKey().getRegionname()). Here, h represents the row object, i.e. your single HashMap<SCPri, String> instance. I'm assuming getRegionname() returns something like a String, which would surely cause null results from h.get() since h uses SCPri instances as they key. Changing the callback to h -> h.get(entry.getKey()) instead might do the trick.
It is an unfortunate leftover from the pre-generics times that Map::get accepts any Object without compilation errors instead of requiring you to pass an instance of the actual key type of the map.
I have simple JTable object with 2 columns. I want to put here values from file.properties but I don't know how do this.
For example file.properties looks like:
some1.text1=Text1
some1.text2=Text2
some2.text1=Text_1
some2.text2=Text_2
And now I want to add these data to TableModel like this(it's example from swing):
Object rowData[][] = { { some1.text1, some2.text1 }, ... };
How can I do this?
You would NOT create a 2 dimensional array since you may not know how many properties you have.
Instead you would create one row of data for each property and then add the row to the DefaultTableModel. The basic logic would be something like:
String columnNames = { "Column1", "Column2" };
DefaultTableModel model = new DefaultTableModel(columnNames, 0);
for (each property pair)
{
Vector<String> row = new Vector<String>(2);
row.addElement( get first value );
row.addElement( get second value );
model.addRow( row );
}
JTable table = new JTable( model );
I found one way to do this using 'new Property()'
This read my file.propertieswell but now I'm interesting something else. How can I read my file in other way, for example my file.property looks like:
some.1.name=...
some.1.value=...
some.2.name=...
some.2.value=...
I can read each of them like this
#ResourceBundleBean(key="some.1.name")
private String some_1_name;
#ResourceBundleBean(key="some.1.value")
private String some_1_value;
etc...
But if is there possibility to use only one String field for for name and value(Value is String too) OR only 1 String field to get each property some.1. some.2. etc and get from this field name and value?
For example if my file.properties will have many item only with name/value some like:
some.1.name=...
some.1.value=...
...
some.200.name=...
some.200.value=...
I do not want to create 200 fields to do this. Is it possible?
Or if it is not possible how can I read arrays from property?
Instead of upper properties make some like this:
some.[1].name=n1
some.[1].value=v1
...
some.[200].name=n200
some.[200].value=v200
And how can I read this array to use it for output some like:
n1 - v1
...
n200 - v200
I have a mySql schema named Contacts that contains 4 tables: Contacts, Phone, Email, and Addresses. The Contacts table contains basic information about a person such as an id number, first name, and last name. The other tables all contain a foreign key that links it to the Contacts table so for example, John Doe in the Contacts table can have multiple phone numbers in the Phones table that are all searchable by using John Doe's id number.
My question is how do I query this schema and return all data for a single (or multiple) users. Can it be done with one SQL statement, or do I need to contact the database for each individual table based on the fact that the amount of results returned will not match for each row returned from the Contacts table. For example, I have some basic search functionality that searches the Contacts table for one or more rows based on search criteria:
public class ContactsListDAO {
//Constants
private static final String SQL_FIND_BY_SEARCH_CRITERIA = "SELECT * FROM Contacts.Contacts WHERE Id LIKE :searchString OR FirstName LIKE :searchString OR LastName LIKE :searchString";
//Variables
private DAOFactory daoFactory;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
//Constructors
public ContactsListDAO(DAOFactory daoFactory) {
this.daoFactory = daoFactory;
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(daoFactory.getDataSource());
}
public List<Contact> findSearchResults(String searchCriteria) {
Map<String, String> namedParameters = Collections.singletonMap("searchString", searchCriteria);
RowMapper<Contact> mapper = new RowMapper<Contact>() {
#Override
public Contact mapRow(ResultSet resultSet, int row) throws SQLException {
Contact contact = new Contact(
resultSet.getInt("Id"),
resultSet.getString("FirstName"),
resultSet.getString("LastName")
);
return contact;
}
};
return namedParameterJdbcTemplate.query(SQL_FIND_BY_SEARCH_CRITERIA, namedParameters, mapper);
}
}
I am using spring to query and map the results back to a Contact bean. How would I go about modifying this SQL statement and mapping functionality to search the contacts table, get the data for each row and then based on the id of each returned row, also query the phone, email, and address tables and then map those to a List object stored in the bean? The problem is that row 1's id might find 8 phone numbers rows that match the id, but row 2's id might only find 3 phone numbers. How is this going to be stored in a ResultSet? Or will I have to query the Contacts table first and then perform a separate query for each other table (for each row returned from the first) and add that data to the bean case by case? If the first query returns 100 results, and I have to perform a query for each of those on 3 tables, I am looking at 301 trips to the database and back.
Is it possible to use one query and just return 1 result from each of the phone, email, address tables for each result found in the Contacts table? Maybe I can add a primary column or something so it only returns 1 result and then if the user clicks something to request more information about the result it can perform the other queries and gather all the info about that user.
The query i've come up with uses LEFT JOIN to search the tables:
SELECT * FROM Contacts.Contacts LEFT JOIN Contacts.Phone ON Contacts.Id = Phone.ContactId AND Phone.Primary = 1 LEFT JOIN Contacts.Email ON Contacts.Id = Email.ContactId AND Email.Primary = 1 WHERE Contacts.Id LIKE :searchString OR Contacts.FirstName LIKE :searchString OR Contacts.LastName LIKE :searchString AND Contacts.OrganizationId = :organizationId
I created a column in the Phone, Email, and Address database called Primary that contains a boolean so that on my initial query I will only return 1 result for each Contact in the database. So far this is doing what I need. Not sure if it the proper way to go about something like this?
I am facing a problem regarding populating bean objects from the resultset.
Description:Resultset contains the result of a stored procedure which is join of 3 tables B, BO, and BOV.
I have 3 POJO's corresponding to tables. The relation between tables is: B can have 0 or more BO's, BO can have 0 or more BOV's. So totally in resultset I have 162 records, which contain duplicates for B.
For example:
B BO BOV
1 1 1
1 1 2
1 2 1
2 1 1
and so on.
Actually there are 10 distinct B's. So II want only 10 B's from the resultset not 162 records. Also II should be able to get corresponding BO and BOV's like for B=1 all values of BO and BOV's.
How can I do this? This is pure java logic and cannot change anything for the stored procedure. Just have to process the resultset.
Have a running Map of the resultset here is a possible pseudocode
[pseudo code only... not guaranteed to compile]
Map mapofBs = new HashMap();
while(rs.hashNext()) {
rs.next();
String bId = rs.getString("columnname for id of b");
B objectB = mapofBs.get(bId);
if(objectB == null) {
objectB = new B();
//read relevant columns from result set and put into objectB
mapOfBs.put(bId, objectB)
}
//now onto the boId
String boId = rs.getString("columnname for id of BO");
BO objectBO = objectB.getBOForId(boId);
if(objectBO == null) {
objectBO = new BO();
//read relevat columns from result set for objectBO
objectB.addObjectBO(objectBO);
}
String bovID = s.getString("columnname for id of BOV");
BOV objectBOV = objectBO.getBOVForId(bovId);
if(objectBOV == null) {
objectBOV = new BOV();
//read relevat columns from result set for objectBOV
objectBO.addObjectBOV(objectBOV);
}
}
//mapOfBs.keySet() gives you a Set<B> which you are interested in
Changing the query to include a GROUP BY is much the best option.