【问题标题】:With Nimbus laf, my JOptionPane doesn't get focus when called from a combobox ItemEvent使用 Nimbus laf,我的 JOptionPane 在从组合框 ItemEvent 调用时没有获得焦点
【发布时间】:2011-11-10 08:08:43
【问题描述】:

希望你能帮忙。

我希望这不仅仅是一个错误,我们可以解决它。

我目前有一个带有JComboBox 的Java 程序。当用户更改组合框中的选定项目时,会出现 JOptionPane 以允许用户确认更改。

在组合框中进行新选择时,JOptionPane 会根据需要显示,但您必须单击两次才能使用它。也就是说,单击一次以获得焦点,然后单击要使用的按钮。另一种方法是在程序的 GUI 范围内(在 optionPane 后面)单击,然后单击按钮。

没有异常发生,点击按钮后程序正常运行。

此功能仅在使用 Nimbus LookAndFeel 时出现,而不是在原生 macos laf 中出现(在 mac 上构建,尚未在 windows 上测试),但出于其他原因我需要使用 nimbus。

我一直在查看Nimbus issue tracking,但还没有找到故障。

我有调用相同代码(即JOptionPane.showConfirmDialog(...)的 JButton,它工作得很好,只是当它从组合框的操作中调用时。

真的希望你能帮上忙! 提前干杯!

import javax.swing.UIManager.*;
import javax.swing.*;

public class TestJavaProblem extends JFrame {
    JComboBox jComboBox1;

    public TestJavaProblem() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents() {
        jComboBox1 = new JComboBox();

        //give it some values
        jComboBox1.setModel(new DefaultComboBoxModel(new String[] { "1", "2"}));

        //add listener
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) { fireTask(evt);}
        });

        this.add(jComboBox1);
        pack();
    }

    private void fireTask(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == 1) { //so dialog fires only once
            int i = JOptionPane.showConfirmDialog(jComboBox1, "Message Text", "Title", JOptionPane.OK_CANCEL_OPTION);
            System.out.println("Result:" + i);
        }
    }

    public static void main(String args[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                break;
                }
            }
        } catch (Exception e) {/*no nimbus*/}

        new TestJavaProblem().setVisible(true);
    }
}

【问题讨论】:

  • 能否请您发布产生此问题的代码(在sscce.org 表单中),因为有一些方法,如何正确地做到这一点
  • 添加了代码。队友的欢呼声。实际上,在我看来,第一次单击将焦点从仍然打开的弹出菜单中移开。其他 l-a-f 几乎立即关闭他们的弹出菜单。

标签: java swing focus joptionpane nimbus


【解决方案1】:

不要使用幻数,

if (evt.getStateChange() == 1) { //so dialog fires only once

int i = JOptionPane.showConfirmDialog(jComboBox1,

这是解决方法代码,但似乎是 MetalLookAndFeel、Windows 操作系统上的 Substance 所必需的

import javax.swing.UIManager.*;
import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class TestJavaProblem extends JFrame {

    private static final long serialVersionUID = 1L;
    private JComboBox jComboBox1;
    private boolean boloComboBox = false;

    public TestJavaProblem() {
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        initComponents();
    }

    private void initComponents() {
        jComboBox1 = new JComboBox();
        jComboBox1.setModel(new DefaultComboBoxModel(new String[]{"1", "2"}));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {

            @Override
            public void itemStateChanged(final java.awt.event.ItemEvent evt) {
                if (jComboBox1.isPopupVisible()) {
                    jComboBox1.setPopupVisible(false);
                    SwingUtilities.invokeLater(new Runnable() {

                        @Override
                        public void run() {
                            fireTask(evt);
                        }
                    });
                }
            }
        });
        jComboBox1.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                System.out.println(e.getSource());
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                System.out.println(e.getSource());
            }

            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                System.out.println(e.getSource());
            }
        });
        add(jComboBox1);
        pack();
    }

    private void fireTask(java.awt.event.ItemEvent evt) {
        if (evt.getStateChange() == 2) {
            int i = JOptionPane.showConfirmDialog(jComboBox1, 
                "Message Text", "Title", JOptionPane.OK_CANCEL_OPTION);
            System.out.println("Result:" + i);
        }
    }

    public static void main(String args[]) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestJavaProblem().setVisible(true);
            }
        });
    }
}

【讨论】:

  • 干杯,这个代码单独修复了它! if (jComboBox1.isPopupVisible()){jComboBox1.setPopupVisible(false);} 不使用 int i = JOptionPane.showConfirmDialog... 是什么意思?
  • int iJOptionPane.YES_OPTIONif (evt.getStateChange() == 1)SELECTED,很高兴为您提供帮助 +1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多