Java Interrupt Flow and ActionListener

I have a graphics () function that creates my JFrame and two JRadioButtons and adds ActionListeners to them. This graphic is called from main (), and the graphic itself calls the game ().

public void game() throws Exception { jTextArea1.setLineWrap(true); jTextArea1.setWrapStyleWord(true); jTextArea1.setText("This is private information."); jRadioButton1.setVisible(true); jRadioButton2.setVisible(true); try { t.sleep(40000); repaint(); } catch (InterruptedException e) { // We've been interrupted: no more messages. return; } 

After displaying "This is personal information." in the text area, I want the program to pause for 40 seconds, or until the user presses the JRadioButton button, whichever comes first. So I added an ActionListener and called t.interrupt () inside it.

 private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) { t.interrupt(); jRadioButton1.setVisible(false); jRadioButton2.setVisible(false); //System.out.println(t.interrupted()); jTextArea1.setText("Please wait..."); } 

However, even after choosing JRadioButton, which should cause an interrupt, this does not happen, and t.interrupted returns false.

Any help would be appreciated.

+4
source share
1 answer

Never, never call Thread.sleep(...) in the Swing event stream, as you will freeze the stream and effectively freeze your program. The solution is to consider using a Swing timer for the temporary part of your requirement and using a SelectionListener for the JCheckBox or JRadioButton requirement.

For instance:

 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.*; public class PausingExecution extends JPanel { private static final String SELECTED_TEXT = "Snafus are Better!!!"; private static final String UNSELECTED_TEXT = "Fubars Rule!!"; private static final String TIMES_UP = "Time Up!!!!"; private static final int TIMER_DELAY = 10 * 1000; private JTextField messageField = new JTextField(UNSELECTED_TEXT, 10); private JCheckBox checkBox = new JCheckBox("Click Me"); public PausingExecution() { add(messageField); add(checkBox); checkBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent iEvt) { if (iEvt.getStateChange() == ItemEvent.SELECTED) { messageField.setText(SELECTED_TEXT); } else { messageField.setText(UNSELECTED_TEXT); } } }); Timer mySwingTimer = new Timer(TIMER_DELAY, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { messageField.setText(TIMES_UP); checkBox.setEnabled(false); } }); mySwingTimer.setRepeats(false); mySwingTimer.start(); } private static void createAndShowGui() { PausingExecution mainPanel = new PausingExecution(); JFrame frame = new JFrame("PausingExecution"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } 
+8
source

Source: https://habr.com/ru/post/1441955/


All Articles