如何在输入验证期间显示临时气球工具提示?

发布于 2024-11-30 19:08:02 字数 317 浏览 2 评论 0原文

我有点习惯 GUI 习惯用法,当我在文本输入字段中输入错误的内容时,该字段会弹出一个气球,其中包含有关我错误/预期输入的信息。它保持可见,直到我再次输入。 但现在我找不到任何例子可以指出。

给定一个 JTextfield,您是否知道任何库/代码在我的按键侦听器触发时会显示这样的气球?

@see http://www.javapractices.com/topic/TopicAction.do?Id =151

I'm somewhat used to the GUI idiom where when I type something wrong in a text input field a balloon pops up from the field with info about what I got wrong / expected input. It remains visible until I type again.
But now I couldn't find any example to point to.

Given a JTextfield do you know of any library/code that would display such a balloon when triggered by my keylistener?

@see http://www.javapractices.com/topic/TopicAction.do?Id=151

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-12-07 19:08:02

气球提示(可在Maven Central) 是一个可以做到这一点的库:

balloonTip = new BalloonTip(f, "Tooltip msg");

这就是所需要的!如果您坚持将其也作为工具提示:

tooltipBalloon = new BalloonTip(someComponent, "I'm a balloon tooltip!");
// Now convert this balloon tip to a tooltip, such that the tooltip shows up after 500 milliseconds and stays visible for 3000 milliseconds
ToolTipUtils.balloonToToolTip(tooltipBalloon, 500, 3000);

Balloon Tip (available on Maven Central) is a library that does that:

balloonTip = new BalloonTip(f, "Tooltip msg");

That was all needed! If you insist on a having it be a tooltip too:

tooltipBalloon = new BalloonTip(someComponent, "I'm a balloon tooltip!");
// Now convert this balloon tip to a tooltip, such that the tooltip shows up after 500 milliseconds and stays visible for 3000 milliseconds
ToolTipUtils.balloonToToolTip(tooltipBalloon, 500, 3000);
深海夜未眠 2024-12-07 19:08:02

James Poulson 给出的链接可能提供了一个更好的解决方案,但我必须看看这是否可以通过使用 DocumentFilter 和 JWindow 的一些简单 Java 代码来实现。这是一种可能的方法:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class InfoBalloon extends JPanel {
   private static final int PREF_WIDTH = 400;
   private static final int PREF_HEIGHT = 300;
   private static final String REGEX_TEST = "\\d*";
   private static final String ERROR_TEXT = "Please only add numbers to the text field";
   private JTextField textField = new JTextField(10);
   private JWindow errorWindow;


   public InfoBalloon() {
      add(new JLabel("Please Enter Number"));
      add(textField);

      ((PlainDocument)textField.getDocument()).setDocumentFilter(new MyNumberDocFilter());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_WIDTH, PREF_HEIGHT);
   }

   private void showErrorWin() {     
      if (errorWindow == null) {
         JLabel errorLabel = new JLabel(ERROR_TEXT);
         Window topLevelWin = SwingUtilities.getWindowAncestor(this);
         errorWindow = new JWindow(topLevelWin);
         JPanel contentPane = (JPanel) errorWindow.getContentPane();
         contentPane.add(errorLabel);
         contentPane.setBackground(Color.white);
         contentPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
         errorWindow.pack();
      }

      Point loc = textField.getLocationOnScreen();
      errorWindow.setLocation(loc.x + 20, loc.y + 30);
      errorWindow.setVisible(true);
   }

   private boolean textOK(String text) {
      if (text.matches(REGEX_TEST)) {
         return true;
      }
      return false;
   }

   private class MyNumberDocFilter extends DocumentFilter {
      @Override
      public void insertString(FilterBypass fb, int offset, String string,
               AttributeSet attr) throws BadLocationException {
         if (textOK(string)) {
            super.insertString(fb, offset, string, attr);
            if (errorWindow != null && errorWindow.isVisible()) {
               errorWindow.setVisible(false);
            }
         } else {
            showErrorWin();
         }
      }

      @Override
      public void replace(FilterBypass fb, int offset, int length, String text,
               AttributeSet attrs) throws BadLocationException {
         if (textOK(text)) {
            super.replace(fb, offset, length, text, attrs);
            if (errorWindow != null && errorWindow.isVisible()) {
               errorWindow.setVisible(false);
            }
         } else {
            showErrorWin();
         }
      }

      @Override
      public void remove(FilterBypass fb, int offset, int length)
               throws BadLocationException {
         super.remove(fb, offset, length);
         if (errorWindow != null && errorWindow.isVisible()) {
            errorWindow.setVisible(false);
         }
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Info Balloon");
      frame.getContentPane().add(new InfoBalloon());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

欢迎建设性或破坏性的批评!

The link given by James Poulson probably provides a better solution, but I had to see if this were possible with some simple Java code using a DocumentFilter and a JWindow. Here's one possible way to do this:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Window;
import javax.swing.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class InfoBalloon extends JPanel {
   private static final int PREF_WIDTH = 400;
   private static final int PREF_HEIGHT = 300;
   private static final String REGEX_TEST = "\\d*";
   private static final String ERROR_TEXT = "Please only add numbers to the text field";
   private JTextField textField = new JTextField(10);
   private JWindow errorWindow;


   public InfoBalloon() {
      add(new JLabel("Please Enter Number"));
      add(textField);

      ((PlainDocument)textField.getDocument()).setDocumentFilter(new MyNumberDocFilter());
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_WIDTH, PREF_HEIGHT);
   }

   private void showErrorWin() {     
      if (errorWindow == null) {
         JLabel errorLabel = new JLabel(ERROR_TEXT);
         Window topLevelWin = SwingUtilities.getWindowAncestor(this);
         errorWindow = new JWindow(topLevelWin);
         JPanel contentPane = (JPanel) errorWindow.getContentPane();
         contentPane.add(errorLabel);
         contentPane.setBackground(Color.white);
         contentPane.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
         errorWindow.pack();
      }

      Point loc = textField.getLocationOnScreen();
      errorWindow.setLocation(loc.x + 20, loc.y + 30);
      errorWindow.setVisible(true);
   }

   private boolean textOK(String text) {
      if (text.matches(REGEX_TEST)) {
         return true;
      }
      return false;
   }

   private class MyNumberDocFilter extends DocumentFilter {
      @Override
      public void insertString(FilterBypass fb, int offset, String string,
               AttributeSet attr) throws BadLocationException {
         if (textOK(string)) {
            super.insertString(fb, offset, string, attr);
            if (errorWindow != null && errorWindow.isVisible()) {
               errorWindow.setVisible(false);
            }
         } else {
            showErrorWin();
         }
      }

      @Override
      public void replace(FilterBypass fb, int offset, int length, String text,
               AttributeSet attrs) throws BadLocationException {
         if (textOK(text)) {
            super.replace(fb, offset, length, text, attrs);
            if (errorWindow != null && errorWindow.isVisible()) {
               errorWindow.setVisible(false);
            }
         } else {
            showErrorWin();
         }
      }

      @Override
      public void remove(FilterBypass fb, int offset, int length)
               throws BadLocationException {
         super.remove(fb, offset, length);
         if (errorWindow != null && errorWindow.isVisible()) {
            errorWindow.setVisible(false);
         }
      }
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Info Balloon");
      frame.getContentPane().add(new InfoBalloon());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Constructive or destructive criticism is most welcome!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文