문제

JPanel을 확장하는 클래스(Timeline)가 있습니다.타임라인 패널에는 수동으로 배치되는("null-Layout") JLabel(녹색 및 주황색 요소)이 많이 포함되어 있습니다.타임라인 상단에는 월 간 전환을 위한 몇 가지 버튼이 있습니다.때로는 달을 전환할 때 스윙이 JLabels를 칠하지 않지만 항상 그리드 배경을 칠합니다.

나는 이미 많은 "마법의" 방법(다시 그리기, 유효성 다시 검사, 무효화, 유효성 검사, updateUI)을 시도했습니다.

타임라인을 성공적으로 그렸습니다:

Timeline succesful

페인팅 실패:

Timeline failed

간단한 예:

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);
   }
}
도움이 되었습니까?

해결책 2

다음 순서로 다음 방법을 호출하면 문제가 해결되었습니다.

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

다른 팁

대안으로 다음을 고려하십시오. org.jfree.chart.renderer.category.GanttRenderer, 이는 시간 영역 차트에 이상적이며 다양한 사용자 정의를 허용합니다.아래 그림과 같은 예를 찾을 수 있습니다. 여기.

Gantt Subtasks

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top