Example usage for javax.swing JOptionPane JOptionPane

List of usage examples for javax.swing JOptionPane JOptionPane

Introduction

In this page you can find the example usage for javax.swing JOptionPane JOptionPane.

Prototype

public JOptionPane(Object message, int messageType) 

Source Link

Document

Creates an instance of JOptionPane to display a message with the specified message type and the default options,

Usage

From source file:Main.java

    public static void main(String[] args) {
  JOptionPane pane = new JOptionPane("JOptionPane",
      JOptionPane.INFORMATION_MESSAGE);
  String dialogTitle = "Resizable Custom Dialog";
  JDialog dialog = pane.createDialog(dialogTitle);
  dialog.setResizable(true);/*from   w w  w.j  ava  2  s . co m*/
  dialog.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    int TIME_VISIBLE = 3000;
    JFrame frame1 = new JFrame();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(100, 100);/*from  ww  w . j  a  v a  2s. c o m*/
    frame1.setLocation(100, 100);

    JButton button = new JButton("My Button");
    frame1.getContentPane().add(button);

    button.addActionListener(e -> {
        JOptionPane pane = new JOptionPane("Message", JOptionPane.INFORMATION_MESSAGE);
        JDialog dialog = pane.createDialog(null, "Title");
        dialog.setModal(false);
        dialog.setVisible(true);

        new Timer(TIME_VISIBLE, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        }).start();
    });

    frame1.setVisible(true);

}

From source file:DualModal.java

public static void main(String args[]) {
    final JFrame frame1 = new JFrame("Left");
    final JFrame frame2 = new JFrame("Right");
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button1 = new JButton("Left");
    JButton button2 = new JButton("Right");
    frame1.add(button1);//from  w  w  w.  j  a  v a2  s  . co m
    frame2.add(button2);
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JButton source = (JButton) e.getSource();

            JOptionPane pane = new JOptionPane("New label", JOptionPane.QUESTION_MESSAGE);
            pane.setWantsInput(true);
            JDialog dialog = pane.createDialog(frame2, "Enter Text");
            // dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
            dialog.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            dialog.setVisible(true);
            String text = (String) pane.getInputValue();

            if (!JOptionPane.UNINITIALIZED_VALUE.equals(text) && text.trim().length() > 0) {
                source.setText(text);
            }
        }
    };
    button1.addActionListener(listener);
    button2.addActionListener(listener);
    frame1.setBounds(100, 100, 200, 200);
    frame1.setVisible(true);
    frame2.setBounds(400, 100, 200, 200);
    frame2.setVisible(true);
}

From source file:Main.java

/**
 * JOption panes static methods are not swing-conform and not decoratable.
 * <p>//from ww w .  j  a v  a  2 s.c  om
 * This utility function corrects this by giving the dialog/rootPane the name "Synth.Dialog"
 */
public static void showMessageDialogSynthCapable(Component parentComponent, Object message, String title,
        int messageType) {
    JOptionPane pane = new JOptionPane(message, messageType);
    JDialog dlg = pane.createDialog(parentComponent, title);
    dlg.getRootPane().setName("Synth.Dialog");
    dlg.pack();
    dlg.setVisible(true);
}

From source file:Main.java

static JOptionPane createOptionPane(String message, int type) {
    JOptionPane pane = new JOptionPane(message, type);

    if (type == JOptionPane.QUESTION_MESSAGE) {

        pane.setOptionType(JOptionPane.YES_NO_CANCEL_OPTION);
    }/*from  w w w.ja  va  2  s  . co  m*/
    return pane;
}

From source file:cz.nn.copytables.gui.CopyTablesGUI.java

private void showDialog(String title, String message) {
    JOptionPane pane = new JOptionPane(message, JOptionPane.INFORMATION_MESSAGE);
    JDialog dialog = pane.createDialog(this, title);
    dialog.setAlwaysOnTop(true);/*  w w  w.  jav a 2 s . c o m*/
    dialog.setVisible(true);
    logger.info("setVisible");
}

From source file:cz.nn.copytables.gui.CopyTablesGUI.java

void showErrorPane(String title, String msg) {
    logger.error(msg);/*  w w  w.j a  va 2  s . c o  m*/
    JOptionPane pane = new JOptionPane(msg, JOptionPane.ERROR_MESSAGE);
    JDialog dialog = pane.createDialog("Application says: " + title);
    dialog.setAlwaysOnTop(true);
    dialog.setVisible(true);
}

From source file:JuliaSet3.java

public void printToService(PrintService service, PrintRequestAttributeSet printAttributes) {
    // Wrap ourselves in the PrintableComponent class defined by JuliaSet2.
    String title = "Julia set for c={" + cx + "," + cy + "}";
    Printable printable = new PrintableComponent(this, title);

    // Now create a Doc that encapsulate the Printable object and its type
    DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    Doc doc = new SimpleDoc(printable, flavor, null);

    // Java 1.1 uses PrintJob.
    // Java 1.2 uses PrinterJob.
    // Java 1.4 uses DocPrintJob. Create one from the service
    DocPrintJob job = service.createPrintJob();

    // Set up a dialog box to monitor printing status
    final JOptionPane pane = new JOptionPane("Printing...", JOptionPane.PLAIN_MESSAGE);
    JDialog dialog = pane.createDialog(this, "Print Status");
    // This listener object updates the dialog as the status changes
    job.addPrintJobListener(new PrintJobAdapter() {
        public void printJobCompleted(PrintJobEvent e) {
            pane.setMessage("Printing complete.");
        }/*from  w  w w . j  a va2  s.com*/

        public void printDataTransferCompleted(PrintJobEvent e) {
            pane.setMessage("Document transfered to printer.");
        }

        public void printJobRequiresAttention(PrintJobEvent e) {
            pane.setMessage("Check printer: out of paper?");
        }

        public void printJobFailed(PrintJobEvent e) {
            pane.setMessage("Print job failed");
        }
    });

    // Show the dialog, non-modal.
    dialog.setModal(false);
    dialog.show();

    // Now print the Doc to the DocPrintJob
    try {
        job.print(doc, printAttributes);
    } catch (PrintException e) {
        // Display any errors to the dialog box
        pane.setMessage(e.toString());
    }
}

From source file:eu.asterics.mw.services.AstericsErrorHandling.java

/**
 * This method is used by the components to report an error. It logs the error in "warning" logger
 *  and sets the status of the ARE to "ERROR" to denote that an error has occurred 
 * @param component the component instance that reports the error
 * @param errorMsg the error message//www. java  2  s .  co m
 */
public void reportError(IRuntimeComponentInstance component, final String errorMsg) {
    if (component != null) {
        String componentID = DeploymentManager.instance
                .getIRuntimeComponentInstanceIDFromIRuntimeComponentInstance(component);
        if (componentID != null) {
            //System.out.println("componentID: "+componentID);
            logger.warning(componentID + ": " + errorMsg);
            setStatusObject(AREStatus.ERROR.toString(), componentID, errorMsg);
        }
    }

    AREProperties props = AREProperties.instance;
    if (props.checkProperty("showErrorDialogs", "1")) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ErrorLogPane.appendLog(errorMsg);
                DeploymentManager.instance.setStatus(AREStatus.ERROR);

                AstericsErrorHandling.this.notifyAREEventListeners("onAreError", errorMsg);

                JOptionPane op = new JOptionPane(errorMsg, JOptionPane.WARNING_MESSAGE);

                JDialog dialog = op.createDialog("AsTeRICS Runtime Environment:");
                dialog.setAlwaysOnTop(true);
                dialog.setModal(false);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.setVisible(true);
            }
        });
    }
}

From source file:me.philnate.textmanager.utils.PDFCreator.java

@SuppressWarnings("deprecation")
private void preparePDF() {
    try {/*from w  w w  .j  a v  a 2s  .c  o m*/
        File path = new File(SystemUtils.getUserDir(), "template");
        File template = new File(path, Setting.find("template").getValue());

        Velocity.setProperty("file.resource.loader.path", path.getAbsolutePath());
        Velocity.init();
        VelocityContext ctx = new VelocityContext();

        // User data/Settings
        for (Setting setting : ds.find(Setting.class).asList()) {
            ctx.put(setting.getKey(), setting.getValue());
        }

        NumberFormat format = NumberFormat.getNumberInstance(new Locale(Setting.find("locale").getValue()));
        // #60 always show 2 digits for fraction no matter if right most(s)
        // are zero
        format.setMinimumFractionDigits(2);
        format.setMaximumFractionDigits(2);
        ctx.put("number", format);
        // TODO update schema to have separate first and lastname
        // Customer data
        ctx.put("customer", customer);

        // General data
        ctx.put("month", new DateFormatSymbols().getMonths()[month]);
        ctx.put("math", new MathTool());
        // Billing data
        ctx.put("allItems", BillingItem.find(customer.getId(), year, month));
        ctx.put("billNo", bill.getBillNo());

        StringWriter writer = new StringWriter();
        Velocity.mergeTemplate(template.getName(), ctx, writer);
        File filledTemplate = new File(path, bill.getBillNo() + ".tex");
        FileUtils.writeStringToFile(filledTemplate, writer.toString(), "ISO-8859-1");

        ProcessBuilder pdfLatex = new ProcessBuilder(Setting.find("pdfLatex").getValue(),
                "-interaction nonstopmode", "-output-format pdf", filledTemplate.toString());

        // Saving template file (just in case it may be needed later
        GridFSFile texFile = tex.createFile(filledTemplate);
        texFile.put("month", month);
        texFile.put("year", year);
        texFile.put("customerId", customer.getId());
        texFile.save();

        pdfLatex.directory(path);
        String pdfPath = filledTemplate.toString().replaceAll("tex$", "pdf");
        if (0 == printOutputStream(pdfLatex)) {
            // display Bill in DocumentViewer
            new ProcessBuilder(Setting.find("pdfViewer").getValue(), pdfPath).start().waitFor();
            GridFSFile pdfFile = pdf.createFile(new File(pdfPath));
            pdfFile.put("month", month);
            pdfFile.put("year", year);
            pdfFile.put("customerId", customer.getId());
            pdf.remove(QueryBuilder.start("month").is(month).and("year").is(year).and("customerId")
                    .is(customer.getId()).get());
            pdfFile.save();
            File[] files = path.listFiles((FileFilter) new WildcardFileFilter(bill.getBillNo() + ".*"));
            for (File file : files) {
                FileUtils.forceDelete(file);
            }
        } else {
            new JOptionPane(
                    "Bei der Erstellung der Rechnung ist ein Fehler aufgetreten. Es wurde keine Rechnung erstellt.\n Bitte Schauen sie in die Logdatei fr nhere Fehlerinformationen.",
                    JOptionPane.ERROR_MESSAGE).setVisible(true);
        }
    } catch (IOException e) {
        Throwables.propagate(e);
    } catch (InterruptedException e) {
        Throwables.propagate(e);
    }
}