Question

this is the documentacion:

Causes doRun.run() to be executed asynchronously on the AWT event dispatching thread. This will happen after all pending AWT events have been processed. This method should be used when an application thread needs to update the GUI. In the following example the invokeLater call queues the Runnable object doHelloWorld on the event dispatching thread and then prints a message.

but i want to know how it means in code

I always made a program like this:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.BufferedImage;

/**
 *
 * @author Robin
 */

public class Example {

    JFrame Frame=new JFrame();


    public Example() {

        Frame.setTitle("Example");
        Frame.setName("Example");
        Frame.setSize(300, 300);
        Frame.setResizable(false);
        Frame.setUndecorated(false);
        Frame.setLayout(null);
        Frame.setLocationRelativeTo(null);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Frame.setIconImage(CrearIcono(Color.decode("#F4141D")).getImage());
        Frame.getContentPane().setBackground(Color.WHITE);
        Formato();
        Accion();
        Mover(Frame.getGlassPane());
        Frame.setVisible(true);
    }


    private void Formato() {


    }

    private void Accion() {


    }

    public ImageIcon Imagen( String dir){return new ImageIcon(getClass().getResource("/lib/"+dir));}

    public static void Mover(final Component objeto) {
        MouseInputAdapter d=new MouseInputAdapter() {int x,X,y,Y;
        @Override public void mousePressed(MouseEvent e){x=e.getXOnScreen();X=objeto.getLocation().x;y=e.getYOnScreen();Y=objeto.getLocation().y;}
        @Override public void mouseDragged(MouseEvent e){objeto.setLocation(X+(e.getXOnScreen()-x), Y+(e.getYOnScreen()-y));}};
        objeto.addMouseListener(d);objeto.addMouseMotionListener(d);
    }

    public int CentrarX(int AnchoObjeto, int AnchoRespectoA){return (AnchoRespectoA/2)-(AnchoObjeto/2);}
    public int CentrarY(int LargoObjeto, int LargoRespectoA){return (LargoRespectoA/2)-(LargoObjeto/2);}
    public int ImgA(JLabel imagen){return imagen.getIcon().getIconWidth();}
    public int ImgL(JLabel imagen){return imagen.getIcon().getIconHeight();}

    public static ImageIcon CrearIcono(Color color) {
        int WIDTH = 32;
        int HEIGHT = 32;
        BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        int[] xPoints = {WIDTH, 0, 0, WIDTH / 2};
        int[] yPoints = {0, WIDTH / 2, WIDTH, WIDTH};
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(color);
        g2.fillPolygon(xPoints, yPoints, xPoints.length);
        g2.dispose();

        ImageIcon icon = new ImageIcon(img);
        return icon;
    }

    public static void main(String[] args) {

        Example Ventana=new Example();
    }


}

what is better? what is the difference?

SwingUtilities.invokeLater

public static void main(String[] args) {



SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            pantalla principal=new pantalla();   
            Calendario s=new Calendario(1); 
        }
    });       
}

Without invokeLater

public static void main(String[] args) {

            Example Ventana=new Example();
        }

thanks for your advices

Was it helpful?

Solution

The first one using invokeLater(..) is better in that it is the correct way to create a GUI.

See Concurrency in Swing (and particularly 'Initial Threads') for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top