Ursprungsmitteilung
Thema help with JOptionPanes with XBOX controller 
Autor Matthew Hawk 
Eingangsdatum 2016-05-16 18:58:58.0 
Mitteilung Hello again, I will stick with English this time, but I have another question. Is there a way to add a listener to a JOptionPane. I want to have the option to shift to the different options in a JOptionPane using the joystick. If you can help me that would be awesome!!! Thanks again, Matthew 
 
      
Antworten
Thema Modeless Option Pane 
Autor Aegidius Pluess 
Eingangsdatum 2016-05-17 05:03:45.0 
Mitteilung Hello, you need a modeless dialog. JOptionPane is modal, but you can constuct a modeless dialog quite easily as shown in the following example:

// ModelessPane.java

import java.awt.event.WindowEvent;
import javax.swing.*;

public class ModelessPane extends JDialog
{
// Used in order to call Swing methods from EDT only
private class SetText implements Runnable
{
private String text;
private boolean adjust;

private SetText(String text, boolean adjust)
{
this.text = text;
this.adjust = adjust;
}

public void run()
{
optionPane.setMessage(text);
if (adjust)
pack(); // Adapt size to new message
}
}

private JOptionPane optionPane;
private boolean exitOnClose = true; // adapt as you like

protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
if (exitOnClose)
System.exit(0);
else
dispose();
}
}

public ModelessPane(int ulx, int uly, String text)
{

optionPane = new JOptionPane(text);
optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
optionPane.setOptions(new Object[]
{
});
optionPane.setInitialSelectionValue(null);
optionPane.setIcon(null);
setContentPane(optionPane);
pack();
setLocation(ulx, uly);
setVisible(true);
}

public void setText(String text, boolean adjust)
{
if (SwingUtilities.isEventDispatchThread())
{
optionPane.setMessage(text);
if (adjust)
pack(); // Adapt size to new message
}
else
{
try
{
SwingUtilities.invokeAndWait(new SetText(text, adjust));
}
catch (Exception ex)
{
}
}
}
}


Here a little demonstration how it works:

// ModelessPaneEx.java


public class ModelessPaneEx
{
private static void delay(long timeout)
{
try
{
Thread.sleep(timeout);
}
catch (InterruptedException ex)
{
}
}

public static void main(String[] args)
{
String msg = "#: ";
ModelessPane mop = new ModelessPane(300, 400, msg + "0 ");
for (int nb = 0; nb < 10; nb++)
{
mop.setText(msg + nb, false);
delay(1000);
}
mop.setText("This is the end of the demonstration", true);
}
}


If you call setText() anyway from the EDT (e.g. in a GUI callback), you may omit the class SetText and simplify the code.

Hope this helps.

AP