2012-01-24 2 views
0

J'ai une classe (Chronologie) qui étend JPanel. Le panneau Montage contient de nombreux JLabels (éléments verts et oranges) qui sont positionnés manuellement ("null-Layout"). En haut de la Timeline, des boutons permettent de passer d'un mois à l'autre. Parfois, lorsque je passe d'un mois à l'autre, le swing ne peint pas les JLabels mais peint toujours le fond de la grille.Peinture dans Swing

J'ai déjà essayé de nombreuses méthodes "magiques" (repeindre, revalider, invalider, valider, mettre à jourUI).

Timeline avec succès peint:

Timeline succesful

Échec peinture:

Timeline failed

Un court exemple:

public interface IDateSelectorRegistrar { 

    void addListener(DateSelectorListener listener); 

    void removeListener(DateSelectorListener listener); 
} 

public interface DateSelectorListener { 
    void dateChanged(Timestamp from, Timestamp to); 
} 

public interface ITimelineModel { 
    Timespan[] getTimespans(Timestamp from, Timestamp to); 
} 

public class Timespan { 
    private String title; 
    private Timestamp to; 
    private Timestamp from; 

    public Timespan(String title, Timestamp from, Timestamp to) { 
    this.title = title; 
    this.from = from; 
    this.to = to; 
    } 

    // setters and getters 
} 

public class TimelineLabel extends JLabel { 
    public TimelineLabel(Timespan timespan) { 
    super(timespan.getTitle()); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
    // paint colored background 
    super.paintComponent(g); 
    } 
} 

public class Timeline extends JPanel { 

    public Timeline(final ITimelineModel model, IDateSelectorRegistrar registrar) { 
    registrar.addListener(new DateSelectorListener() { 
     public void dateChanged(Timestamp from, Timestamp to) { 
     Timeline.this.removeAll(); 
     Timespan[] timespans = model.getTimespans(from, to); 
     for(Timespan timespan : timespans) { 
      TimelineLabel label = new TimelineLabel(timespan); 
      Timeline.this.add(label); 
      // label positioning because of Timestamp object data 
     } 
     // repaint of timeline 
     Timeline.this.invalidate(); 
     Timeline.this.repaint(); 
     } 
    }); 
    } 

    @Override 
    protected void paintComponent(Graphics g) { 
     // paint background grid 
     super.paintComponent(g); 
    } 
} 
+0

Lorsque la peinture tombe en panne, avez-vous vérifié si votre peinture pertinente/méthode paintComponent est appelée? – ARRG

+0

@endian Je n'ai jamais vu ça, avez-vous placé ces JLabels dans le JPanel par des implements Insets ???, – mKorbel

+0

La méthode paintComponent des JLabels n'est pas appelée. – endian

Répondre

0

appel de méthodes suivantes dans l'ordre suivant résolu le problème:

invalidate(); 
repaint(); 
validate();