Java 当组更改时,如何更改细节标注栏颜色?

Java 当组更改时,如何更改细节标注栏颜色?,java,jasper-reports,grouping,ireport,Java,Jasper Reports,Grouping,Ireport,我的数据库中有一个类别和客户表,如下所示: 然后我想从这些表中生成报告,并在jasper中按category id进行分组。这是我的jrxml文件: <?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insta

我的数据库中有一个类别和客户表,如下所示:

然后我想从这些表中生成报告,并在jasper中按
category id
进行分组。这是我的
jrxml
文件:

    <?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="jasper" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
    <queryString language="plsql">
        <![CDATA[select c.cid as "CID", c.title, j.id, j.name, j.family
  from Jasper j
  join Category c
    on j.c_id = c.cid]]>
    </queryString>
    <field name="CID" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="TITLE" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="ID" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="NAME" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="FAMILY" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <group name="category">
        <groupExpression><![CDATA[$F{CID}]]></groupExpression>
        <groupFooter>
            <band>
                <frame>
                    <reportElement x="1" y="0" width="212" height="0"/>
                    <box>
                        <topPen lineWidth="0.5"/>
                    </box>
                </frame>
            </band>
        </groupFooter>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <pageHeader>
        <band height="33" splitType="Stretch">
            <staticText>
                <reportElement x="196" y="1" width="162" height="32"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas" size="20" isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Jasper]]></text>
            </staticText>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement mode="Opaque" x="213" y="0" width="100" height="20" backcolor="#F0F0F0"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="313" y="0" width="100" height="20" backcolor="#F0F0F0"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="413" y="0" width="100" height="20" backcolor="#F0F0F0"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <text><![CDATA[Family]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="1" y="0" width="100" height="20" backcolor="#F0F0F0"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <text><![CDATA[CID]]></text>
            </staticText>
            <staticText>
                <reportElement mode="Opaque" x="101" y="0" width="112" height="20" backcolor="#F0F0F0"/>
                <box>
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <text><![CDATA[Title]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="213" y="0" width="100" height="20"/>
                <box leftPadding="5">
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement>
                    <font fontName="Consolas"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="313" y="0" width="100" height="20"/>
                <box leftPadding="5">
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement>
                    <font fontName="Consolas"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{NAME}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="413" y="0" width="100" height="20"/>
                <box leftPadding="5">
                    <topPen lineWidth="0.5"/>
                    <leftPen lineWidth="0.5"/>
                    <bottomPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
                <textElement>
                    <font fontName="Consolas"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{FAMILY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="1" y="0" width="100" height="20">
                    <printWhenExpression><![CDATA[new Boolean($V{category_COUNT}.longValue() == 1 ? true : false)]]></printWhenExpression>
                </reportElement>
                <box leftPadding="5">
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{CID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="101" y="0" width="112" height="20">
                    <printWhenExpression><![CDATA[new Boolean($V{category_COUNT}.longValue() == 1 ? true : false)]]></printWhenExpression>
                </reportElement>
                <box leftPadding="5">
                    <topPen lineWidth="0.0"/>
                    <leftPen lineWidth="0.0"/>
                    <bottomPen lineWidth="0.0"/>
                    <rightPen lineWidth="0.0"/>
                </box>
                <textElement verticalAlignment="Middle">
                    <font fontName="Consolas"/>
                </textElement>
                <textFieldExpression class="java.lang.String"><![CDATA[$F{TITLE}]]></textFieldExpression>
            </textField>
            <frame>
                <reportElement x="1" y="0" width="100" height="20"/>
                <box>
                    <leftPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
            </frame>
            <frame>
                <reportElement x="101" y="0" width="112" height="20"/>
                <box>
                    <leftPen lineWidth="0.5"/>
                    <rightPen lineWidth="0.5"/>
                </box>
            </frame>
        </band>
    </detail>
</jasperReport>

此文件的结果如下所示:

<style name="myStyle">
    <box>
        <pen lineWidth="0.25"/>
        <topPen lineWidth="0.25"/>
        <leftPen lineWidth="0.25"/>
        <bottomPen lineWidth="0.25"/>
        <rightPen lineWidth="0.25"/>
    </box>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) != 0]]></conditionExpression>
        <style mode="Opaque" forecolor="#000000" backcolor="#FFFF00"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) == 0]]></conditionExpression>
        <style forecolor="#000000" backcolor="#FFFFFF"/>
    </conditionalStyle>
</style>

但我希望我有这样的结果:

<style name="myStyle">
    <box>
        <pen lineWidth="0.25"/>
        <topPen lineWidth="0.25"/>
        <leftPen lineWidth="0.25"/>
        <bottomPen lineWidth="0.25"/>
        <rightPen lineWidth="0.25"/>
    </box>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) != 0]]></conditionExpression>
        <style mode="Opaque" forecolor="#000000" backcolor="#FFFF00"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) == 0]]></conditionExpression>
        <style forecolor="#000000" backcolor="#FFFFFF"/>
    </conditionalStyle>
</style>


我该怎么做呢?

您可以使用条件样式。 定义如下样式:

<style name="myStyle">
    <box>
        <pen lineWidth="0.25"/>
        <topPen lineWidth="0.25"/>
        <leftPen lineWidth="0.25"/>
        <bottomPen lineWidth="0.25"/>
        <rightPen lineWidth="0.25"/>
    </box>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) != 0]]></conditionExpression>
        <style mode="Opaque" forecolor="#000000" backcolor="#FFFF00"/>
    </conditionalStyle>
    <conditionalStyle>
        <conditionExpression><![CDATA[($F{CID} % 2) == 0]]></conditionExpression>
        <style forecolor="#000000" backcolor="#FFFFFF"/>
    </conditionalStyle>
</style>

将细节部分中的所有字段放入框架中,并为框架设置此样式


这在iReportUltimateGuide的“使用样式”部分中进行了描述。

图像出现了问题……对不起,是我的错,我看不到图像。哦,你能给我你的电子邮件将图像发送到你的电子邮件吗?这是我的电子邮件地址:rasool_ghafary@yahoo.comIf类别被删除,则此解决方案无法正常工作。例如,如果类别id是这样的:1,2,4使用JR的变量。例如