プログラマメモ2 - programmer no memo2

SwingUtilitiesのinvokeLater、invokeAndWaitはいつつかうべき? 2006/06/26
2006/11/26

Swingを使ったプログラミングででてくる

SwingUtilities.invokeLater(...)

をどのようなときに使えばよいのだろうか。



実はよくわかっていない。



こういったコードをみかける。



SwingUtilities.invokeAndWait(new Runnable() {

public void run() {

button.setEnabled(false);

}

});





Javadocには下記のように説明されています。





保留中のすべての AWT イベントが処理されたあとに発生します。

Javadoc 日本語版





invokeLaterを試してみるサンプルをつくる試み。

未完です。



import javax.swing.SwingUtilities;

import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import java.awt.GridBagLayout;

import javax.swing.JButton;

import java.awt.GridBagConstraints;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;



public class TestFrame extends JFrame {



private static final long serialVersionUID = 1L;



private JPanel jContentPane = null;

private JMenuBar jJMenuBar = null;

private JMenu jMenu = null;

private JMenuItem jMenuItem = null;

private JMenu jMenu1 = null;

private JPanel jPanel = null;

private JButton jButton = null;

private JButton jButton1 = null;

private JButton jButton2 = null;



private JMenuItem jMenuItem1 = null;



private JMenuItem jMenuItem2 = null;



private TestFrame.ExecuteActionCommand executeActionCommand = null; // @jve:decl-index=0:visual-constraint="463,138"



private JMenuBar getJJMenuBar() {

if (jJMenuBar == null) {

jJMenuBar = new JMenuBar();

jJMenuBar.add(getJMenu());

}

return jJMenuBar;

}



private JMenu getJMenu() {

if (jMenu == null) {

jMenu = new JMenu();

jMenu.setText("menu");

jMenu.add(getJMenu1());

}

return jMenu;

}



private JMenuItem getJMenuItem() {

if (jMenuItem == null) {

jMenuItem = new JMenuItem();

jMenuItem.setText("take long time..");

jMenuItem.setActionCommand("act1");

jMenuItem.addActionListener(getExecuteActionCommand());

}

return jMenuItem;

}



/**

* This method initializes jMenu1

*

* @return javax.swing.JMenu

*/

private JMenu getJMenu1() {

if (jMenu1 == null) {

jMenu1 = new JMenu();

jMenu1.setText("menu");

jMenu1.add(getJMenuItem());

jMenu1.add(getJMenuItem1());

jMenu1.add(getJMenuItem2());

}

return jMenu1;

}



private JPanel getJPanel() {

if (jPanel == null) {

GridBagConstraints gridBagConstraints2 = new GridBagConstraints();

gridBagConstraints2.gridx = 0;

gridBagConstraints2.gridy = 2;

GridBagConstraints gridBagConstraints1 = new GridBagConstraints();

gridBagConstraints1.gridx = 0;

gridBagConstraints1.gridy = 1;

GridBagConstraints gridBagConstraints = new GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 0;

jPanel = new JPanel();

jPanel.setLayout(new GridBagLayout());

jPanel.add(getJButton(), gridBagConstraints);

jPanel.add(getJButton1(), gridBagConstraints1);

jPanel.add(getJButton2(), gridBagConstraints2);

}

return jPanel;

}



private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButton.setActionCommand("act1");

jButton.setText("take long time..");

jButton.addActionListener(getExecuteActionCommand());



}

return jButton;

}



private JButton getJButton1() {

if (jButton1 == null) {

jButton1 = new JButton();

jButton1.setText("act2");

jButton1.setActionCommand("act2");

jButton1.addActionListener(getExecuteActionCommand());

}

return jButton1;

}



private JButton getJButton2() {

if (jButton2 == null) {

jButton2 = new JButton();

jButton2.setText("act3");

jButton2.setActionCommand("act3");

jButton2.addActionListener(getExecuteActionCommand());

}

return jButton2;

}



private JMenuItem getJMenuItem1() {

if (jMenuItem1 == null) {

jMenuItem1 = new JMenuItem();

jMenuItem1.setText("act2");

jMenuItem1.setActionCommand("act2");

jMenuItem1.addActionListener(getExecuteActionCommand());

}

return jMenuItem1;

}



private JMenuItem getJMenuItem2() {

if (jMenuItem2 == null) {

jMenuItem2 = new JMenuItem();

jMenuItem2.setText("act2");

jMenuItem2.setActionCommand("act2");

jMenuItem2.addActionListener(getExecuteActionCommand());

}

return jMenuItem2;

}



private TestFrame.ExecuteActionCommand getExecuteActionCommand() {

if (executeActionCommand == null) {

executeActionCommand = new TestFrame.ExecuteActionCommand();

}

return executeActionCommand;

}



public static void main(String[] args) {



SwingUtilities.invokeLater(new Runnable() {

public void run() {

TestFrame thisClass = new TestFrame();

thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

thisClass.setVisible(true);

}

});

}



public TestFrame() {

super();

initialize();

}



private void initialize() {

this.setSize(300, 200);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setJMenuBar(getJJMenuBar());

this.setContentPane(getJContentPane());

this.setTitle("JFrame");

}



private JPanel getJContentPane() {

if (jContentPane == null) {

jContentPane = new JPanel();

jContentPane.setLayout(new BorderLayout());

jContentPane.add(getJPanel(), BorderLayout.CENTER);

}

return jContentPane;

}



class ExecuteActionCommand implements ActionListener {



public void actionPerformed(ActionEvent e) {

String actionCommand = e.getActionCommand();

action(actionCommand);

}



public void action(String actionCommand) {

if (eq(actionCommand, "act1")) {

try {

System.out.println("take long time action...");

Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

System.out.println("wake up!!");

}

return;

}



if (eq(actionCommand, "act2")) {



System.out.println(Thread.currentThread());



SwingUtilities.invokeLater(new Runnable() {



public void run() {

jButton.setEnabled(false);

System.out.println("@_@!!" + Thread.currentThread());

}



});



SwingUtilities.invokeLater(new Runnable() {



public void run() {

try {

jButton2.setEnabled(false);

System.out.println("take long time action...");

System.out.println(Thread.currentThread());

Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

System.out.println("wake up!!");



}

}



});



System.out.println("==== THIS LINE");

return;

}

if (eq(actionCommand, "act3")) {

System.out.println("act3...");

Runnable runnable = new Runnable() {



public void run() {

try {

System.out.println("Hoooo "

+ Thread.currentThread().getName());

Thread.sleep(3000);

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

System.out.println("wake up["

+ Thread.currentThread().getName() + "]");

}

}



};

Thread thread = new Thread(runnable);

thread.start();

return;

}

}

}



protected static boolean eq(Object object, Object object2) {

return object == null ? object2 == null : object.equals(object2);

}

}





: