2011-08-21 31 views
10

Tôi phần nào được sử dụng cho thành ngữ GUI khi tôi nhập sai thứ gì đó vào trường nhập văn bản một quả bóng bật lên từ trường có thông tin về những gì tôi nhận được sai/đầu vào được mong đợi. Nó vẫn hiển thị cho đến khi tôi gõ lại. Nhưng bây giờ tôi không thể tìm thấy bất kỳ ví dụ để trỏ đến.Làm thế nào để hiển thị chú giải công cụ baloon tạm thời trong quá trình xác thực đầu vào?

Cho một JTextfield bạn có biết bất kỳ thư viện/mã nào sẽ hiển thị quả bóng như vậy khi được trình kích hoạt khóa của tôi kích hoạt không?

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

+1

liên quan: http://stackoverflow.com/questions/5852941/popup-messages-for-validation –

Trả lời

11

Balloon Tip là một thư viện nào đó:

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

Đó là tất cả cần thiết! Nếu bạn nhấn mạnh vào một việc có nó là một tooltip quá:

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); 
+0

cảm ơn liên kết +1 – mKorbel

+1

Thư viện tuyệt vời! –

6

Các liên kết đưa ra bởi James Poulson lẽ cung cấp một giải pháp tốt hơn, nhưng tôi đã để xem nếu điều này là có thể với một số mã Java đơn giản sử dụng một DocumentFilter và JWindow. Dưới đây là một cách có thể thực hiện việc này:

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(); 
     } 
     }); 
    } 
} 

Lời chỉ trích xây dựng hoặc phá hoại được hoan nghênh nhất!

+1

Khá tốt. Điều duy nhất tôi sẽ thay đổi cho mã sản xuất là tôi sẽ đảm bảo JWindow giữ vị trí tương đối của nó với JFrame cố định bằng cách tạo JWindow thành ComponentAdapter (ghi đè 'componentMoved') đã đăng ký với JFrame. – toto2

+0

@ toto2: cảm ơn vì đề xuất! –

+0

phaaa cửa sổ của bạn có thể dính vào bố cục :-) +1 – mKorbel

Các vấn đề liên quan