2011-08-21 3 views
10

मुझे कुछ हद तक जीयूआई मुहावरे में उपयोग किया जाता है, जहां जब मैं टेक्स्ट इनपुट फ़ील्ड में कुछ गलत टाइप करता हूं तो एक गुब्बारा क्षेत्र से पॉप अप करता है जिसके बारे में मुझे गलत/अपेक्षित इनपुट मिलता है। जब तक मैं फिर से टाइप नहीं करता तब तक यह तब तक दिखाई देता है। लेकिन अब मुझे इंगित करने के लिए कोई उदाहरण नहीं मिला।इनपुट सत्यापन के दौरान एक अस्थायी बालन टूलटिप कैसे प्रदर्शित करें?

एक जेटीक्स्टफील्ड को देखते हुए क्या आप किसी भी लाइब्रेरी/कोड के बारे में जानते हैं जो मेरे कीलिस्टलिस्ट द्वारा ट्रिगर किए जाने पर ऐसा गुब्बारा प्रदर्शित करेगा?

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

+1

संबंधित: http://stackoverflow.com/questions/5852941/popup-messages-for-validation –

उत्तर

11

Balloon Tip एक पुस्तकालय है कि कि है!

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

लिंक +1 – mKorbel

+1

बहुत बढ़िया लाइब्रेरी के लिए धन्यवाद! –

6

लिंक जेम्स Poulson द्वारा दिए गए शायद एक बेहतर समाधान प्रदान करता है, लेकिन मैं यह देखने के लिए किया था कि अगर यह कुछ सरल जावा एक DocumentFilter और एक JWindow का उपयोग कर कोड के साथ संभव थे। ऐसा करने का एक संभावित तरीका यहां दिया गया है:

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

रचनात्मक या विनाशकारी आलोचना का स्वागत है!

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

बस इतना ही जरूरत थी:

+1

सुंदर अच्छा आप एक होने यह भी एक टूलटिप हो पर जोर देते हैं। उत्पादन कोड के लिए मैं केवल एक ही चीज बदलूंगा कि मैं यह सुनिश्चित कर दूंगा कि जेडविंडो जेएफआरएएम के साथ पंजीकृत जेडविंडो को कंपोनेंट एडाप्टर (ओवरराइडिंग 'घटक संशोधित') द्वारा तय किए गए जेएफआरएएम को अपनी सापेक्ष स्थिति रखेगा। – toto2

+0

@ toto2: सिफारिश के लिए धन्यवाद! –

+0

phaaa आपकी खिड़की शायद माता-पिता से चिपक रही है :-) +1 – mKorbel

संबंधित मुद्दे