Example usage for javax.swing SwingUtilities invokeAndWait

List of usage examples for javax.swing SwingUtilities invokeAndWait

Introduction

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

Prototype

public static void invokeAndWait(final Runnable doRun) throws InterruptedException, InvocationTargetException 

Source Link

Document

Causes doRun.run() to be executed synchronously on the AWT event dispatching thread.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//  w  w  w  . j a v  a2  s  .  co  m
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                System.out.println("Hello World on " + Thread.currentThread());
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Finished on " + Thread.currentThread());
}

From source file:Main.java

public static void main(String args[]) {
    final Main it = new Main();

    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);//from www  . ja v  a2  s. co  m
    frame.pack();
    frame.setVisible(true);

    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
        final int percent = i;
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    it.updateBar(percent);
                }
            });

            java.lang.Thread.sleep(100);

        } catch (Exception e) {
            ;
        }
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    final MainClass it = new MainClass();

    JFrame frame = new JFrame("Progress Bar Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(it);/*from w  w  w .jav  a 2  s  .  co m*/
    frame.pack();
    frame.setVisible(true);

    for (int i = MY_MINIMUM; i <= MY_MAXIMUM; i++) {
        final int percent = i;
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    it.updateBar(percent);
                }
            });

            java.lang.Thread.sleep(100);

        } catch (Exception e) {
            ;
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {/*from   www  . j a v a2 s  . c  o  m*/
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    doc.insertString(0, text, null);

                    doc.setParagraphAttributes(0, 1, heading2Style, false);
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample5.java

public static void main(String[] args) {
    try {//from  w  w  w  .  j a v a 2 s . c  o m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 5");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Create and add the style
    final Style heading2Style = sc.addStyle("Heading2", null);
    heading2Style.addAttribute(StyleConstants.Foreground, Color.red);
    heading2Style.addAttribute(StyleConstants.FontSize, new Integer(16));
    heading2Style.addAttribute(StyleConstants.FontFamily, "serif");
    heading2Style.addAttribute(StyleConstants.Bold, new Boolean(true));

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);

                    // Set the foreground and font
                    pane.setForeground(Color.blue);
                    pane.setFont(new Font("serif", Font.PLAIN, 12));
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample4.java

public static void main(String[] args) {
    try {/*  ww w .  ja va 2 s .c o  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 4");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    // Create and add the main document style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Set the logical style
                    doc.setLogicalStyle(0, mainStyle);
                } catch (BadLocationException e) {
                }
                doc.dump(System.out);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample2.java

public static void main(String[] args) {
    try {//from w  w  w  .  j a  va  2s .c o m
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 2");

    // Create the StyleContext, the document and the pane
    StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    JTextPane pane = new JTextPane(doc);

    // Create and add the main document style
    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
    StyleConstants.setLeftIndent(mainStyle, 16);
    StyleConstants.setRightIndent(mainStyle, 16);
    StyleConstants.setFirstLineIndent(mainStyle, 16);
    StyleConstants.setFontFamily(mainStyle, "serif");
    StyleConstants.setFontSize(mainStyle, 12);

    // Create and add the constant width style
    final Style cwStyle = sc.addStyle("ConstantWidth", null);
    StyleConstants.setFontFamily(cwStyle, "monospaced");
    StyleConstants.setForeground(cwStyle, Color.green);

    // Create and add the heading style
    final Style heading2Style = sc.addStyle("Heading2", null);
    StyleConstants.setForeground(heading2Style, Color.red);
    StyleConstants.setFontSize(heading2Style, 16);
    StyleConstants.setFontFamily(heading2Style, "serif");
    StyleConstants.setBold(heading2Style, true);
    StyleConstants.setLeftIndent(heading2Style, 8);
    StyleConstants.setFirstLineIndent(heading2Style, 0);

    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                try {
                    // Set the logical style
                    doc.setLogicalStyle(0, mainStyle);

                    // Add the text to the document
                    doc.insertString(0, text, null);

                    // Apply the character attributes
                    doc.setCharacterAttributes(49, 13, cwStyle, false);
                    doc.setCharacterAttributes(223, 14, cwStyle, false);
                    doc.setCharacterAttributes(249, 14, cwStyle, false);
                    doc.setCharacterAttributes(286, 8, cwStyle, false);
                    doc.setCharacterAttributes(475, 14, cwStyle, false);
                    doc.setCharacterAttributes(497, 21, cwStyle, false);
                    doc.setCharacterAttributes(557, 9, cwStyle, false);
                    doc.setCharacterAttributes(639, 12, cwStyle, false);
                    doc.setCharacterAttributes(733, 21, cwStyle, false);
                    doc.setCharacterAttributes(759, 9, cwStyle, false);

                    // Finally, apply the style to the heading
                    doc.setParagraphAttributes(0, 1, heading2Style, false);
                } catch (BadLocationException e) {
                }
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:edu.mit.fss.tutorial.part4.ElementGUI.java

/**
 * The main method./* w  w w  .j a  va 2 s  . co  m*/
 *
 * @param args the arguments
 * @throws RTIexception the RTI exception
 */
public static void main(String[] args) throws RTIexception {
    // Configure the logger and set it to display info messages.
    BasicConfigurator.configure();
    logger.setLevel(Level.INFO);

    // Use an input dialog to request the element's name.
    String name = null;
    while (name == null || name.isEmpty()) {
        name = JOptionPane.showInputDialog("Enter element name:");
    }

    // Create a MobileElement object instance. The "final" keyword allows 
    // it to be referenced in the GUI thread below.
    final MobileElement element = new MobileElement(name, new Vector3D(0, 0, 0));

    // Create an OnlineTutorialFederate object instance. The "final" 
    // keyword allows it to be referenced in the GUI thread below.
    final OnlineTutorialFederate fed = new OnlineTutorialFederate(element);

    // Create the graphical user interface using the Event Dispatch Thread.
    try {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                // Create a new ControlPanel object instance.
                ControlPanel controlPanel = new ControlPanel();

                // Bind it to the element above.
                controlPanel.setBoundElement(element);

                // Add the control panel as an object change listener.
                fed.addObjectChangeListener(controlPanel);

                // Create a new frame to display the panel. Add the panel
                // as the content, pack it, and make it visible.
                JFrame frame = new JFrame();
                frame.setContentPane(controlPanel);
                frame.pack();
                frame.setVisible(true);

                // Add a new WindowAdapter object instance to exit the
                // federate when the window is closing.
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        fed.exit();
                    }
                });
            }
        });
    } catch (InvocationTargetException | InterruptedException e) {
        logger.error(e);
    }

    // Execute the federate.
    fed.execute(0, Long.MAX_VALUE, 1000);
}

From source file:StylesExample7.java

public static void main(String[] args) {
    try {//from w  ww .j  a v  a  2s  .  com
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 7");

    // Create the StyleContext, the document and the pane
    final StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:StylesExample8.java

public static void main(String[] args) {
    try {/*from w w w  .j  a  va  2s .  co  m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (Exception evt) {
    }

    JFrame f = new JFrame("Styles Example 8");

    // Create the StyleContext, the document and the pane
    final StyleContext sc = new StyleContext();
    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
    final JTextPane pane = new JTextPane(doc);

    // Build the styles
    createDocumentStyles(sc);

    try {
        // Add the text and apply the styles
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                // Add the text
                addText(pane, sc, sc.getStyle(mainStyleName), content);
            }
        });
    } catch (Exception e) {
        System.out.println("Exception when constructing document: " + e);
        System.exit(1);
    }

    f.getContentPane().add(new JScrollPane(pane));
    f.setSize(500, 300);
    f.setVisible(true);
}