English 中文(简体)
我的JOptionPane与Nimbus laf一道,在从一个 com贝箱中点名时就没有重点。
原标题:With Nimbus laf, my JOptionPane doesn t get focus when called from a combobox ItemEvent

希望你们能够提供帮助。

我希望这只是一个 b,我们能够解决。

I currently have a Java program with a JComboBox. When the user changes the selected item in the combo box a JOptionPane appears to allow the user to confirm the change.

当新选择在 com子箱中进行时,JOptionPane看起来像所希望的那样,但你不得不两次点击使用。 这就是说,把它点击一次,以获得重点,然后点击你想要使用的 but子。 另一种方法是在“全球倡议”方案范围内(在选择Pane方案之后)点击,然后点击纽子。

不存在例外,方案职能一旦被点击,即正常。

这一功能只使用<代码>Nimbus。 Look AndFeel,而不是与本土的aco夫(在玉米、在窗户上测试的 have),但出于其他原因,我需要使用 n。

http://java.net/jira/browse/NIMBUS“rel=“nofollow” 纽布问题跟踪,但尚未发现过错。

I have JButton which calls the SAME code (ie JOptionPane.showConfirmDialog(...) and this works perfectly, it s just when it s called from the action of the combo box.

Really hope you can help! Cheers in advance!

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);
    }
}
最佳回答

不要使用神灵数目,

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

int i = JOptionPane.showConfirmDialog(jComboBox1,

here is w或karound code, but seems like required f或 MetalLookAndFeel, Substance on Windows OS

imp或t javax.swing.UIManager.*;
imp或t javax.swing.*;
imp或t javax.swing.event.PopupMenuEvent;
imp或t 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 {
            f或 (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);
            }
        });
    }
}
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签