Domanda

Sto sviluppando un'applicazione di rendering XFDL in Java. XFDL è una lingua di definizione della forma che è un'estensione di XML.

La classe di rendering utilizza STAX per elaborare il documento e aggiungere gli elementi richiesti a un JPanel personalizzato per la visualizzazione. Ho avuto successo nell'aggiunta di campi correttamente (JTextfields) e nelle etichette (Jlabels) alle linee JPanel sono diventate un problema. Quando costruisci un modulo con più pagine, quando mai visualizzo il modulo ogni pagina ottiene le righe dell'ultima pagina nel modulo.

Ho esaminato il mio codice (mostrato di seguito) e non riesco a trovare una causa di questo comportamento. Quando passo, tutti i contatori aumentano correttamente in modo che le righe vengano aggiunte alle pagine appropriate, ma ottengo sempre l'ultima pagina di righe dietro la pagina corrente di etichette e campi.

Sospetto di aver frainteso qualcosa su come Java gestisce il disegno, ma non sono sicuro di cosa.

Grazie in anticipo per le risposte e i consigli.

Nelle linee del documento XFDL sono definite come tali:

<page SID="PAGE1">
      <line sid="LINE1">
         <itemlocation>
            <ae>
               <ae>absolute</ae>
               <ae>1219</ae>
               <ae>75</ae>
            </ae>
            <ae>
               <ae>extent</ae>
               <ae>3</ae>
               <ae>854</ae>
            </ae>
         </itemlocation>
      </line>
      ...
</page>
<page SID="PAGE2">
      <line sid="LINE2">
         <itemlocation>
            <ae>
               <ae>absolute</ae>
               <ae>1219</ae>
               <ae>75</ae>
            </ae>
            <ae>
               <ae>extent</ae>
               <ae>3</ae>
               <ae>854</ae>
            </ae>
         </itemlocation>
      </line>
      ...
</page>

Quando la classe di rendering raggiunge un tag, viene chiamato il seguente metodo:

private ArrayList<FormPanel> pages;

/**
 * Draws a new line on the Panel
 * 
 * Start State: Cursor is positioned on the <line> start element.
 * End State: Cursor is positioned on the <line> end element.
 * 
 * @throws XMLStreamException 
 */
private void addLine() throws XMLStreamException {
    while(reader.hasNext() &&
            !(reader.isEndElement() && 
                    reader.getLocalName().equals(XFDL_LINE))) {

        reader.next();
        if(reader.isStartElement()) {
            if(reader.getLocalName().equals(XFDL_ITEMLOCATION)) {
                pages.get(currentPage).addLine(processItemLocation());
            }
        }
    }

}

/**
 * Processes an item location field into a Rectangle object used to set
 * the bounds and location of form item.
 * Start State: Cursor positioned on the itemlocation start element.
 * End State: Cursor positioned on the itemlocation end element
 * 
 * Currently only handles absolute positioning and extent sizes.
 * Any other form of positioning data returns null.
 * 
 * @return Rectangle representing the location and size of item.
 * @throws XMLStreamException 
 */
private Rectangle processItemLocation() throws XMLStreamException {
    Rectangle result = new Rectangle();

    ArrayList<String> attributes = new ArrayList<String>();

    while(reader.hasNext() &&
            !(reader.isEndElement() && 
                    reader.getLocalName().equals(XFDL_ITEMLOCATION)))
    {
        reader.next();
        if(reader.isCharacters() && !reader.isWhiteSpace()) {
            attributes.add(reader.getText());
        }
    }

    for(int x=0; x<attributes.size(); x++) {
        if(attributes.get(x).equals(XFDL_LOCATION_STYLE_ABSOLUTE)) {
            result.setLocation(Integer.parseInt(attributes.get(x+1)),
                    Integer.parseInt(attributes.get(x+2)));
        } else if(attributes.get(x).equals(XFDL_LOCATION_SIZE_EXTENT)) {
            result.setSize(Integer.parseInt(attributes.get(x+1)),
                    Integer.parseInt(attributes.get(x+2)));
        } else {
            try{
                Integer.parseInt(attributes.get(x));
            } catch (NumberFormatException nfe) {
                result = null;
                x = attributes.size();
            }
        }
    }

    return result;
}

FormPanel è un'estensione di jpanel con un metodo aggiuntivo addLine () e un paintomponent () sopravvalutato mostrato di seguito:

private static ArrayList<Rectangle> lines;

public void addLine(Rectangle line) {
    lines.add(line);
}

/**
 * Overrides JPanel paintComponenet and draws lines on the form.
 */
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    for(int x = 0; x<lines.size(); x++) {
        g.drawRect(lines.get(x).x, 
                lines.get(x).y, 
                lines.get(x).width, 
                lines.get(x).height);
        g.fillRect(lines.get(x).x, 
                lines.get(x).y, 
                lines.get(x).width, 
                lines.get(x).height);
    }
}

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top