Java JOptionPane焦点隐藏

Java JOptionPane焦点隐藏,java,swing,joptionpane,Java,Swing,Joptionpane,我使用上面的代码创建一个JOptionPane 焦点绘制在主选定选项上,但我想完全隐藏它。这可能吗?不要使用选项[0]使用null: Object[] options = {"questions", "list"}; Object selection = JOptionPane.showOptionDialog(Main.mWindow, "newDocText", "newDoc", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, n

我使用上面的代码创建一个
JOptionPane


焦点绘制在主选定选项上,但我想完全隐藏它。这可能吗?

不要使用
选项[0]
使用
null

Object[] options = {"questions", "list"};

Object selection = JOptionPane.showOptionDialog(Main.mWindow, "newDocText", "newDoc",
JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
依照

显示选项对话框

Object[] options = {"questions", "list"};

Object selection = JOptionPane.showOptionDialog(Main.mWindow, "newDocText", "newDoc",
JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,null);
initialValue—表示默认选择的对象 对话;只有在使用选项时才有意义;可以为空


对我来说,David Kroukamp的回答仍然会导致第一个按钮聚焦,可能是因为必须始终有一个具有聚焦的组件。以下代码明确地将重点放在JLabel上:

public static int showOptionDialog(Component parentComponent,
                                   Object message,
                                   String title,
                                   int optionType,
                                   int messageType,
                                   Icon icon,
                                   Object[] options,
                                   Object initialValue)
编辑:如果焦点的绘制有问题,在调用setFocusPaint(false)之后,可以将JButtons传递给JOptionPane。您可以这样做:

    JLabel message = new JLabel("newDocText");
    final JOptionPane pane = new JOptionPane(message, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_OPTION, null, options);
    JDialog dialog = pane.createDialog(f, "newDoc");
    message.requestFocus();
    dialog.setVisible(true);
    Object selection = pane.getValue();

但是在这种情况下,您需要自己设置对话框的关闭。我认为这是一个更复杂的解决方案。

很好的回答:-),尽管我通过查看您的答案理解了这个问题,至于OP实际期望的是什么:-)您是否运行了您的答案?对我来说,第一个按钮仍然是选中的-可能是因为总是有一个组件具有焦点。它去除了所选按钮的边框,但是是的,按钮的内部文本看起来仍然具有foucs,但是除非您将有一个带有自己按钮的自定义面板,组件等是关于如何将按钮传递到JOptionPane的最佳方法?添加了(不推荐)答案。如果您真的需要这样做(有焦点但没有绘制),那么我建议您在自定义JDialog中编写所有内容,而不要使用JOptionPane。
    JButton questionsButton = new JButton("questions");
    JButton listButton = new JButton("list");
    questionsButton.setFocusPainted(false);
    listButton.setFocusPainted(false);
    Object[] options = {questionsButton, listButton};