GUI friert ein nur warum?

Status
Nicht offen für weitere Antworten.

Java Chris

Bekanntes Mitglied
Code:
private void loading(String arg0) {
		frame.setSize(260,70);
		frame.setLocation((java.awt.Toolkit.getDefaultToolkit().getScreenSize().width-frame.getWidth())/2,(java.awt.Toolkit.getDefaultToolkit().getScreenSize().height-frame.getHeight())/2);
		
		JProgressBar load = new JProgressBar();
		load.setMaximum(200000);
		load.setValue(0);
		load.setVisible(true);
		load.setLocation(15, 10);
		load.setSize(180, 20);
		
		container.removeAll();
		container.add(load);
		container.validate();
		container.repaint();
		
		for(int i=0; i<=20000;i++) {
			load.setValue(load.getValue()+1);
			new Thread(){
				public void run() {
					super.run();
					
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}.start();
		}
	}
 

The_S

Top Contributor
Evtl. weil du ca. 20000 (keine übertriebene Zahl :p , hätte auch nie gedacht dass ich das so ma einsetzen würde) Threads unmittelbar hintereinander lostrittst!? Dadurch kommts wohl zu Verzögerungen UND Swing ist nicht Thread-Sicher. Schau dir lieber nochmal das JProgressBar Tutorial (z. B. hier im Forum) an, vielleicht findest du ja dann deinen Fehler ;) .
 

Java Chris

Bekanntes Mitglied
die zahl is ja wurscht, will ja nur mal nen fortschrittsehen, aber der thread is doch korrekt, or? also ka was da falsch drann is
 

The_S

Top Contributor
Hier mal 2 Schlagworte/Halbsätze ( :p ) für dich:

1. Threads außerhalb der Schleife starten
2. SwingUtilities#invokeLater

ansonsten kann ich dir echt nur das Tutorial in den FAQ hier empfehlen.
 

Java Chris

Bekanntes Mitglied
irgendwie häng ich voll...

ich hab jetzt 2 classen erstellen einmal die runnable und einmal den "algo"

Code:
public class RunforIt implements Runnable{
	private JProgressBar bar;
	
	public RunforIt(JProgressBar bar) {
		this.bar = bar;
	}
	
	public void run(){
	      new Runit().run(bar);   
	} 
}
Code:
public class Runit{
	private JProgressBar status; 
	public void run(JProgressBar bar){
		 status = bar;
		 for(int i = 0; i<=20000;i++) {
			 try{
		            Thread.sleep( 100 ); // Kurz warten
		         }
		         catch( InterruptedException e ){
		         }
		         setValue( i ); // Hey! Es ist was passiert!
		 }
	}
	private void setValue( final int value ) {

	      SwingUtilities.invokeLater( new Runnable(){
	         public void run() {
	            try{
	               status.setValue( value );
	            }
	            catch( NullPointerException ex ){
	               // silent exception
	            }
	         }
	      });
	   } 
}

das meiste davon ist aus dem tut. aber funktionieren tuts nicht... dass ganze zeug mit interrupt hin oder her brauch ich nicht
 

The_S

Top Contributor
Ein kleines (schnell dahingeklatschtes) Beispiel:

Code:
import javax.swing.*;

public class ProgressExample extends JFrame {
	
	private JProgressBar bar = null;
	private int zaehler = 0;
	
	public ProgressExample() {
		
		bar = new JProgressBar();
		bar.setMaximum(20000);
		getContentPane().add(bar);
		pack();
	}
	
	public void load() {
		
		new Thread(new Runnable() {
			public void run() {
				while (zaehler < 20001) {
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							bar.setValue(zaehler++);
						}
					});
					try {
						Thread.sleep(5);
					}
					catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}
	
	public static void main(String[] args) {
		
		ProgressExample pe = new ProgressExample();
		pe.setVisible(true);
		pe.load();
	}
}
 
Status
Nicht offen für weitere Antworten.

Ähnliche Java Themen

Neue Themen


Oben