Problem GUI - thread

Status
Nicht offen für weitere Antworten.
G

Gardakor

Gast
I'm sorry for the english comments, I don't speak German
This is the code :
Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

public class TestProblem implements ActionListener {
	
	public static void main(String[] args) {
		new TestProblem();
	}

	final int WAIT_TIME_IN_SECONDS = 5;
	JButton button1, button2;
	
	public TestProblem () {
		JFrame frame = new JFrame();
		JPanel window = new JPanel();
		button1 = new JButton("Test");
		button1.addActionListener(this);
		window.add(button1);
		button2 = new JButton("Click on this button, and within "+WAIT_TIME_IN_SECONDS+" seconds, 10 times on the other button");
		button2.addActionListener(this);
		window.add(button2);
		frame.setContentPane(window);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		/*  
			the commands under the first if-statement show strange behaviour.
			All GUI-related commands will be only executed after this method has returned.
			Put all commands belonging to the first if-statement in an separate thread -> problem solved
		*/
		if (e.getSource() == button2) {
			// this command will be executed after this ActionEvent has been handled !
			button1.setEnabled(false);
			try {
				Thread.sleep(WAIT_TIME_IN_SECONDS * 1000);
			} catch (Exception ex) {}
			// this command will be executed after this ActionEvent has been handled !
			button1.setEnabled(true);
		}
		else if (e.getSource() == button1) {
			//JOptionPane.showMessageDialog(null, "Noooooo");
			System.out.println("Button1 pressed");
		}
	}
}

Does anybody know a better solution than starting a new thread ?
My teacher (who hasn't the time to examine this case) told me it's something about resetting the focus :bahnhof:
 

Wildcard

Top Contributor
I have to admit that I don't understand your problem. What do you want to do anyway?
A better description would be helpfull.
 
G

Gardakor

Gast
Wildcard hat gesagt.:
I have to admit that I don't understand your problem. What do you want to do anyway?
A better description would be helpfull.

It's a simplified program, I want to do something similar, but in a large program.
In this application , I simply want that button1 is disabled for 5 seconds, after I press button2.
 
R

Roar

Gast
you have to put all code that affects the GUI into another thread which has to be invoked by SwingUtilities.invokeLater() in the actionPerformed() method.
 

Wildcard

Top Contributor
Why not this way?
Code:
button1.setEnabled(true);
if (e.getSource() == button2) { 
  button1.setEnabled(false);
  Timer t = new Timer(5000,this);
   t.start();
}
 
G

Gardakor

Gast
Roar hat gesagt.:
you have to put all code that affects the GUI into another thread which has to be invoke by SwingUtilities.invokeLater() in the actionPerformed() method.

Something like this ?
But this doesn't work
Code:
		if (e.getSource() == button2) {
			SwingUtilities.invokeLater(new Thread() {
					public void run() {
						button1.setEnabled(false);
						try {
							Thread.sleep(WAIT_TIME_IN_SECONDS * 1000);
						} catch (Exception ex) {}
						// this command will be executed after this ActionEvent has been handled !
						button1.setEnabled(true);
					}
				}
			);
		}
 
G

Guest

Gast
Wildcard hat gesagt.:
Why not this way?
Code:
button1.setEnabled(true);
if (e.getSource() == button2) { 
  button1.setEnabled(false);
  Timer t = new Timer(5000,this);
   t.start();
}

thx for the code, but in the application I want to use it, I don't know how much time the function will consume.
 

Wildcard

Top Contributor
Then you have to do it like that:
Code:
if (e.getSource() == button2) 
{ 
    button1.setEnabled(false);
    SwingUtilities.invokeLater(new Runnable() { 
        public void run() 
        { 
	                   
          try 
          { 
              Thread.sleep(WAIT_TIME_IN_SECONDS * 1000); 
               button1.setEnabled(true);
           } catch (Exception ex) {} 
          // this command will be executed after this ActionEvent has been handled ! 
		                   
        } 
    } 
);
 
G

Gardakor

Gast
Wildcard hat gesagt.:
Then you have to do it like that:
Code:
if (e.getSource() == button2) 
{ 
    button1.setEnabled(false);
    SwingUtilities.invokeLater(new Runnable() { 
        public void run() 
        { 
	                   
          try 
          { 
              Thread.sleep(WAIT_TIME_IN_SECONDS * 1000); 
               button1.setEnabled(true);
           } catch (Exception ex) {} 
          // this command will be executed after this ActionEvent has been handled ! 
		                   
        } 
    } 
);

This looks a lot better, but : the ActionEvents are still fired, however the button is disabled ! :meld:
 

Wildcard

Top Contributor
It's clear that actionEvents are fired because button2 is still enabled. So you can either remove the ActionListener
while the Thread is active, or work with a boolean flag.
 
G

Gardakor

Gast
Wildcard hat gesagt.:
It's clear that actionEvents are fired because button2 is still enabled. So you can either remove the ActionListener
while the Thread is active, or work with a boolean flag.
I mean button1 ; how is it possible that clicking on button1 (while it is disabled) generates an ActionEvent after these 5 seconds ?
 

Wildcard

Top Contributor
Ok, last Version :wink:
Code:
	      if (e.getSource() == button2) { 
			  button1.setEnabled(false);
		         Thread t =  new Thread(){ 
		               public void run() { 
		                   
		                  try { 
		                     sleep(WAIT_TIME_IN_SECONDS * 1000); 
							 button1.setEnabled(true);
		                  } catch (Exception ex) {System.out.println("blupp");} 
		                  // this command will be executed after this ActionEvent has been handled ! 
		                   
		               } 
		            }; 
					t.start();
 
G

Gardakor

Gast
Wildcard hat gesagt.:
Ok, last Version :wink:
Code:
	      if (e.getSource() == button2) { 
			  button1.setEnabled(false);
		         Thread t =  new Thread(){ 
		               public void run() { 
		                   
		                  try { 
		                     sleep(WAIT_TIME_IN_SECONDS * 1000); 
							 button1.setEnabled(true);
		                  } catch (Exception ex) {System.out.println("blupp");} 
		                  // this command will be executed after this ActionEvent has been handled ! 
		                   
		               } 
		            }; 
					t.start();

This works ! I allready implemented it like this, but I was wondering whether there was no more convenient solution.
Thx anyway :wink:
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
D Swing SwingUtils / Thread Problem AWT, Swing, JavaFX & SWT 3
C Thread-/ Simulations- Problem AWT, Swing, JavaFX & SWT 18
P Problem Thread.sleep() und JProgressBar AWT, Swing, JavaFX & SWT 7
Luk10 Swing Problem mit Zeichen-Thread AWT, Swing, JavaFX & SWT 8
H Thread-Problem mit der Darstellung beim Sperren des Fensters AWT, Swing, JavaFX & SWT 2
X Problem bei JTextArea und Thread.sleep() AWT, Swing, JavaFX & SWT 8
T thread.sleep Sprung Problem AWT, Swing, JavaFX & SWT 24
A Sleep Funktion / Thread-Problem ! AWT, Swing, JavaFX & SWT 11
S Problem mit 2 Thread.sleep AWT, Swing, JavaFX & SWT 3
B ActionListener, Thread, JButton Problem AWT, Swing, JavaFX & SWT 2
T Problem mit Oberfläche und Thread AWT, Swing, JavaFX & SWT 10
U Problem mit zweitem Thread AWT, Swing, JavaFX & SWT 10
S Problem mit Thread AWT, Swing, JavaFX & SWT 9
R Thread-Problem (Aktualisierung von JLabel-Komponente) AWT, Swing, JavaFX & SWT 9
M JTextArea in JScrollPane, Problem mit Thread.sleep() AWT, Swing, JavaFX & SWT 5
C Problem beim Ausführen von Thread per Button AWT, Swing, JavaFX & SWT 2
F Problem mit Thread AWT, Swing, JavaFX & SWT 5
Juelin Problem mit TextField.requestFocus(); AWT, Swing, JavaFX & SWT 5
Juelin Problem beim Laden Controller AWT, Swing, JavaFX & SWT 2
G Problem mit der Anzeige von jLabel. Unlesbar wenn der Text geändert wird. AWT, Swing, JavaFX & SWT 28
H 2D-Grafik Problem mit Paint AWT, Swing, JavaFX & SWT 1
S Layout - Problem AWT, Swing, JavaFX & SWT 1
Tassos JavaFX/Problem mit der Maussteuerung in Stackpane AWT, Swing, JavaFX & SWT 7
sserio Java Fx - Problem AWT, Swing, JavaFX & SWT 3
A Problem Spiel auf Panel der GUI zu bringen AWT, Swing, JavaFX & SWT 1
A JavaFX Controller Problem AWT, Swing, JavaFX & SWT 1
TheWhiteShadow JavaFX ListView Problem beim Entfernen von Elementen AWT, Swing, JavaFX & SWT 1
E LayoutManager Welcher Layout-Mix löst mein Problem? AWT, Swing, JavaFX & SWT 3
Umb3rus JavaFX Problem mit PropertyValueFactory: can not read from unreadable property AWT, Swing, JavaFX & SWT 1
T Problem mit paintComponent() AWT, Swing, JavaFX & SWT 17
AmsananKING Java Menü-Problem AWT, Swing, JavaFX & SWT 1
K JavaFX Resizing-Problem beim BorderLayout (Center Component) beim Arbeiten mit mehreren FXMLs AWT, Swing, JavaFX & SWT 2
G Instance OF Problem AWT, Swing, JavaFX & SWT 9
FrittenFritze Ein Problem mit der CSSBox, die Größe wird nicht angepasst AWT, Swing, JavaFX & SWT 5
M Problem mit dem Anzeigen von Frames im Vordergrund AWT, Swing, JavaFX & SWT 5
Badebay Problem mit JButton AWT, Swing, JavaFX & SWT 2
newJavaGeek Grid-Layout problem AWT, Swing, JavaFX & SWT 7
J JavaFX Löschen im Tabelview macht Problem AWT, Swing, JavaFX & SWT 15
JavaTalksToMe JavaFx ExekutorService Problem AWT, Swing, JavaFX & SWT 2
Zrebna Problem bei Eventhandling (Value soll nach jedem erneutem Klick gelöscht werden) AWT, Swing, JavaFX & SWT 4
B Problem mit JavaFX AWT, Swing, JavaFX & SWT 5
J css Problem AWT, Swing, JavaFX & SWT 5
B JavaFX habe mein Problem fett markiert AWT, Swing, JavaFX & SWT 2
A Swing Filter-Problem AWT, Swing, JavaFX & SWT 1
temi JavaFX Problem mit IntelliJ und JavaFx 11 unter XUbuntu AWT, Swing, JavaFX & SWT 3
L Java FX Problem mit Ubuntu 18 und JavaFx AWT, Swing, JavaFX & SWT 27
H JTable TableCellEditor-Problem AWT, Swing, JavaFX & SWT 0
kodela Swing Problem mit Warten-Dialog AWT, Swing, JavaFX & SWT 16
B JavaFx Scene Builder Problem AWT, Swing, JavaFX & SWT 2
B [Problem] Java öffnet Word-Datein nicht AWT, Swing, JavaFX & SWT 14
T DataBinding Problem AWT, Swing, JavaFX & SWT 5
Blender3D Problem mit € Symbol Font Gotham Windows 10 Swing AWT, Swing, JavaFX & SWT 11
T Problem mit JTable Sortierung AWT, Swing, JavaFX & SWT 2
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 15
J Problem mit Platfrom run later AWT, Swing, JavaFX & SWT 0
L JavaFX Problem beim Aufrufen einer Methode AWT, Swing, JavaFX & SWT 5
T Swing Problem mit Datum und FormattedTextField AWT, Swing, JavaFX & SWT 2
S AWT Java print dialog Problem AWT, Swing, JavaFX & SWT 0
olfibits JavaFX Problem mit HTMLEditor AWT, Swing, JavaFX & SWT 0
W SWT hover-background-problem with first column in TreeViewer AWT, Swing, JavaFX & SWT 0
M Problem mit Add JScrollPane AWT, Swing, JavaFX & SWT 25
Mario1409 Swing JTextArea scroll Problem AWT, Swing, JavaFX & SWT 0
N Swing Problem mit loop AWT, Swing, JavaFX & SWT 2
S Swing Problem mit Button und ActionListener AWT, Swing, JavaFX & SWT 5
S Swing & Clean und build Problem AWT, Swing, JavaFX & SWT 12
S JLabel setText() Problem AWT, Swing, JavaFX & SWT 6
I 2D-Grafik Problem beim Ändern der Farbe eine 2d Objekts AWT, Swing, JavaFX & SWT 3
G Swing Splitpane Problem AWT, Swing, JavaFX & SWT 1
F Problem mit der FXML Rectangle Shape AWT, Swing, JavaFX & SWT 2
N JavaFX Stranges Problem mit der Autoscroll-Eigenschaft von Textareas AWT, Swing, JavaFX & SWT 0
E Java FX FXML Problem mit html Scriptausführung AWT, Swing, JavaFX & SWT 2
J JavaFX Intersect Problem mit Shapes AWT, Swing, JavaFX & SWT 10
R JavaFX MediaPlayer AVI-Problem AWT, Swing, JavaFX & SWT 1
M Swing Problem mit ListCellRenderer AWT, Swing, JavaFX & SWT 7
D Problem mit JTable AWT, Swing, JavaFX & SWT 1
F GUI Auflösung ändern - Koordianten und Proportions Problem AWT, Swing, JavaFX & SWT 21
J Problem mit Button darstellung AWT, Swing, JavaFX & SWT 23
M Problem mit Layoutmanagern... Hilfe wäre sehr nett. AWT, Swing, JavaFX & SWT 2
S 2D-Grafik Problem mit Variablen AWT, Swing, JavaFX & SWT 4
7 JavaFX Problem beim Zeichnen eines Dreiecks in einem GUI AWT, Swing, JavaFX & SWT 6
M Swing AttributiveCellTableModel addRow() Problem AWT, Swing, JavaFX & SWT 1
J Swing Problem mit Graphics Methode AWT, Swing, JavaFX & SWT 4
N JavaFX Problem mit table multiple selection AWT, Swing, JavaFX & SWT 5
K CheckBox Problem AWT, Swing, JavaFX & SWT 5
Grevak DisplayMode Problem seit Windows 10 AWT, Swing, JavaFX & SWT 2
S Swing Eigene JComboBox Problem! AWT, Swing, JavaFX & SWT 1
B Swing Problem mit Bildpfad AWT, Swing, JavaFX & SWT 4
N Swing Problem beim Scrollen mit JScrollPane AWT, Swing, JavaFX & SWT 6
V Graphics g - drawOval problem mit background AWT, Swing, JavaFX & SWT 1
C AWT Problem mit Protokol Fenster AWT, Swing, JavaFX & SWT 0
M Swing pack() Problem mit Taskleiste AWT, Swing, JavaFX & SWT 4
N Swing Choice- Problem! AWT, Swing, JavaFX & SWT 8
Q "AWT-EventQueue-0" Exception Problem AWT, Swing, JavaFX & SWT 4
D jButton Problem, ein Rieser Button bedeckt das ganze frame AWT, Swing, JavaFX & SWT 1
A Problem: repaint() - Schleife AWT, Swing, JavaFX & SWT 3
J Anfänger GUI Problem bei der Ausführung eines sehr einfachen Programms AWT, Swing, JavaFX & SWT 2
P AWT Problem mit Platzierung (GridBagLayout) AWT, Swing, JavaFX & SWT 2
N Swing JTree Problem beim erstellen der Knoten AWT, Swing, JavaFX & SWT 0
N Swing CardLayout: Problem beim Wechsel zwischen den JPanels AWT, Swing, JavaFX & SWT 3
A Mini-Menu-Schriften. Ein Problem bei hohen DPI Zahlen AWT, Swing, JavaFX & SWT 2

Ähnliche Java Themen


Oben