下面列出了javax.swing.JDialog#setVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Show font selection dialog.
*
* @param parent Dialog's Parent component.
* @return OK_OPTION, CANCEL_OPTION or ERROR_OPTION
*
* @see #OK_OPTION
* @see #CANCEL_OPTION
* @see #ERROR_OPTION
**/
public int showDialog(Component parent) {
dialogResultValue = ERROR_OPTION;
JDialog dialog = createDialog(parent);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dialogResultValue = CANCEL_OPTION;
}
});
dialog.setVisible(true);
dialog.dispose();
dialog = null;
return dialogResultValue;
}
@Override
public void setVisible(JDialog dialog, boolean modal) {
/*
IcyFrame icf = new IcyFrame();
icf.addFrameListener(this);
icf.setTitle(dialog.getTitle());
//dialog.setModal(modal);
icf.add(dialog.getContentPane());
//icf.add(panel);
icf.toFront();
icf.addToDesktopPane();
icf.setVisible(true);
*/
//Lab.setVisible(dialog, true);
dialog.pack();
dialog.setLocation(30, 30);
dialog.setVisible(true);
}
/**
* Displays the swing window.
*
* @param parent Parent frame used for positioning.
* @param headerLabel The label text to display at the top of the window.
* @param newKey The API key value to show to the user.
*/
public static void showKey(final JFrame parent, final String headerLabel, final String newKey) {
final JDialog frame = new JDialog(parent, "API Key", true);
frame.setLocationRelativeTo(null);
frame
.getContentPane()
.add(
new JPanelBuilder()
.borderLayout()
.addNorth(JLabelBuilder.builder().border(20).text(headerLabel).build())
.addCenter(
JTextAreaBuilder.builder()
.text(newKey)
.selectAllTextOnFocus()
.readOnly()
.build())
.addSouth(
new JButtonBuilder().title("Close").actionListener(frame::dispose).build())
.build());
frame.pack();
frame.setVisible(true);
}
public static int showDialog(String message, int messageType){
int optionType = JOptionPane.DEFAULT_OPTION;
String title = null;
if (messageType == JOptionPane.ERROR_MESSAGE){
title = "Error ";
optionType = JOptionPane.DEFAULT_OPTION;
}else if (messageType == JOptionPane.QUESTION_MESSAGE){
title = "Confirm ";
optionType = JOptionPane.YES_NO_OPTION;
}
else if (messageType == JOptionPane.INFORMATION_MESSAGE){
title = "Information ";
optionType = JOptionPane.DEFAULT_OPTION;
}
JOptionPane pane = new JOptionPane(message, messageType, optionType);
JDialog dialog = pane.createDialog(pane, title);
UIUtil.applyOptionPaneBackground(pane,UIConsts.OPTIONPANE_BACKGROUND);
dialog.setVisible(true);
Object selectedValue = pane.getValue();
if(selectedValue instanceof Integer) {
return ((Integer)selectedValue).intValue();
}
return JOptionPane.CLOSED_OPTION;
}
/**
* Show dialog.
*
* @param parentPanel the parent panel
* @param connectionDetails the connection details
* @return the connector details panel
*/
public static GeoServerConnection showDialog(
JDialog parentPanel, GeoServerConnection connectionDetails) {
JDialog dialog =
new JDialog(
parentPanel,
Localisation.getString(
ConnectorDetailsPanel.class, "ConnectorDetailsPanel.title"),
true);
dialog.setResizable(false);
ConnectorDetailsPanel panel = new ConnectorDetailsPanel(dialog);
dialog.getContentPane().add(panel);
panel.populate(connectionDetails);
dialog.pack();
dialog.setSize(BasePanel.FIELD_PANEL_WIDTH, 175);
Controller.getInstance().centreDialog(dialog);
if (!isInTestMode()) {
dialog.setVisible(true);
}
if (panel.okButtonPressed() || isInTestMode()) {
return panel.getConnectionDetails();
}
return null;
}
static JDialog show(JColorChooser chooser) {
JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
dialog.setVisible(true);
// block till displayed
Point point = null;
while (point == null) {
try {
point = dialog.getLocationOnScreen();
}
catch (IllegalStateException exception) {
pause(DELAY);
}
}
return dialog;
}
@Override
public String readLine(String promptMessage) {
final JTextField juf = new JTextField();
JOptionPane juop = new JOptionPane(juf,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION);
JDialog userDialog = juop.createDialog(promptMessage);
userDialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentShown(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
juf.requestFocusInWindow();
}
});
}
});
userDialog.setVisible(true);
int uresult = (Integer) juop.getValue();
userDialog.dispose();
String userName = null;
if (uresult == JOptionPane.OK_OPTION) {
userName = new String(juf.getText());
}
if (StringUtils.isEmpty(userName)) {
return null;
} else {
return userName;
}
}
public JDialog showToast(final int DURATION) {
JDialog dialog = this;
Timer timer = new Timer(DURATION, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true); // if modal, application will pause here
return dialog;
}
/**
* Test if a dialog fire the WINDOW_CLOSED event
* on parent dispose().
* @throws Exception
*/
public static void testVisibleChildParentDispose() throws Exception {
JFrame f = new JFrame();
JDialog dlg = new JDialog(f);
Listener l = new Listener();
dlg.addWindowListener(l);
dlg.setVisible(true);
f.dispose();
waitEvents();
assertEquals(1, l.getCount());
}
public void inspectXML() {
// display a TreePanel in a modal dialog
XMLControl xml = new XMLControlElement(getOSPApp());
XMLTreePanel treePanel = new XMLTreePanel(xml);
JDialog dialog = new JDialog((java.awt.Frame) null, true);
dialog.setContentPane(treePanel);
dialog.setSize(new Dimension(600, 300));
dialog.setVisible(true);
}
static public JDialog addModelessWindow(Frame mainWindow, Component jpanel, String title) {
JDialog dialog = new JDialog(mainWindow, title, true);
dialog.getContentPane().setLayout(new BorderLayout());
dialog.getContentPane().add(jpanel, BorderLayout.CENTER);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.pack();
dialog.setLocationRelativeTo(mainWindow);
dialog.setModalityType(ModalityType.MODELESS);
dialog.setSize(jpanel.getPreferredSize());
dialog.setVisible(true);
return dialog;
}
private synchronized void manageRefactorings(boolean single) {
HintsPanel panel;
if (single) {
panel = new HintsPanel((HintMetadata) singleRefactoringCombo.getSelectedItem(), null, cpBased);
} else {
panel = new HintsPanel((Configuration) configurationCombo.getSelectedItem(), cpBased);
}
DialogDescriptor descriptor = new DialogDescriptor(panel, NbBundle.getMessage(InspectAndRefactorPanel.class, "CTL_ManageRefactorings"), true, new Object[]{}, null, 0, null, null);
JDialog dialog = (JDialog) DialogDisplayer.getDefault().createDialog(descriptor);
dialog.validate();
dialog.pack();
dialog.setVisible(true);
if (panel.isConfirmed()) {
if (this.configurationRadio.isSelected()) {
Configuration selectedConfiguration = panel.getSelectedConfiguration();
if (selectedConfiguration != null) {
configurationCombo.setSelectedItem(selectedConfiguration);
}
} else {
HintMetadata selectedHint = panel.getSelectedHint();
if (selectedHint != null) {
if (panel.hasNewHints()) {
singleRefactoringCombo.setModel(new InspectionComboModel((allHints = Utilities.getBatchSupportedHints(cpBased)).keySet()));
}
singleRefactoringCombo.setSelectedItem(selectedHint);
}
}
}
}
private static void doTest(Runnable action) {
String description
= " Visual inspection of print dialog is required.\n"
+ " A print dialog will be shown.\n "
+ " Please verify Copies 5 is selected.\n"
+ " Also verify, Page Range is selected with "
+ " from page 3 and to Page 4.\n"
+ " If ok, press PASS else press FAIL";
final JDialog dialog = new JDialog();
dialog.setTitle("printSelectionTest");
JTextArea textArea = new JTextArea(description);
textArea.setEditable(false);
final JButton testButton = new JButton("Start Test");
final JButton passButton = new JButton("PASS");
passButton.setEnabled(false);
passButton.addActionListener((e) -> {
dialog.dispose();
pass();
});
final JButton failButton = new JButton("FAIL");
failButton.setEnabled(false);
failButton.addActionListener((e) -> {
dialog.dispose();
fail();
});
testButton.addActionListener((e) -> {
testButton.setEnabled(false);
action.run();
passButton.setEnabled(true);
failButton.setEnabled(true);
});
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(textArea, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(testButton);
buttonPanel.add(passButton);
buttonPanel.add(failButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
dialog.add(mainPanel);
dialog.pack();
dialog.setVisible(true);
}
private static void createAndShowTestDialog() {
String description =
" 1. Click on \"Start Test\" button.\r\n" +
" 2. Multiple strings will be displayed on console.\r\n" +
" 3. A print dialog will be shown. Select any printer to print. " +
"\r\n" +
" If the printed output of the strings are same without any alignment issue, click on \"PASS\"\r\n" +
" button, otherwise click on \"FAIL\" button.";
final JDialog dialog = new JDialog();
dialog.setTitle("SaveFileWithoutPrinter");
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dialog.dispose();
fail("Main dialog was closed.");
}
});
final JLabel testTimeoutLabel = new JLabel(String.format(
"Test timeout: %s", convertMillisToTimeStr(testTimeout)));
final long startTime = System.currentTimeMillis();
final Timer timer = new Timer(0, null);
timer.setDelay(1000);
timer.addActionListener((e) -> {
int leftTime = testTimeout - (int) (System.currentTimeMillis() - startTime);
if ((leftTime < 0) || testFinished) {
timer.stop();
dialog.dispose();
}
testTimeoutLabel.setText(String.format(
"Test timeout: %s", convertMillisToTimeStr(leftTime)));
});
timer.start();
JTextArea textArea = new JTextArea(description);
textArea.setEditable(false);
final JButton testButton = new JButton("Start Test");
final JButton passButton = new JButton("PASS");
final JButton failButton = new JButton("FAIL");
testButton.addActionListener((e) -> {
testButton.setEnabled(false);
new Thread(() -> {
try {
doTest();
SwingUtilities.invokeLater(() -> {
passButton.setEnabled(true);
failButton.setEnabled(true);
});
} catch (Throwable t) {
t.printStackTrace();
dialog.dispose();
fail("Exception occurred in a thread executing the test.");
}
}).start();
});
passButton.setEnabled(false);
passButton.addActionListener((e) -> {
dialog.dispose();
pass();
});
failButton.setEnabled(false);
failButton.addActionListener((e) -> {
dialog.dispose();
fail("Printed texts are not aligned as shown in console");
});
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel labelPanel = new JPanel(new FlowLayout());
labelPanel.add(testTimeoutLabel);
mainPanel.add(labelPanel, BorderLayout.NORTH);
mainPanel.add(textArea, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(testButton);
buttonPanel.add(passButton);
buttonPanel.add(failButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
dialog.add(mainPanel);
dialog.pack();
dialog.setVisible(true);
}
@Override
public void actionPerformed(final ActionEvent event) {
final JDialog userChoiceDialog = new JDialog(parent, "Actions and Operations", true);
final JPanel userChoicePanel = new JPanel();
userChoicePanel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
userChoicePanel.setLayout(new GridBagLayout());
int row = 0;
final JScrollPane choiceScroll =
new JScrollPane(getUserActionButtonPanel(userChoiceDialog));
choiceScroll.setBorder(BorderFactory.createEtchedBorder());
userChoicePanel.add(
choiceScroll,
new GridBagConstraints(
0,
row++,
2,
1,
1,
1,
GridBagConstraints.CENTER,
GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0),
0,
0));
final JButton noActionButton =
new JButton(SwingAction.of("No Actions", e -> userChoiceDialog.setVisible(false)));
SwingUtilities.invokeLater(noActionButton::requestFocusInWindow);
userChoicePanel.add(
noActionButton,
new GridBagConstraints(
0,
row,
2,
1,
0.0,
0.0,
GridBagConstraints.EAST,
GridBagConstraints.NONE,
new Insets(12, 0, 0, 0),
0,
0));
userChoiceDialog.setContentPane(userChoicePanel);
userChoiceDialog.pack();
userChoiceDialog.setLocationRelativeTo(parent);
userChoiceDialog.setVisible(true);
userChoiceDialog.dispose();
}
private static void doTest(Runnable action) {
String description
= " Initially, a page dialog will be shown.\n "
+ " The left, right, bottom, right margin will have value 1.0.\n"
+ " Modify right margin from 1.0 to 0.0 and press tab.\n"
+ " Please verify the right margin changes back to 1.0. \n"
+ " Please do the same for bottom margin.\n"
+ " If right and bottom margin changes back to 1.0, press PASS else press fail";
final JDialog dialog = new JDialog();
dialog.setTitle("printSelectionTest");
JTextArea textArea = new JTextArea(description);
textArea.setEditable(false);
final JButton testButton = new JButton("Start Test");
final JButton passButton = new JButton("PASS");
passButton.setEnabled(false);
passButton.addActionListener((e) -> {
dialog.dispose();
pass();
});
final JButton failButton = new JButton("FAIL");
failButton.setEnabled(false);
failButton.addActionListener((e) -> {
dialog.dispose();
fail();
});
testButton.addActionListener((e) -> {
testButton.setEnabled(false);
action.run();
passButton.setEnabled(true);
failButton.setEnabled(true);
});
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(textArea, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(testButton);
buttonPanel.add(passButton);
buttonPanel.add(failButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
dialog.add(mainPanel);
dialog.pack();
dialog.setVisible(true);
}
@Override
protected void doInstall ()
throws Exception
{
// When running without UI, we assume license is accepted
if (!Installer.hasUI()) {
return;
}
// User choice (must be an output, yet final)
final boolean[] isOk = new boolean[1];
final String yes = "Yes";
final String no = "No";
final String browse = "View License";
final JOptionPane optionPane = new JOptionPane(
"Do you agree to license " + LICENSE_NAME + "?",
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_CANCEL_OPTION,
null,
new Object[]{yes, no, browse},
yes);
final String frameTitle = "End User License Agreement";
final JDialog dialog = new JDialog(
Installer.getFrame(),
frameTitle,
true);
dialog.setContentPane(optionPane);
// Prevent dialog closing
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
optionPane.addPropertyChangeListener(
new PropertyChangeListener()
{
@Override
public void propertyChange (PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (dialog.isVisible()
&& (e.getSource() == optionPane)
&& (prop.equals(JOptionPane.VALUE_PROPERTY))) {
Object option = optionPane.getValue();
logger.debug("option: {}", option);
if (option == yes) {
isOk[0] = true;
dialog.setVisible(false);
dialog.dispose();
} else if (option == no) {
isOk[0] = false;
dialog.setVisible(false);
dialog.dispose();
} else if (option == browse) {
logger.info(
"Launching browser on {}",
LICENSE_URL);
showLicense();
optionPane.setValue(
JOptionPane.UNINITIALIZED_VALUE);
} else {
}
}
}
});
dialog.pack();
dialog.setLocationRelativeTo(Installer.getFrame());
dialog.setVisible(true);
logger.debug("OK: {}", isOk[0]);
if (!isOk[0]) {
throw new LicenseDeclinedException();
}
}
@Override
public void setActive(boolean pActive)
{
if (!mActive && pActive)
{
if (mFirstTime)
{
mRootFolder = FolderChooser.openFolderChooser(null,
"Choose root folder to save videos",
mRootFolder);
mFirstTime = false;
}
while (getNewVideoFolder())
mVideoCounter++;
mVideoFolder.mkdirs();
mExecutorService =
Executors.newFixedThreadPool(Runtime.getRuntime()
.availableProcessors());
mActive = true;
}
else if (mActive && !pActive)
{
mActive = false;
mExecutorService.shutdown();
final JDialog lJDialog = new JDialog((JFrame) null,
"Saving video",
true);
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
final JProgressBar lJProgressBar = new JProgressBar(0, 500);
lJProgressBar.setValue(499);
lJDialog.add(BorderLayout.CENTER, lJProgressBar);
lJDialog.add(BorderLayout.NORTH,
new JLabel("Saving images, please wait!"));
lJDialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
lJDialog.setSize(300, 75);
lJDialog.validate();
lJDialog.setVisible(true);
}
});
try
{
mExecutorService.awaitTermination(30, TimeUnit.SECONDS);
}
catch (final InterruptedException e)
{
e.printStackTrace();
}
mExecutorService.shutdownNow();
mExecutorService = null;
lJDialog.setVisible(false);
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
lJDialog.setModal(false);
lJDialog.dispose();
}
});
}
}
private TopicMapIF importTopicMap(TopicMapReaderIF reader, String name) {
final JOptionPane pane = new JOptionPane(new Object[] { Messages
.getString("Viz.LoadingTopicMap") + name },
JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null,
new String[] {}, null);
Frame frame = JOptionPane.getFrameForComponent(vpanel);
final JDialog dialog = new JDialog(frame, Messages
.getString("Viz.Information"), true);
dialog.setContentPane(pane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
JOptionPane
.showMessageDialog(pane,
Messages.getString("Viz.CannotCancelOperation"),
Messages.getString("Viz.Information"),
JOptionPane.INFORMATION_MESSAGE);
}
});
dialog.pack();
dialog.setLocationRelativeTo(frame);
TopicMapIF tm;
final TopicMapReaderIF r = reader;
final SwingWorker worker = new SwingWorker() {
@Override
public Object construct() {
TopicMapIF result = null;
try {
result = r.read();
} catch (IOException e) {
dialog.setVisible(false);
ErrorDialog.showError(vpanel, e.getMessage());
}
return result;
}
@Override
public void finished() {
dialog.setVisible(false);
}
};
worker.start();
dialog.setVisible(true);
tm = (TopicMapIF) worker.getValue();
return tm;
}
public static void showRunScriptDialog(SessionPanel session) {
JPanel rsp = new JPanel();
rsp.setLayout(new BorderLayout());
JLabel jl = new JLabel("Enter script to run");
final JTextField rst = new JTextField();
rsp.add(jl,BorderLayout.NORTH);
rsp.add(rst,BorderLayout.CENTER);
Object[] message = new Object[1];
message[0] = rsp;
String[] options = {"Run","Cancel"};
final JOptionPane pane = new JOptionPane(
message, // the dialog message array
JOptionPane.QUESTION_MESSAGE, // message type
JOptionPane.DEFAULT_OPTION, // option type
null, // optional icon, use null to use the default icon
options, // options string array, will be made into buttons//
options[0]); // option that should be made into a default button
// create a dialog wrapping the pane
final JDialog dialog = pane.createDialog(session, // parent frame
"Run Script" // dialog title
);
// add the listener that will set the focus to
// the desired option
dialog.addWindowListener( new WindowAdapter() {
public void windowOpened( WindowEvent e) {
super.windowOpened( e );
// now we're setting the focus to the desired component
// it's not the best solution as it depends on internals
// of the OptionPane class, but you can use it temporarily
// until the bug gets fixed
// also you might want to iterate here thru the set of
// the buttons and pick one to call requestFocus() for it
rst.requestFocus();
}
});
dialog.setVisible(true);
// now we can process the value selected
// now we can process the value selected
// if its Integer, the user most likely hit escape
Object myValue = pane.getValue();
if (!(myValue instanceof Integer)) {
String value = (String) myValue;
if (value.equals(options[0])) {
// send option along with system request
if (rst.getText().length() > 0) {
invoke(rst.getText(), session);
}
}
}
}