JTextField: Blinkender Unterstrich

XPlayer

Mitglied
Hallo,

ich arbeite gerade an einer kleinen Java-Anwendung, die eine Buchstabenauswahl in einem JTextField nur über das Mausrad ermöglicht (zur nächsten Position wird mittels eines Buttons gesprungen). Nun soll die aktuelle Position im JTextField, also wo gerade der Buchstabe ausgewählt wird, durch einen blinkenden Unterstrich (wie in der Konsole) gekennzeichnet werden.
Hat jemand zumindest einen Ansatz, wie so etwas in Java umsetzbar wäre.

Danke schonmal im Voraus.

Gruß XPlayer
 
Zuletzt bearbeitet:
Eine eigene Implementierung von Caret hilft hier. Und da man die ganz sicher nicht selber machen will, passt man am besten DefaultCaret an:

Java:
final JTextField tf = new JTextField();
final DefaultCaret caret = new DefaultCaret() {

  private static final long serialVersionUID = 1L;

  @Override
  public void paint(Graphics g) {
    if (isVisible()) {
      try {
        final JTextComponent component = getComponent();
        TextUI mapper = component.getUI();
        final int dot = getDot();
        final Bias dotBias = getDotBias();
        Rectangle r = mapper.modelToView(component, dot, dotBias);

        if ((r == null) || ((r.width == 0) && (r.height == 0))) {
          return;
        }
        if (width > 0
              && height > 0
              && !this.contains(r.x, r.y, r.width, r.height)) {
          // We seem to have gotten out of sync and no longer
          // contain the right location, adjust accordingly.
          Rectangle clip = g.getClipBounds();

          if (clip != null && !clip.contains(this)) {
            // Clip doesn't contain the old location, force it
            // to be repainted lest we leave a caret around.
            repaint();
          }
          // This will potentially cause a repaint of something
          // we're already repainting, but without changing the
          // semantics of damage we can't really get around this.
          damage(r);
        }
        g.setColor(component.getCaretColor());
        int paintWidth =
              component.getFontMetrics(component.getFont())
                    .charWidth('M');
        g.fillRect(r.x, r.y, paintWidth, r.height);
      } catch (BadLocationException e) {
        // can't render I guess
        // System.err.println("Can't render cursor");
      }
    }
  }
};
caret.setBlinkRate(300);
tf.setCaret(caret);
Ebenius
 
Hi,

thx für deine Lösung.

Allerdings findet das Blinken über dem gesamten Buchstaben statt und nicht nur als Linie unter ihm.

Vermutlich muss an dieser Anweisung noch etwas an den Parametern verändert werden:
Java:
g.fillRect(r.x, r.y, paintWidth, r.height);

Desweiteren habe ich das Problem, das beim Scrollen der Maus die Animation unterbrochen wird, sprich das schwarze Rechteck ist immer da.

Ich hoffe du kannst mir nochmals weiterhelfen.

Gruß
XPlayer
 
Zuletzt bearbeitet:
Ich wusste's ja immer schon; Textcursor sind einfach kompliziert. So sollte's gehen:

Java:
final DefaultCaret caret = new DefaultCaret() {

  private static final long serialVersionUID = 1L;

  private Timer flasher;
  private FlashHandler flashHandler;
  private boolean hide;

  private volatile int caretHeight = 3; // adjust this value
  private volatile int caretWidth = 0;

  class FlashHandler implements ActionListener, java.io.Serializable {

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("boxing")
    public void actionPerformed(ActionEvent e) {
      hide = !hide;
      System.out.printf("Hide %b%n", hide);
      repaint();
    }
  }

  @Override
  public void install(JTextComponent c) {
    super.install(c);
    flashHandler = new FlashHandler();
    if (flasher != null) {
      flasher.addActionListener(flashHandler);
    }
  }

  @Override
  public void deinstall(JTextComponent c) {
    super.deinstall(c);
    if (flasher != null) {
      flasher.stop();
      flasher.removeActionListener(flashHandler);
    }
    flashHandler = null;
  }

  @Override
  public int getBlinkRate() {
    return (flasher == null) ? 0 : flasher.getDelay();
  }

  @Override
  public void setBlinkRate(int rate) {
    if (rate != 0) {
      if (flasher == null) {
        flasher = new Timer(rate, flashHandler);
      }
      flasher.setDelay(rate);
    } else {
      if (flasher != null) {
        flasher.stop();
        flasher.removeActionListener(flashHandler);
        flasher = null;
      }
    }
  }

  @Override
  public void paint(Graphics g) {
    if (isVisible()) {
      try {
        final JTextComponent component = getComponent();
        TextUI mapper = component.getUI();
        final int dot = getDot();
        final Bias dotBias = getDotBias();
        Rectangle r = mapper.modelToView(component, dot, dotBias);

        if ((r == null) || ((r.width == 0) && (r.height == 0))) {
          return;
        }
        g.setFont(getComponent().getFont());
        caretWidth = g.getFontMetrics().charWidth('M');
        System.out.println("Caret set");
        if (width > 0
              && height > 0
              && !this.contains(r.x, r.y, r.width, r.height)) {
          // We seem to have gotten out of sync and no longer
          // contain the right location, adjust accordingly.
          Rectangle clip = g.getClipBounds();

          if (clip != null && !clip.contains(this)) {
            // Clip doesn't contain the old location, force it
            // to be repainted lest we leave a caret around.
            repaint();
          }

          // This will potentially cause a repaint of something
          // we're already repainting, but without changing the
          // semantics of damage we can't really get around this.
          damage(r);
        }
        if (!hide) {
          g.setColor(component.getCaretColor());
          final int h = Math.min(caretHeight, r.height);
          g.fillRect(r.x, r.y + height - h, caretWidth, h);
        }
      } catch (BadLocationException e) {
        // can't render I guess
        // System.err.println("Can't render cursor");
      }
    }
  }

  @Override
  protected synchronized void damage(Rectangle r) {
    if (r != null) {
      int damageWidth = caretWidth;
      x = r.x;
      y = r.y;
      width = damageWidth;
      height = r.height + caretHeight;
      repaint();
      System.out.println("Damage");
    }
  }

  @Override
  public void setVisible(boolean visible) {
    if (visible) {
      final JTextComponent component = getComponent();
      final Font font = component.getFont();
      caretWidth = component.getFontMetrics(font).charWidth('M');
    }
    super.setVisible(visible);
    if (flasher != null) {
      if (visible) {
        flasher.start();
      } else {
        flasher.stop();
      }
    }
  }
};

Ebenius
 
Hallo Ebenius,

vielen Dank nochmals für deine Hilfe.

Der Code tut im Gegensatz zum ersten leider irgendwie gar nicht, sprich ich erhalte gar keinen Unterstrich, die Ausgaben auf der Konsole erfolgen dagegen. Die Parameter caretHeight (= 3) und caretWidth (= 0) sind dabei unverändert.

Gruß XPlayer
 
Hm. Könnte an fehlender Höhe liegen oder an was anderem. Ich teste auch nur unter Linux. Probier mal meinen gesamten Test:
Java:
/* (@)JCustomCaretTest.java */

/* Copyright 2011 Sebastian Haufe

 * Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       [url]http://www.apache.org/licenses/LICENSE-2.0[/url]

 * Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

package com.ebenius;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.plaf.TextUI;
import javax.swing.text.*;
import javax.swing.text.Position.Bias;

/**
 * TODO: Javadoc me!
 * 
 * @version $Revision$ as of $Date$
 * @author Sebastian Haufe
 * @since Playground-3.8
 */
public class JCustomCaretTest {

  /** Creates the GUI. Call on EDT, only! */
  static void createAndShowGui() {
    final JPanel contentPane = new JPanel(new BorderLayout(6, 6));

    final JTextField tf = new JTextField();
    final DefaultCaret caret = new DefaultCaret() {

      private static final long serialVersionUID = 1L;

      private Timer flasher;
      private FlashHandler flashHandler;
      private boolean hide;

      private volatile int caretHeight = 3; // adjust this value
      private volatile int caretWidth = 0;

      class FlashHandler implements ActionListener, java.io.Serializable {

        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent e) {
          hide = !hide;
          repaint();
        }
      }

      @Override
      public void install(JTextComponent c) {
        super.install(c);
        flashHandler = new FlashHandler();
        if (flasher != null) {
          flasher.addActionListener(flashHandler);
        }
      }

      @Override
      public void deinstall(JTextComponent c) {
        super.deinstall(c);
        if (flasher != null) {
          flasher.stop();
          flasher.removeActionListener(flashHandler);
        }
        flashHandler = null;
      }

      @Override
      public int getBlinkRate() {
        return (flasher == null) ? 0 : flasher.getDelay();
      }

      @Override
      public void setBlinkRate(int rate) {
        if (rate != 0) {
          if (flasher == null) {
            flasher = new Timer(rate, flashHandler);
          }
          flasher.setDelay(rate);
        } else {
          if (flasher != null) {
            flasher.stop();
            flasher.removeActionListener(flashHandler);
            flasher = null;
          }
        }
      }

      @Override
      public void paint(Graphics g) {
        if (isVisible()) {
          try {
            final JTextComponent component = getComponent();
            TextUI mapper = component.getUI();
            final int dot = getDot();
            final Bias dotBias = getDotBias();
            Rectangle r = mapper.modelToView(component, dot, dotBias);

            if ((r == null) || ((r.width == 0) && (r.height == 0))) {
              return;
            }
            g.setFont(getComponent().getFont());
            caretWidth = g.getFontMetrics().charWidth('M');
            if (width > 0
                  && height > 0
                  && !this.contains(r.x, r.y, r.width, r.height)) {
              // We seem to have gotten out of sync and no longer
              // contain the right location, adjust accordingly.
              Rectangle clip = g.getClipBounds();

              if (clip != null && !clip.contains(this)) {
                // Clip doesn't contain the old location, force it
                // to be repainted lest we leave a caret around.
                repaint();
              }

              // This will potentially cause a repaint of something
              // we're already repainting, but without changing the
              // semantics of damage we can't really get around this.
              damage(r);
            }
            if (!hide) {
              g.setColor(component.getCaretColor());
              final int h = Math.min(caretHeight, r.height);
              g.fillRect(r.x, r.y + height - h, caretWidth, h);
            }
          } catch (BadLocationException e) {
            // can't render I guess
            // System.err.println("Can't render cursor");
          }
        }
      }

      @Override
      protected synchronized void damage(Rectangle r) {
        if (r != null) {
          int damageWidth = caretWidth;
          x = r.x;
          y = r.y;
          width = damageWidth;
          height = r.height + caretHeight;
          repaint();
        }
      }

      @Override
      public void setVisible(boolean visible) {
        if (visible) {
          final JTextComponent component = getComponent();
          final Font font = component.getFont();
          caretWidth = component.getFontMetrics(font).charWidth('M');
        }
        super.setVisible(visible);
        if (flasher != null) {
          if (visible) {
            flasher.start();
          } else {
            flasher.stop();
          }
        }
      }
    };
    caret.setBlinkRate(300);
    tf.setCaret(caret);

    contentPane.add(tf);
    final JFrame f = new JFrame("Test Frame: JCustomCaretTest"); //$NON-NLS-1$
    f.setContentPane(contentPane);
    f.setSize(400, 400);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setVisible(true);
  }

  /** @param args ignored */
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        createAndShowGui();
      }
    });
  }
}
Wenns nicht funktioniert, musst du mal einen Debugger anschmeißen. Besonders interessant ist dabei, wann/ob der flasher bei Dir überhaupt gestartet und gestoppt wird.

Ebenius
 
Hallo Ebenius,

inzwischen habe ich herausgefunden, an was es gelegen hat. Du hast im Test-Code das JTextField als BorderLayout.Center festgelegt, es nimmt also den vorhandenen Platz vollständig ein. In meinem Code steht das JTextField im FlowLayout. Da es dort eine viel geringere Höhe besitzt, ist der blickende Strich vermutlich außerdem des sichtbaren Bereichs.

Ich habe nun folgende Zeile
Java:
g.fillRect(r.x, r.y + height - h, caretWidth, h);
durch diese Zeile ersetzt
Java:
g.fillRect(r.x, r.y + height - 3, caretWidth, 2);
und
Java:
private volatile int caretHeight = 1;
dann ist der blinkende Strich sichtbar.

Die Variable h ist damit unnnötig geworden.
Vielleicht hast du aber auch noch eine bessere Lösung...

Übrigens gibt es da noch ein kleines Problem, wenn der Text markiert ist. Es bleibt dann nämlich ein kleiner Punkt übrig (unten rechts), der weiter blinkt.

Aber danke nochmals für die Hilfe!

Gruß
XPlayer
 
Zuletzt bearbeitet:

Zurück
Oben