More rows in column header - java

I would like to get a table header like this:
But I'm able to get only column header without the third row (y1, y2, y3). How can I do that?
I have something like this so far:
protected ILayer createColumnHeaderLayer(IDataProvider dataProvider,
ILayer bodyLayer,
IUniqueIndexLayer dataLayer,
SelectionLayer selectionLayer) {
IDataProvider columnHeaderDataProvider = new TeppichDiagramHeaderDataProvider(dataProvider);
DataLayer columnBaseLayer = new DataLayer(
columnHeaderDataProvider,
DataLayer.DEFAULT_COLUMN_WIDTH,
30
);
ColumnHeaderLayer columnHeaderLayer = new ColumnHeaderLayer(columnBaseLayer, bodyLayer, selectionLayer, false);
ColumnGroupModel columnGroupModel = new ColumnGroupModel();
ColumnGroupHeaderLayer columnGroupHeaderLayer = new ColumnGroupHeaderLayer(
columnHeaderLayer,
selectionLayer,
columnGroupModel,
false
);
columnGroupHeaderLayer.addColumnsIndexesToGroup(
"Group",
3, 4, 5
);
return columnGroupHeaderLayer;
}
where TeppichDiagramHeaderDataProvider is a dataprovider for the column header. Where it returns 1 from its getRowCount() method.

Related

Star rank in table row are displayed outside the table

when I Run the project the table rows are displayed correctly except the rank stars the show outside the table and inside the colonne a text appears as displayed in the image :
public ListTasksForm(Form previous) {
SpanLabel sp = new SpanLabel();
sp.setText(ServiceTask.getInstance().getAllArticles().toString());
ArrayList<Articles> articles = ServiceTask.getInstance().getAllArticles();
Object[][] rows = new Object[articles.size()][];
for (int iter = 0; iter < rows.length; iter++) {
rows[iter] = new Object[]{
articles.get(iter).getName(), articles.get(iter).getDescription(), articles.get(iter).getLabel(), articles.get(iter).getQuantity(),
articles.get(iter).getRating(), add(createStarRankSlider(articles.get(iter).getId_article()))
};
}
TableModel model = new DefaultTableModel(new String[]{"name", "description", "label", "quantity", "rating", "rate"}, rows);
Table table = new Table(model);
add(table);
getToolbar().addMaterialCommandToLeftBar("", FontImage.MATERIAL_ARROW_BACK, e -> previous.showBack());
}
});
and this is the function for the star rank creation
private Slider createStarRankSlider(int id) {
Slider starRank = new Slider();
starRank.setEditable(true);
starRank.setMinValue(0);
starRank.setMaxValue(10);
int fontSize = Display.getInstance().convertToPixels(3);
Font fnt = Font.createTrueTypeFont("Handlee", "Handlee-Regular.ttf").
derive(fontSize, Font.STYLE_PLAIN);
Style s = new Style(0xffff33, 0, fnt, (byte) 0);
Image fullStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
s.setOpacity(100);
s.setFgColor(0);
Image emptyStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
initStarRankStyle(starRank.getSliderEmptySelectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderEmptyUnselectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderFullSelectedStyle(), fullStar);
initStarRankStyle(starRank.getSliderFullUnselectedStyle(), fullStar);
starRank.setPreferredSize(new Dimension(fullStar.getWidth() * 5, fullStar.getHeight()));
starRank.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
ServiceTask.getInstance().UpdateRank(id,starRank.getIncrements());
}
});
return starRank;
}
You didn't include the code for the initStarRankStyle but it's pretty obvious what you did here. You just relied on the behavior of the container. Table derives Container so it includes all of its methods e.g. add(Component).
But these methods won't work correctly since a table fetches its data from the model and invokes add internally. So you're logic is conflicting with the table.
You need to derive table and define how you want that data to be rendered. You can do that by overriding the method protected Component createCell(Object value, int row, int column, boolean editable) as explained here.

Codename one a loop inside a TableModel

i'm trying to place a loop inside a TableModel to loop all the articles inside an arraylist to insert all the rows inside the table so i can add it to the form and show all the articles to the user
public class ListArticlesForm extends Form {
public ListArticlesForm(Form previous) {
setTitle("List all articles");
SpanLabel sp = new SpanLabel();
sp.setText(ServiceTask.getInstance().getAllArticles().toString());
ArrayList<Articles> articles = ServiceTask.getInstance().getAllArticles();
TableModel model = new DefaultTableModel(new String[]{"name", "description", "label", "quantity", "rating", "rate"},
new Object[][]{
{
// I WANT TO PLACE A FOR HERE (this is showing only the first row !
articles.get(0).getName(), articles.get(0).getDescription(), articles.get(0).getLabel(), articles.get(0).getQuantity(), articles.get(0).getRating(), add(createStarRankSlider())
},});
Table table = new Table(model);
add(table);
getToolbar().addMaterialCommandToLeftBar("", FontImage.MATERIAL_ARROW_BACK, e -> previous.showBack());
}
}
You can't create an array as a for loop inside the array. You need to do it a line sooner.
Object[][] rows = new Object[articles.size()][];
for(int iter = 0 ; iter < rows.length ; iter++) {
rows[iter] = new Object[] {
articles.get(iter).getName(), articles.get(0).getDescription(), articles.get(0).getLabel(), articles.get(0).getQuantity(), articles.get(0).getRating(), add(createStarRankSlider())
};
}
TableModel model = new DefaultTableModel(new String[]{"name", "description", "label", "quantity", "rating", "rate"}, rows);
now after i Run the project the table rows are displayed correctly except the rank stars
the show outside the table and inside the colonne a text appears:
the function for the star rank creation is :
private Slider createStarRankSlider(int id) {
Slider starRank = new Slider();
starRank.setEditable(true);
starRank.setMinValue(0);
starRank.setMaxValue(10);
int fontSize = Display.getInstance().convertToPixels(3);
Font fnt = Font.createTrueTypeFont("Handlee", "Handlee-Regular.ttf").
derive(fontSize, Font.STYLE_PLAIN);
Style s = new Style(0xffff33, 0, fnt, (byte) 0);
Image fullStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
s.setOpacity(100);
s.setFgColor(0);
Image emptyStar = FontImage.createMaterial(FontImage.MATERIAL_STAR, s).toImage();
initStarRankStyle(starRank.getSliderEmptySelectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderEmptyUnselectedStyle(), emptyStar);
initStarRankStyle(starRank.getSliderFullSelectedStyle(), fullStar);
initStarRankStyle(starRank.getSliderFullUnselectedStyle(), fullStar);
starRank.setPreferredSize(new Dimension(fullStar.getWidth() * 5, fullStar.getHeight()));
starRank.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
ServiceTask.getInstance().UpdateRank(id,starRank.getIncrements());
Dialog.show("Success","thank you for rating our product",new Command("OK"));
}
});
return starRank;
}

Apache POI PPT (Java) - Updating PPT Chart underlying values keeping formatting (position, dimension, colors)

I am trying to produce several reports (i.e. N PPTX files) based on different inputs/for different users on the same PPTX template I created.
I have several preformatted XSLFChart on the PPTX template that contains a single dummy underlying values. Charts are already formatted (i.e. legend, color, position, ... both the shape and the related text).
Each chart contains an underlying datasheet as placeholder that I need to substitute with a dynimic values. I have this value in different data structure. I am successful in updating a PieChart (see below example) but I failed with barChart barStackedChart and others.
public static void updatePieChart(XSLFChart chart, Map<String, String> pieChartValues) {
// Embedded Excel workbook that holds the chart data
String pieChartTitle = "MyUpdatedPieChart";
POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
CTPieChart pieChart = plotArea.getPieChartArray(0);
//Pie Chart Series
CTPieSer ser = pieChart.getSerArray(0);
// Series Text
CTSerTx tx = ser.getTx();
tx.getStrRef().getStrCache().getPtArray(0).setV(pieChartTitle);
sheet.createRow(0).createCell(1).setCellValue(pieChartTitle);
String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
tx.getStrRef().setF(titleRef);
// Category Axis Data
CTAxDataSource cat = ser.getCat();
CTStrData strData = cat.getStrRef().getStrCache();
// Values
CTNumDataSource val = ser.getVal();
CTNumData numData = val.getNumRef().getNumCache();
strData.setPtArray(null); // unset old axis text
numData.setPtArray(null); // unset old values
// set model
int idx = 0;
int rownum = 1;
for (Object key : pieChartValues.keySet()) {
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(idx);
numVal.setV(pieChartValues.get(key));
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(idx);
sVal.setV((String) key);
idx++;
XSSFRow row = sheet.createRow(rownum++);
row.createCell(0).setCellValue((String) key);
row.createCell(1).setCellValue(Double.valueOf(pieChartValues.get(key)));
}
numData.getPtCount().setVal(idx);
strData.getPtCount().setVal(idx);
String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
val.getNumRef().setF(numDataRange);
String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
cat.getStrRef().setF(axisDataRange);
// updated the embedded workbook with the data
try (OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream()) {
wb.write(xlsOut);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
Here the same code adapted for BarChart. It compiles fine, but when I try to open the PPT "PowerPoint found a problem with comment in FileName.pptx".
public static void updateBarChart(XSLFChart chart, Map<String, String> pieChartValues, String pieChartTitle) {
// Embedded Excel workbook that holds the chart data
POIXMLDocumentPart xlsPart = chart.getRelations().get(0);
try (XSSFWorkbook wb = new XSSFWorkbook()) {
XSSFSheet sheet = wb.createSheet();
CTChart ctChart = chart.getCTChart();
CTPlotArea plotArea = ctChart.getPlotArea();
CTBarChart barChart = plotArea.getBarChartArray(0);
//Pie Chart Series
CTBarSer ser = barChart.getSerArray(0);
// Series Text
CTSerTx tx = ser.getTx();
tx.getStrRef().getStrCache().getPtArray(0).setV(pieChartTitle);
sheet.createRow(0).createCell(1).setCellValue(pieChartTitle);
String titleRef = new CellReference(sheet.getSheetName(), 0, 1, true, true).formatAsString();
tx.getStrRef().setF(titleRef);
// Category Axis Data
CTAxDataSource cat = ser.getCat();
CTStrData strData = cat.getStrRef().getStrCache();
// Values
CTNumDataSource val = ser.getVal();
CTNumData numData = val.getNumRef().getNumCache();
strData.setPtArray(null); // unset old axis text
numData.setPtArray(null); // unset old values
// set model
int idx = 0;
int rownum = 1;
for (Object key : pieChartValues.keySet()) {
CTNumVal numVal = numData.addNewPt();
numVal.setIdx(idx);
numVal.setV(pieChartValues.get(key));
CTStrVal sVal = strData.addNewPt();
sVal.setIdx(idx);
sVal.setV((String) key);
idx++;
XSSFRow row = sheet.createRow(rownum++);
row.createCell(0).setCellValue((String) key);
row.createCell(1).setCellValue(Double.valueOf(pieChartValues.get(key)));
}
numData.getPtCount().setVal(idx);
strData.getPtCount().setVal(idx);
String numDataRange = new CellRangeAddress(1, rownum - 1, 1, 1).formatAsString(sheet.getSheetName(), true);
val.getNumRef().setF(numDataRange);
String axisDataRange = new CellRangeAddress(1, rownum - 1, 0, 0).formatAsString(sheet.getSheetName(), true);
cat.getStrRef().setF(axisDataRange);
// updated the embedded workbook with the data
try (OutputStream xlsOut = xlsPart.getPackagePart().getOutputStream()) {
wb.write(xlsOut);
}
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
I would like to create a single function that given the template chart (XSLFChart) and the data structure (may be also List - thus one String[] for each datasheet row) containing the new values updates the chart on the PPT file.
Any suggestion/solution? Thanks for your help!

JTable Column defaults with a data value on Load.Now , I need a combobox on the same Column with that default value already selected

I have a Requirement where I got a JTable Column defaulting with certain data on Load.
Now , I need a combobox on the same Column with that default value already selected in the combobox of that column in the Table +few other options to select or change the value of the cell of that column.
Here is the Sample Code:
public class Test extends JFrame {
public void init() {
JTable table = new JTable( new Object[][] { { "Paul J" , "20" }, { "Jerry M" , "30" }, { "Simon K" , "25" } },
new String[] { "Name" , "Age" } );
table.getColumnModel().getColumn(1).setCellEditor( new SampleCellEditor() );
getContentPane().add( new JScrollPane( table ));
setSize( 400, 200 );
setVisible(true);
}
// Sample Editor to Show Combobox with all sample values in that column
// also can edit the value to add new Value that is not in the column
public static class SampleCellEditor extends DefaultCellEditor {
public SampleCellEditor( ) {
super( new JComboBox() );
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row, int column) {
JComboBox combo = ( JComboBox ) editorComponent;
combo.setEditable(true); // make editable so that we can add new values
combo.removeAllItems(); // remove All pre-existing values.
Vector<Object> objectList = new Vector<Object>();
Object obj = null;
for( int i = 0; i < table.getRowCount(); i++ ) {
obj = table.getValueAt( i, column );
if( !objectList.contains( obj ) )
objectList.add( obj );
}
combo.setModel( new DefaultComboBoxModel(objectList) );
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test t = new Test();
t.init();
}
}
I hope this will solve your problem.
Let me Explain clearly with the above Example.
JTable table = new JTable( new Object[][] { { "Paul J" , "20" }, { "Jerry M" , "30" }, { "Simon K" , "25" } },
new String[] { "Name" , "Age" } );
The above is the data that is going to be loaded in the Table Initially.
Where the Columns as Name and Age and values resp.
Now,I need is ' Age ' column will a combobox and For 'Paul J' in the Table populates the 'Age' as 20 by default and the comboBox should appear in this column and the User Now wants to change, the User now will have a option to select another value from the combobox to overwrite the default value.

How to set a right BORDER after a column in BIRT

I am adding a new column with the following script, however, the column is coming without a border on the right hand side.
How can I set a BLACK border on the right hand side?
var mytable = reportContext.getDesignHandle().findElement(tableName);
var colbinds = mytable.getColumnBindings( );
var cs1 = StructureFactory.createComputedColumn( );
cs1.setName("Q2");
cs1.setExpression( "dataSetRow[\"Q2\"]" );
colbinds.addItem( cs1 );
//second parameter is before(-1) or after(1)
mytable.insertColumn(2,1);
//get header and add label
var myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( 2 );
var mylabel = elementFactory.newLabel( null );
mylabel.setText( "Number of Responses (2010 Sites)" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
//tcell.getStyle().borderRightColor = "Black"?
//get first detail row
mydetail = mytable.getDetail( ).get( 0 );
//get first column and add detail data item
var tcell = mydetail.getCells( ).get( 2 );
var mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "Q2");
tcell.getContent( ).add( mydata );
//tcell.getStyle().borderRightColor = "Black"?
You should set style also:
tcell.getStyle().borderRightColor = "Black";
tcell.getStyle().borderRightStyle = "solid";
and optionally:
tcell.getStyle().borderRightWidth = "1pt";

Categories