JComboBox没有显示箭头

时间:2012-05-30 20:50:24

标签: java swing layout jcombobox

我一直在搜索这个网站并谷歌搜索我的问题的解决方案,我找不到任何东西。我认为应该只是工作;但事实并非如此。我的JComboBox的箭头图标没有显示,我找不到任何地方将其可见性设置为true。

这是我的代码:

public class Driver implements ActionListener {

private JTextField userIDField;
private JTextField[] documentIDField;
private JComboBox repository, environment;
private JButton close, clear, submit;
private JFrame window;

    public Driver()
    {
    window = makeWindow();
    makeContents(window);
    window.repaint();
    }

    private JFrame makeWindow()
    {
    JFrame window = new JFrame("");
    window.setSize(500,300);
    window.setLocation(50,50);
    window.getContentPane().setLayout(null);
    window.setResizable(false);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    return window;
    }

    private void makeContents(JFrame w)
    {
    makeDropDowns(w);
    w.repaint();
    }

    private void makeDropDowns(JFrame w)
    {       
    String[] repositoryArray = {"Click to select", "NSA", "Finance", "Test"};
    repository = new JComboBox(repositoryArray);
    repository.setSelectedIndex(0);
    repository.addActionListener(this);
    repository.setSize(150,20);
    repository.setLocation(175,165);
    repository.setEditable(false);
    w.add(repository);

    String[] environmentArray = {"Click to select", "Dev", "Test", "Qual"};
    environment = new JComboBox(environmentArray);
    environment.setSelectedIndex(0);
    environment.addActionListener(this);
    environment.setSize(150,20);
    environment.setLocation(175,195);
    //environment.setEditable(false);
    w.add(environment,0);
}

    public void actionPerformed(ActionEvent e) 
    {

    String repositoryID = "null", environmentID = "null";

    if (e.getSource() == repository)
        {
        repositoryID = (String)repository.getSelectedItem();
        }

    if(e.getSource() == environment)
        {
        environmentID = (String)environment.getSelectedItem();
        }
    }
}

以下是问题图片的链接:

image

如果有人能提供帮助那就太棒了。

4 个答案:

答案 0 :(得分:5)

您展示的代码有效,但看起来您正在与封闭容器的默认layout作斗争。在此处,ComboTestJPanel,默认为FlowLayout

附录:一般情况下,使用absolute positioning,如update所示。我已将示例更改为使用GridLayout;注释掉setLayout()来电,以查看默认值FlowLayout

enter image description here

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
* @see https://stackoverflow.com/a/10824504/230513
*/
public class ComboTest extends JPanel {

    private JComboBox repository = createCombo(new String[]{
        "Click to select", "NSA", "Finance", "Test"});
    private JComboBox environment = createCombo(new String[]{
        "Click to select", "Dev", "Test", "Qual"});

    public ComboTest() {
        this.setLayout(new GridLayout(0, 1));
        this.add(repository);
        this.add(environment);
    }

    private JComboBox createCombo(String[] data) {
        final JComboBox combo = new JComboBox(data);
        combo.setSelectedIndex(1);
        combo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(e.getActionCommand()
                    + ": " + combo.getSelectedItem().toString());
            }
        });
        return combo;
    }

    private void display() {
        JFrame f = new JFrame("ComboTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ComboTest().display();
            }
        });
    }
}

答案 1 :(得分:5)

它似乎不是你遇到的问题,但我发现这篇文章是由于同样的箭头问题消失了。

在我的情况下,由于我在.removeAll()上错误地使用JComboBox而不是.removeAllItems(),当我尝试清空并在刷新后重复使用JComboBox时我正在使用的数据。我以为我会把它作为一个答案,以防其他人出于类似的原因遇到这个帖子。

答案 2 :(得分:1)

我有同样的问题。我通过使用以下代码重新验证并重新绘制面板来修复它:

myPanel.revalidate();
myPanel.repaint();

答案 3 :(得分:1)

也许有点晚了,但是对于那些仍在寻找使用JComboBox的简单且安全的方法的人来说,可以使用以下方法:

public class FixedJComboBox<E>
        extends JComboBox<E> {
    // Copied constructors
    public FixedJComboBox() {
        super();
    }
    public FixedJComboBox(ComboBoxModel<E> aModel) {
        super(aModel);
    }
    public FixedJComboBox(E[] items) {
        super(items);
    }
    public FixedJComboBox(Vector<E> items) {
        super(items);
    }

    @Override
    public void setBounds(int x, int y, int width, int height) {
        super.setBounds(x, y, width, height);

        // The arrow is the first (and only) component
        // that is added by default
        Component[] comps = getComponents();
        if (comps != null && comps.length >= 1) {
            Component arrow = comps[0];
            // 20 is the default width of the arrow (for me at least)
            arrow.setSize(20, height);
            arrow.setLocation(width - arrow.getWidth(), 0);
        }
    }
}

here所述,该错误是由于错误地将箭头的位置和大小设置为(0,0)引起的,随后出现了一些重新绘制问题。通过简单地覆盖setBounds()函数,在UI /布局管理器错误地更新了箭头之后,总会纠正该箭头。

此外,由于新组件是在旧组件之后添加的(即索引较高),所以箭头将始终位于数组的第一个元素上(假设您不删除并重新添加箭头)。

此类的缺点是箭头的宽度现在由常量而不是UI /布局管理器确定。