在空的JCombobox中添加水印

在空的JCombobox中添加水印

本文介绍了在空的JCombobox中添加水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重现Firefox或Safari的搜索字段或本页右上方stackoverflow.com的搜索字段的行为.

I'm trying to reproduce the behavior of the search field of Firefox or Safari, or the search field of stackoverflow.com on the top right of this page.

我的意思是,当可编辑的JComboBox上没有文本时,将显示一条指令文本,例如"Type here"或其他内容.当JComboBox聚焦时,文本将被删除.如果没有输入任何内容而失去焦点,则说明文本会返回.

I mean, when there is no text on the editable JComboBox, an instruction text is displayed, like "Type here" or whatever. When the JComboBox is focused the text is removed. If the focus is lost with no text typed, the instruction text comes back.

推荐答案

注意:

查看文本提示,以获得更完整的实现下面的代码具有更多功能.

Check out Text Prompt for a more complete implementation of the code below with more features.

这是我放在一起的简单内容.我敢肯定你可以整理一下.由于代码在JTextField上工作,因此您需要获取组合框的编辑器.我对釉面列表的实现方式一无所知,所以我只是猜测它会为您工作.

Here's something simple I threw together. I'm sure you can tidy it up. Since the code works on a JTextField, you would need to get the editor of the combobox. I no nothing about how Glazed lists is implemented so I'm just guessing it will work for you.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class TextPrompt extends JLabel
    implements FocusListener, DocumentListener
{
    private JTextComponent component;
    private Document document;

    public TextPrompt(String text, JTextComponent component)
    {
        this.component = component;
        document = component.getDocument();

        setText( text );
        setFont( component.getFont() );
        setBorder( new EmptyBorder(component.getInsets()) );

        component.addFocusListener( this );
        document.addDocumentListener( this );

        component.add( this );
    }

    public void checkForPrompt()
    {
        if (document.getLength() == 0)
            setSize( component.getSize() );
        else
            setSize(0, 0);
    }

//  Implement FocusListener

    public void focusGained(FocusEvent e)
    {
        checkForPrompt();
    }

    public void focusLost(FocusEvent e)
    {
        setSize(0, 0);
    }

//  Implement DocumentListener

    public void insertUpdate(DocumentEvent e)
    {
        checkForPrompt();
    }

    public void removeUpdate(DocumentEvent e)
    {
        checkForPrompt();
    }

    public void changedUpdate(DocumentEvent e) {}

    public static void main(String[] args)
    {
        JPanel panel = new JPanel();
        JTextField tf1 = new JTextField(10);
        panel.add(tf1);
        JTextField tf2 = new JTextField(10);
        panel.add(tf2);

        new TextPrompt("First Name", tf1);
        new TextPrompt("Last Name", tf2);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

这篇关于在空的JCombobox中添加水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:27