【问题标题】:Is this the intention behavior in JComboBox? How I can avoid this behavior?这是 JComboBox 中的意图行为吗?我怎样才能避免这种行为?
【发布时间】:2010-05-08 15:23:12
【问题描述】:

我意识到,如果您在 JComboBox 中有相同的选择,使用向上/向下箭头键将无法帮助您浏览选择。如何避免这种行为?

请看下面的截图:

alt text http://sites.google.com/site/yanchengcheok/Home/jcombobox.png

public class NewJFrame extends javax.swing.JFrame {

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();

        /* If you are having 3 same strings here. Using, up/down arrow key,
         * will not move the selection around.
         */
        this.jComboBox1.addItem("Intel");
        this.jComboBox1.addItem("Intel");
        this.jComboBox1.addItem("Intel");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jComboBox1.setEditable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(105, 105, 105)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(137, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(217, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration                   
}

【问题讨论】:

  • 作为记录,在我的 Mac 上的 JRE 1.6 上运行此代码,向上/向下键工作正常。
  • 我刚刚在带有 JRE 1.6 的 Ubuntu 10.04 中使用它,你是对的,向上和向下键不起作用。如果您更改它们,向上/向下键会起作用,但如果它们相同则不会。奇怪......我会继续玩它。
  • 补充我上面的评论:向上/向下确实有效,但如果您选择一项并重新打开组合框,则无法正确记住选择。

标签: java swing jcombobox


【解决方案1】:

退出cross posting

那个帖子里已经给出了答案。

【讨论】:

  • 是的,我就是这样知道它是交叉发布的。我花时间在许多论坛上,并尝试在发现交叉海报时发出警告,这样我们就不会浪费时间回答已经回答的问题。然后下次 OP 这样做时,我会忽略这个问题并警告大家,希望他们也忽略它。
  • 如果您在此处复制您的答案,我会更愿意为您投票。 :) (是的,这会违反 DRY,但最好在 SO 上找到答案。我想我倾向于认为 Sun 更有可能......呃,Oracle Java 论坛从网络比 SO...)
  • @Jonik,这就是我鄙视交叉海报的原因。所有信息都应该放在一个地方,以便讨论可以在一个地方进行。因此,解决方案应该只在一个地方,以防有人想要制作额外的 cmets。将代码和 cmets 放在两个不同的地方太难管理了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多