DynamicReports remove row if column is null - java

I am a little bit stuck with such an easy problem. I am using DynamicReports and I want to hide whole row if column value is null. As I know, DynamicReports is based on JasperReports and it's possible there to do that by checking TextField's option "Remove line when blank". How can I do that in Dynamic?
Components, which I use:
TextColumnBuilder, ColumnGroupBuilder, JasperReportBuilder
I want to hide whole row if any of my TextColumns would be null.

OK, after some thoughts, I found that this problem could be resolved in other way.
We gonna use column, group etc property setPrintWhenExpression(DRIExpression expression)
1. Create class, which will handle, print or not to print the row. Dynamic has the abstract class for doing this:
public class ShowExpressionDynamicReports extends AbstractSimpleExpression<Boolean> {
private String fieldName;
public ShowExpressionDynamicReports(String fieldName) {
this.fieldName = fieldName;
}
#Override
public Boolean evaluate(net.sf.dynamicreports.report.definition.ReportParameters reportParameters) {
return reportParameters.getValue(fieldName) != null;
}
}
You should extend AbstractSimpleExpression in order to pass it as parameter to methods which are listed below.
So, the column value is printed if evaluate(ReportParameters rp) returns true.
I've also added field fieldName which allows me to print (or not) column due to other's column state.
2. Add property to your
column:
setPrintWhenExpression(DRIExpression expression)
group:
.setPrintSubtotalsWhenExpression(DRIExpression expression)
or
setFooterPrintWhenExpression(DRIExpression expression)
or
setHeaderPrintWhenExpression(DRIExpression expression)
Depends on what you want to hide.
Example:
We have 2 columns in our report: Product and ProductCount columns. I would like to hide Product column if ProductCount is null (we have no information for this Product).
So, to do that i will add property PrintWhenExpression to Product column
TextColumnBuilder<String> productColumn = col.column("Product", "Product", type.stringType())
.setPrintWhenExpression(new ShowExpressionDynamicReports("ProductCount"));

Related

Get column value from Ebean model row/record using variable in Play Framework

I am trying to return a value from a row returned from an Ebean model in the Play Framework.
I retrieve the row/record with this:
public static IntakeEdit findByEditKey(String editKey) {
return find.where().eq("editkey", editKey).findUnique();
}
I am cycling through an array with the column names and want to grab that column value. I would like to use the name as variable and get the value that way.
Something along the lines of:
IntakeEdit intakeEdit = IntakeEdit.findByEditKey(editkey);
String fieldValue = "";
String columnToGet = "projectid"
fieldValue = intakeEdit.getColumnValue(columnToGet);
Can this be done? Or is there another method/function I could use?
I appreciate the help!

SmartGWT: Programmatically set edit value in list grid to null for numeric field

I need to set edit value in list grid from existing value to null in numeric field. Edits come from external component and must be reflected on the grid. Filed is not required so it can have null value.
I was trying the following:
1) throws an exception
Integer nullValue = null;
listGrid.setEditValue(rowNum, fieldName, nullValue);
2) look like this is working the same as clearEditValue(rowNum, "fieldName")
HashMap map = new HashMap<>();
map.put("fieldName", null);
listGrid.setEditValues(rowNum, map);
I'm using SmartGWT 6.0p
I'm able to set a cell of a Integer field to null in a ListGrid with the following code
public void setCellValue(int rowNum, String nameOfField, ListGrid listgrid) {
ListGridRecord row = listgrid.getRecord(rowNum);
Integer intNull = null;
row.setAttribute(nameOfField, intNull);
listgrid.updateData(row);
}
After setCellValue was called, the corresponding cell correctly changed to blank.
Found solution. Regardless of filed type I can cast null to String
In this case SmartGWT is not throwing any exception and edit value is set to null
listGrid.setEditValue(rowNum, fieldName (String)null);

create a method where the variable is a jTable

I have 3 different buttons on java, depending on with I press I want to take a value from one of 3 different Tables I have so:
public MethodName (String Table) {
String value=Table.getValueAt(1,1);
Return value;
}
how can this be done so Table is substitute with jTable1, jTable2, or jTable3?
is this possible?

Getting name of JComponent and assigning as parameter in PreparedStatement

Okay, so this is what I have so far and it works. However, I'm not able to specify to which ? (or parameter) the int 1 or 0 (boolean converted to int) is set (through setInt()). I tried using the getName() to see the component variable name but it returns null when I print it.
How can I assign a checkbox's value as parameter to my PreparedStatement if I'm not able to specify correctly on which textbox the value of 1 or 0 is from?
ps.setInt(1,valueOfUnknownCheckbox); //need to put the correct checkbox as 2nd argument.
I saw some information about using Java Reflection which I'm totally unfamiliar with. Is there any way without using Java Reflection?
Some of the checkbox values are not going to the correct database table's column.
I hope you can help with me guys. I wanted to be able to use List to collect components from containers to lessen the lines of codes.
Here's my code.
private void saveAdminPermissions(){
List<Component> adminPermissionsChbxs = fm.getComponentsAsList(administrationPermissionsCheckBoxPanel);
Boolean bool = null;
String updateSQL =
"UPDATE allusers_admin_permissions SET CURC_BTN=?, DISCOUNTS_BTN=?, SECTIONS_BTN=?,"
+ " USERS_BTN=?, SCHEDULING_BTN=?, YRLEVELS_BTN=?, ACCTG_BTN=? WHERE USERID=? ";
try(Connection con = DBUtil.getConnection(DBType.MYSQL);
PreparedStatement ps = con.prepareStatement(updateSQL);)
{
int x=1;
for(Component c : adminPermissionsChbxs){
if(c instanceof JCheckBox){
bool = ( (JCheckBox)c ).isSelected();
JOptionPane.showMessageDialog(null,"Name: "+ ((JCheckBox)c).getName() );
JOptionPane.showMessageDialog(null,bool);
} //--end of if
int boolToInt = (bool)?1:0 ;
ps.setInt(x, boolToInt);
x++;
}//--end of forloop
ps.setInt(8, um.getIdOfSelected(usersList));
ps.executeUpdate(); JOptionPane.showMessageDialog(null,"Update successful");
}catch(SQLException e){
JOptionPane.showMessageDialog(null,"Error#saveAdminPermissions\n"+ e.getMessage());
}
}//--end of method
Thanks.
Several approaches are possible:
Instead of looping though view components, create a permission model having a collection of states such as List<Boolean>. As shown in How to Use Check Boxes, let each JCheckBox have an ItemListener that updates its Boolean value in the List; an example is seen here. Use the list in constructing your PreparedStatement.
Alternatively, store the permission description and state in a suitable TableModel, and use a JTable as the view. As described in How to Use Tables: Concepts: Editors and Renderers, the default renderer/editor for a model value of type Boolean is a check box. Neither the renderer nor editor remember the value between calls to render or edit a cell's value, but the table underlying TableModel must do so. Some guidelines and a typical example are cited here. You should be able to invoke the table's getValueAt() method to find the current setting of the desired cell in a particular row and column, as shown here.

Adding a database ID to an Eclipse Combo

I'm working on a Java application in Eclipse that pulls data out of a MySQL database. I'm populating a combo box with data. So far I can get the value of a field to show up but I can't figure out how to store the database row's unique ID value. One suggestion I found was to create a custom class that could store both the display value and the id value. However, this doesn't appear to work with the Eclipse widget combo object. This is what I have
import org.eclipse.swt.widgets.Combo;
class myClass {
public static void createCombo(ResultSet rs) {
Combo c = new Combo();
while(rs.next()) {
int id = rs.getInt("id");
int display = rs.getString("display");
comboitem ci = new comboitem(id,display);
c.add(ci);
}
}
}
class comboitem {
private int _id;
private String _display;
public comboitem(int id, String display) {
this._id = id;
this._display = display;
}
public int getID(){
return _id;
}
public String toString(){
return _display;
}
}
The above errors at c.add(ci). It's expecting a string, not an object. Is there a way to do this?
No idea but, I've always felt it was a bad move anyway.
Create a collection/list of comboitems, populate the widget from comboitem.display.
Index in the combo is index in your collection.
Means you can unit test lots of things without a UI or with simple mock, and it keeps you away from desktop specific implementations in your data models.
The combo widget displays an array of String's, so simply concatenate the two values if you want to display them both. I am not sure what your end goal is from your question. If it is to select the appropriate comboitem based on the combo selection, then store the comboitems in a Map and use the combo values as the keys.
Another approach is to use a jface ComboViewer which allows you to set the input to a complex object, provide a label provider and more complex controls around the Combo widget.
You should also look up some information on java coding conventions and not access your database directly from the UI unless this is a very simple application.
You can find some examples on using most SWT widgets here.

Categories

Resources