没有选择任何内容时如何设置JComboBox的标题? [英] How to set the title of a JComboBox when nothing is selected?

查看:190
本文介绍了没有选择任何内容时如何设置JComboBox的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Swing应用程序中有一个JCombobox,它在未选择任何内容时显示标题.像这样:

I want to have a JCombobox in my Swing application, which shows the title when nothing is selected. Something like this:

国家(地区)▼
西班牙
德国
爱尔兰

COUNTRY ▼
Spain
Germany
Ireland

我希望在所选索引为-1时显示"COUNTRY",因此用户将无法选择它.我试图将其放在第一个插槽中,然后覆盖ListCellRenderer,以便第一个元素显示为灰色,并处理事件,以便在尝试选择标题"时选择第一个实际元素,但是我认为这很脏方法.

I want "COUNTRY" to show when the selected index is -1 and thus, the user wouldn't be able to select it. I tried to put it on the first slot and then overriding the ListCellRenderer so the first element appears greyed out, and handling the events so when trying to select the "title", it selects the first actual element, but I think this is a dirty approach.

你能帮我一下吗?

推荐答案

覆盖ListCellRenderer是一个很好的方法,但是您尝试了一些过于复杂的操作.如果要渲染单元格-1且没有选择(值是null),则仅显示特定字符​​串.您不仅限于显示列表中的元素.

Overriding the ListCellRenderer is a good approach, but you tried something overly complicated. Just display a certain string if you are rendering the cell -1 and there is no selection (value is null). You are not limited to display elements on the list.

下面是一个演示它的示例程序:

The following is an example program that demonstrates it:

完整代码:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;

public class ComboBoxTitleTest
{
    public static final void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run()
            {
                new ComboBoxTitleTest().createAndShowGUI();
            }
        });
    }

    public void createAndShowGUI()
    {
        JFrame frame = new JFrame();

        JPanel mainPanel = new JPanel();
        JPanel buttonsPanel = new JPanel();
        frame.add(mainPanel);
        frame.add(buttonsPanel, BorderLayout.SOUTH);

        String[] options = { "Spain", "Germany", "Ireland", "The kingdom of far far away" };

        final JComboBox comboBox = new JComboBox(options);
        comboBox.setRenderer(new MyComboBoxRenderer("COUNTRY"));
        comboBox.setSelectedIndex(-1); //By default it selects first item, we don't want any selection
        mainPanel.add(comboBox);

        JButton clearSelectionButton = new JButton("Clear selection");
        clearSelectionButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                comboBox.setSelectedIndex(-1);
            }
        });
        buttonsPanel.add(clearSelectionButton);

        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    class MyComboBoxRenderer extends JLabel implements ListCellRenderer
    {
        private String _title;

        public MyComboBoxRenderer(String title)
        {
            _title = title;
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean hasFocus)
        {
            if (index == -1 && value == null) setText(_title);
            else setText(value.toString());
            return this;
        }
    }
}

渲染器中的

index == -1是head组件,默认情况下,其显示所选项目以及在没有选择的情况下我们想要放置标题的位置.

index == -1 in the renderer is the head component that, by default, displays the selected item and where we want to put our title when there's no selection.

渲染器知道没有选择任何东西,因为传递给它的值是null,通常是这种情况.但是,如果出于某些奇怪的原因,您的列表中有可选择的null值,则可以通过将其传递给comboBox的引用,让渲染器查阅这是显式当前选择的索引,但这是完全不现实的.

The renderer knows that there's nothing selected because the value passed to it is null, which is usually the case. However if for some weird reasons you had selectable null values in your list, you can just let the renderer consult which is the explicit current selected index by passing it a reference to the comboBox, but that is totally unrealistic.

这篇关于没有选择任何内容时如何设置JComboBox的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆