3 Replies Latest reply: Jun 20, 2011 8:56 AM by DarrylBurke RSS

    How to keep a label at the left side of a JScrollPane

    801313
      Hi all

      I am essentially trying to make a text block that doesn't move as the scrollpane scrolls (horizontally in my case)

      Here's my code trying to do this simply using Graphics.drawString (not compilable - tell me if you need something that compiles):
                g.setFont(getTaskFont());
                g.setColor(getTaskLabelForeground());
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                JScrollPane sp = ComponentUtilities.getJScrollPaneAncester(this);
                for (int i = 0; i < tasks.size() - 1; i++) {
                     Task t = tasks.get(i + 1);
                     FontMetrics fm = g.getFontMetrics();
                     int y = i * (rowHeight + rowSpace) + fm.getAscent() + (rowHeight - fm.getHeight()) / 2;
                     int strlen = fm.stringWidth(t.getName().toUpperCase());
                     int start = getXLocationForTime(t.getStart());
                     int end = getXLocationForTime(t.getFinish());
                     int len = end - start;
                     int x = 0;
                     //System.out.println("task: " + t.getName());
                     if (strlen < len) {
                          x = Math.min(end + 5, Math.max(start + 3, sp.getHorizontalScrollBar().getValue()));
                     }
                     else {
                          x = end + 5;
                     }
      
                     g.drawString(t.getName().toUpperCase(), x, y);
                }
      Here's my code trying to do this using a JLabel
                     JScrollPane sp = ComponentUtilities.getJScrollPaneAncester(this);
                     int y = i * (rowHeight + rowSpace); //+ fm.getAscent() + (rowHeight - fm.getHeight()) / 2;
                     int strlen = fm.stringWidth(t.getName().toUpperCase());
                     int start = getXLocationForTime(t.getStart());
                     int end = getXLocationForTime(t.getFinish());
                     int len = end - start;
                     int x = 0;
                     //System.out.println("task: " + t.getName());
                     if (strlen < len) {
                          x = Math.min(end + 5, Math.max(start + 3, sp.getHorizontalScrollBar().getValue()));
                     }
                     else {
                          x = end + 5;
                     }
                     floaters.setLocation(x, y + (rowHeight - fm.getHeight()) / 4);
                     floaters[i].repaint();


      Both [i]almost work
      , but the scrolling is not smooth (blinks back & forth). It seemed like a double buffering problem, but when I added code to paint first to an image and then to screen the issue remained. I also tried adding labels to the JLayeredPane popup layer, but I this solution is problematic also, as labels start drawing all over the place. Plus, I'd rather not have to keep track of the labels and be repositioning them in the first place. Any advice on this is sincerely appreciated. Thanks! -Tom Anyone know how to fix this?