下面列出了怎么用javax.swing.JDialog的API类实例代码及写法,或者点击链接到github查看源代码。
@Before
public void initialize() throws Exception {
settings = TestObjectFactory.ZKN3Settings.ZKN3_TRICKY_MARKDOWN;
daten = TestObjectFactory.getDaten(settings);
JDialog parent = null;
Application app = org.jdesktop.application.Application
.getInstance(ZettelkastenApp.class);
JLabel label = new JLabel();
TasksData td = null;
DesktopData dt = null;
File fp = null;
BibTex bto = null;
ArrayList<Object> ee = null;
int type = 0;
int part = 0;
DefaultMutableTreeNode n = null;
boolean bibtex = false;
boolean ihv = false;
boolean numberprefix = false;
boolean contenttable = false;
exportToTexTask = new ExportToTexTask(app, parent, label, td, daten,
dt, settings.settings, bto, fp, ee, type, part, n, bibtex, ihv,
numberprefix, contenttable, false);
}
public void switchTo() throws Throwable {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
d2 = new JDialog(frame, false);
d2.setName("dialog-2");
d2.setTitle("My Dialog 2");
JButton b = new JButton("Click-Me-2");
b.setName("click-me-2");
d2.getContentPane().add(b);
d2.pack();
d2.setVisible(true);
}
});
Collection<String> windowHandles = driver.getWindowHandles();
AssertJUnit.assertEquals(2, windowHandles.size());
driver.switchTo().window("dialog-2");
AssertJUnit.assertNotNull(driver.findElementByName("click-me-2"));
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
d2.dispose();
}
});
}
private static JDialog createInformationDialog(final JComponent component, final String title) {
final JDialog dialog = new JDialog((JFrame) null, title);
dialog.add(component, BorderLayout.CENTER);
final JPanel buttons = new JPanel();
final JButton button =
new JButton(
SwingAction.of(
"OK",
event -> {
dialog.setVisible(false);
dialog.removeAll();
dialog.dispose();
}));
buttons.add(button);
dialog.getRootPane().setDefaultButton(button);
dialog.add(buttons, BorderLayout.SOUTH);
dialog.pack();
dialog.addWindowListener(
new WindowAdapter() {
@Override
public void windowOpened(final WindowEvent e) {
button.requestFocus();
}
});
return dialog;
}
public static void main(String[] args) throws Exception {
JColorChooser chooser = new JColorChooser();
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
JDialog dialog = show(chooser);
pause(DELAY);
dialog.dispose();
pause(DELAY);
Test4177735 test = new Test4177735();
SwingUtilities.invokeAndWait(test);
if (test.count != 0) {
throw new Error("JColorChooser leaves " + test.count + " threads running");
}
}
public static boolean showDlg(final JDialog dialog) {
if (SwingUtilities.isEventDispatchThread()) {
dialog.setVisible(true);
} else {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
dialog.setVisible(true);
}
});
} catch (Exception ex) {
System.err.println("Show dialog error: " + ex);
return false;
}
}
return true;
}
/**
* Shows this panel in a modal dialog.
*
* @param parent The dialog parent window.
* @return New password entered by user, otherwise null if the window is closed.
*/
private Optional<String> show(final Window parent) {
dialog = new JDialog(JOptionPane.getFrameForComponent(parent), "", true);
dialog.getContentPane().add(this);
SwingKeyBinding.addKeyBinding(this, KeyCode.ESCAPE, this::close);
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
dialog.dispose();
dialog = null;
if (!validatePasswordsAndUpdateValidationText()) {
return Optional.empty();
}
final char[] password = passwordField.getPassword();
if (rememberPassword.isSelected()) {
ClientSetting.lobbySavedPassword.setValueAndFlush(password);
} else {
ClientSetting.lobbySavedPassword.resetValue();
}
return Optional.of(Sha512Hasher.hashPasswordWithSalt(String.valueOf(password)));
}
private void initComponents() {
ExtractDialog _this = this;
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
// setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setIconImage(MainWin.icon);
setSize(820, 460);
// setResizable(false);
setLocationRelativeTo(this.getParent());
setTitle("请选择压缩包中要保存的字幕文件");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
_this.clear();
_this.dispose();
}
});
add(bsPanel, BorderLayout.CENTER);
openUrl(MainWin.class.getResource("/html/extract_dialog.html").toExternalForm());
}
public static void main(String[] args) throws Exception {
JColorChooser chooser = new JColorChooser();
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
JDialog dialog = show(chooser);
pause(DELAY);
dialog.dispose();
pause(DELAY);
Test4177735 test = new Test4177735();
SwingUtilities.invokeAndWait(test);
if (test.count != 0) {
throw new Error("JColorChooser leaves " + test.count + " threads running");
}
}
public void open(Wandora w, Context c) {
context = c;
wandora = w;
accepted = false;
dialog = new JDialog(w, true);
dialog.setSize(550, 500);
dialog.add(this);
dialog.setTitle("Mashape API extractor");
UIBox.centerWindow(dialog, w);
if(apikey != null){
forgetButton.setEnabled(true);
} else {
forgetButton.setEnabled(false);
}
dialog.setVisible(true);
}
public static void main(String[] args) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
System.out.println(
"Metal Look & Feel not supported on this platform. \n"
+ "Program Terminated");
System.exit(0);
}
JFrame frame = new MetalworksFrame();
frame.setVisible(true);
}
public static void main(String[] args) {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame.setDefaultLookAndFeelDecorated(true);
Toolkit.getDefaultToolkit().setDynamicLayout(true);
System.setProperty("sun.awt.noerasebackground", "true");
try {
UIManager.setLookAndFeel(new MetalLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
System.out.println(
"Metal Look & Feel not supported on this platform. \n"
+ "Program Terminated");
System.exit(0);
}
JFrame frame = new MetalworksFrame();
frame.setVisible(true);
}
public void testYesDialogShown() throws Exception {
Frame mainWindow = WindowManager.getDefault().getMainWindow();
final JDialog modalDialog = new HiddenDialog( mainWindow, true );
DD.toReturn = modalDialog;
Logger l = Logger.getLogger(getName());
l.setLevel(Level.ALL);
System.setProperty("netbeans.exception.report.min.level", "200");
l.log(Level.CONFIG, "Something is wrong", new NullPointerException("npe"));
waitEQ();
assertNotNull("Really returned", DD.lastDescriptor);
assertEquals("It is DialogDescriptor", DialogDescriptor.class, DD.lastDescriptor.getClass());
DialogDescriptor dd = (DialogDescriptor)DD.lastDescriptor;
assertFalse( "The request is for non-modal dialog", dd.isModal());
assertFalse("Main window is not visible", mainWindow.isVisible());
}
public BrowseButton(final JDialog parentDialog, final JTextField targetField) {
super("Browse ...");
addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
int returnVal = fileChooser.showOpenDialog(parentDialog);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
targetField.setText(fileChooser.getSelectedFile().getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
public RedisRefreshService(RedisTreeTableModel treeTableModel, String host, int port) {
this.treeTableModel = treeTableModel;
this.host = host;
this.port = port;
panel = new JXPanel();
panel.setLayout(new MigLayout("wrap 1"));
panel.add(label, "growx, wrap 20");
panel.add(progressBar, "grow, push");
dialog = new JDialog();
dialog.add(panel);
dialog.setSize(300, 120);
Point p = WindowUtils.getPointForCentering(dialog);
dialog.setLocation(p);
dialog.setModal(true);
dialog.setResizable(false);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
}
@Test
public void testCodeUnitIgnore() throws Exception {
openDiff(diffTestP1, diffTestP2);
JDialog dialog = waitForJDialog(tool.getToolFrame(), "Memory Differs", 2000);
assertNotNull(dialog);
pressButtonByText(dialog, "OK");
waitForPostedSwingRunnables();
showApplySettings();
ProgramSelection origDiffs = diffPlugin.getDiffHighlightSelection();
ignore(codeUnitApplyCB);
AddressSet as = new AddressSet(addr("10024b8"), addr("10024b8"));
setDiffSelection(as);
apply();
assertEquals(origDiffs, diffPlugin.getDiffHighlightSelection());
}
@Test
public void testPropertyIgnore() throws Exception {
openDiff(diffTestP1, diffTestP2);
JDialog dialog = waitForJDialog(tool.getToolFrame(), "Memory Differs", 2000);
assertNotNull(dialog);
pressButtonByText(dialog, "OK");
waitForPostedSwingRunnables();
showApplySettings();
ProgramSelection origDiffs = diffPlugin.getDiffHighlightSelection();
ignore(propertiesApplyCB);
AddressSet as = new AddressSet(addr("100248c"), addr("100248e"));
setDiffSelection(as);
apply();
assertEquals(origDiffs, diffPlugin.getDiffHighlightSelection());
}
public static void main(String[] args) throws Exception {
JColorChooser chooser = new JColorChooser();
AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[1] });
JDialog dialog = show(chooser);
pause(DELAY);
dialog.dispose();
pause(DELAY);
Test4177735 test = new Test4177735();
SwingUtilities.invokeAndWait(test);
if (test.count != 0) {
throw new Error("JColorChooser leaves " + test.count + " threads running");
}
}
public static void main(final String[] args)
throws InvocationTargetException, InterruptedException {
SwingUtilities.invokeAndWait(() -> {
UIDefaults ui = UIManager.getDefaults();
ui.put("control", new ColorUIResource(54, 54, 54));
final JDialog dialog = new JDialog();
final JFrame frame = new JFrame();
frame.pack();
dialog.pack();
final Color dialogBackground = dialog.getBackground();
final Color frameBackground = frame.getBackground();
frame.dispose();
dialog.dispose();
if (!dialogBackground.equals(frameBackground)) {
System.err.println("Expected:" + frameBackground);
System.err.println("Actual:" + dialogBackground);
throw new RuntimeException("Wrong background color");
}
});
}
public Dialog createDialog(DialogDescriptor descriptor) {
lastDescriptor = descriptor;
return new JDialog() {
@SuppressWarnings("deprecation")
@Override
public void show() {}
};
}
/**
* Add a property change listener to the root pane.
*
* @param listener the propertiy change listener to add.
*/
private void addParentPropertyChangeListener(PropertyChangeListener listener) {
if (rootParent instanceof JFrame) {
((JFrame) rootParent).addPropertyChangeListener(listener);
} else if (rootParent instanceof JDialog) {
((JDialog) rootParent).addPropertyChangeListener(listener);
}
rootPane.addPropertyChangeListener(listener);
}
/**
* Empties the cache: deletes all files in the cache.<br>
* Displays a modal info dialog about the fact that the cache is being emptied.
* @param owner optional owner of the info dialog
*/
protected static void emptyCache_( final String cacheEntity, final Dialog owner, final String infoTextKey, final String cacheFolder ) {
System.out.println( "Clearing " + cacheEntity + " cache..." );
final JDialog infoDialog = owner == null ? new JDialog() : new JDialog( owner );
infoDialog.setModal( true );
infoDialog.setTitle( Language.getText( "general.infoTitle" ) );
final JLabel infoLabel = new JLabel( Language.getText( infoTextKey ) );
infoLabel.setFont( infoLabel.getFont().deriveFont( Font.ITALIC ) );
infoLabel.setBorder( BorderFactory.createEmptyBorder( 25, 35, 25, 35 ) );
infoDialog.getContentPane().add( infoLabel );
infoDialog.pack();
if ( owner == null )
infoDialog.setLocationRelativeTo( null );
else
GuiUtils.centerWindowToWindow( infoDialog, owner );
new NormalThread( cacheEntity + " cache emptier" ) {
@Override
public void run() {
final File[] cacheFiles = new File( cacheFolder ).listFiles();
if ( cacheFiles != null )
for ( final File cacheFile : cacheFiles )
deleteFile( cacheFile );
infoDialog.dispose();
}
}.start();
infoDialog.setVisible( true );
}
public MarkdownDialog(final JFrame owner, final String title,
@NonNls final String filename,
final Map<String, String> variables,
final int width, final int height) {
super(owner, true);
this.filename = filename;
this.variables = variables;
setTitle(title);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setContentPane(buildContent());
setSize(width, height);
setLocationRelativeTo(owner);
setVisible(true);
}
static InputMethodPopupMenu getInstance(Component client, String title) {
if ((client instanceof JFrame) ||
(client instanceof JDialog)) {
return new JInputMethodPopupMenu(title);
} else {
return new AWTInputMethodPopupMenu(title);
}
}
public static void installEscapeCloseOperation(final JDialog dialog) {
Action dispatchClosing = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent event) {
dialog.dispatchEvent(new WindowEvent(
dialog, WindowEvent.WINDOW_CLOSING));
}
};
JRootPane root = dialog.getRootPane();
root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
escapeStroke, dispatchWindowClosingActionMapKey);
root.getActionMap().put(dispatchWindowClosingActionMapKey, dispatchClosing);
}
/**
* Constructor
* @param inParent parent dialog
*/
public ColourChooser(JDialog inParent)
{
_dialog = new JDialog(inParent, I18nManager.getText("dialog.colourchooser.title"), true);
_dialog.setLocationRelativeTo(inParent);
_dialog.getContentPane().add(makeContents());
_dialog.pack();
}
/**
* Implements abstract Filter method.
*
* @return the inspector
*/
public synchronized JDialog getInspector() {
Inspector myInspector = inspector;
if (myInspector==null) {
myInspector = new Inspector();
}
if (myInspector.isModal() && vidPanel!=null) {
frame = JOptionPane.getFrameForComponent(vidPanel);
myInspector.dispose();
myInspector = new Inspector();
}
inspector = myInspector;
inspector.initialize();
return inspector;
}
/** Returns the content pane of our owner, so as to display the wait
* cursor while the dialog is being invoked */
@Override
public Component getCursorChangeComponent() {
Container cont = SheetTable.this.getTopLevelAncestor();
return (cont instanceof JFrame) ? ((JFrame) cont).getContentPane()
: ((cont instanceof JDialog) ? ((JDialog) cont).getContentPane() : cont);
}
public static JDialog createDialog(final Component parent, final String title,
final boolean modal, final CColorChooser chooserPane, final ActionListener okListener,
final ActionListener cancelListener) throws HeadlessException {
// Just forward to ancestor
return JColorChooser
.createDialog(parent, title, modal, chooserPane, okListener, cancelListener);
}
public String open(Wandora wandora, String label, String title) {
wasAccepted = false;
this.wandora = wandora;
dialog = new JDialog(wandora, true);
dialog.setTitle(title);
locatorLabel.setText(label);
dialog.add(this);
dialog.setSize(500, 170);
wandora.centerWindow(dialog);
dialog.setVisible(true);
if(wasAccepted) return locatorField.getText();
else return null;
}
/**
* Test if a dialog that has never been shown fire
* the WINDOW_CLOSED event on parent dispose().
* @throws Exception
*/
public static void testHidenChildDispose() throws Exception {
JFrame f = new JFrame();
JDialog dlg = new JDialog(f);
Listener l = new Listener();
dlg.addWindowListener(l);
f.dispose();
waitEvents();
assertEquals(0, l.getCount());
}