1

This is a music player. It works fine, but when I try to create a Jframe around I fail.

I have 3 jTextfields that should present me the playtime and the remaining playtime. But in the program they dont update.

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
//
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequencer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.*;
import javax.swing.event.*;

/**
  *
  * Beschreibung
  *
  * @version 1.0 vom 26.10.2015
  * @author 
  */

public class test extends JFrame  {
  // Anfang Attribute
  private static File chossen = new File("");
  private static int x=1;

  private static JTextField jTextField1 = new JTextField();
  private static JTextField jTextField2 = new JTextField();
  private static JTextField jTextField3 = new JTextField();
  private JLabel jLabel1 = new JLabel();
  private JLabel jLabel2 = new JLabel();
  private JLabel jLabel3 = new JLabel();
  private static JProgressBar jProgressBar1 = new JProgressBar();
  private JButton jButton1 = new JButton();
  private JButton jButton2 = new JButton();
  private JButton jButton3 = new JButton();
  // Ende Attribute

  public test(String title) throws MidiUnavailableException { 
    // Frame-Initialisierung

    super(title);

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    int frameWidth = 574; 
    int frameHeight = 412;
    setSize(frameWidth, frameHeight);
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (d.width - getSize().width) / 2;
    int y = (d.height - getSize().height) / 2;
    setLocation(x, y);
    setResizable(false);
    Container cp = getContentPane();
    cp.setLayout(null);
    // Anfang Komponenten

    jTextField1.setBounds(400, 8, 150, 20);
    cp.add(jTextField1);
    jTextField2.setBounds(400, 32, 150, 20);
    cp.add(jTextField2);
    jTextField3.setBounds(400, 56, 150, 20);
    cp.add(jTextField3);
    jLabel1.setBounds(288, 8, 110, 20);
    jLabel1.setText("Gesamte Dauer");
    cp.add(jLabel1);
    jLabel2.setBounds(288, 32, 110, 20);
    jLabel2.setText("Aktuelle Dauer");
    cp.add(jLabel2);
    jLabel3.setBounds(288, 56, 110, 20);
    jLabel3.setText("Verbleibend");
    cp.add(jLabel3);
    jProgressBar1.setBounds(280, 88, 270, 16);
    cp.add(jProgressBar1);
    jButton1.setBounds(480, 344, 75, 25);
    jButton1.setText("Start");
    jButton1.setMargin(new Insets(2, 2, 2, 2));
    jButton1.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        try {
            jButton1_ActionPerformed(evt);
        } catch (MidiUnavailableException | InvalidMidiDataException |                      IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
    });
    cp.add(jButton1);
    jButton2.setBounds(400, 344, 75, 25);
    jButton2.setText("Stop");
    jButton2.setMargin(new Insets(2, 2, 2, 2));
    jButton2.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        try {
            jButton2_ActionPerformed(evt);
        } catch (MidiUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidMidiDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
    });
    cp.add(jButton2);
    jButton3.setBounds(8, 8, 267, 73);
    jButton3.setText("Dateiauswahl");
    jButton3.setMargin(new Insets(2, 2, 2, 2));
    jButton3.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
        jButton3_ActionPerformed(evt);
      }
    });
    cp.add(jButton3);
    // Ende Komponenten

    setVisible(true);
  } // end of public test

  // Anfang Methoden

  public static void main(String[] args) throws MidiUnavailableException {

    new test("test");

  } // end of main

  public void test(int zahl) throws MidiUnavailableException,     InvalidMidiDataException, IOException, InterruptedException{
      final Sequencer sequencer = MidiSystem.getSequencer();
      sequencer.open();
      if(zahl==2){
          sequencer.setSequence(MidiSystem.getSequence(chossen));


            sequencer.setTempoFactor(1);
            sequencer.start();
            System.out.println("start");
            Thread.sleep(5000);
            System.out.println("tettstdt");

            x=1;
            while(x==1){
                long z = sequencer.getMicrosecondLength()/1000/1000;
                long y=sequencer.getMicrosecondPosition()/1000/1000;
                jProgressBar1.setMinimum(0);
                jProgressBar1.setMaximum((int) z);
                jTextField1.setText(z+"s");


                z = sequencer.getMicrosecondLength()/1000/1000;
                y=sequencer.getMicrosecondPosition()/1000/1000; 
                System.out.println(z+" "+y);
                jProgressBar1.setValue((int) y);
                jTextField2.setText(y+"s");
                jTextField3.setText("s");
                if(z-y==0){
                    x=0;
                }


            }    
      }else if(zahl==3){
          System.out.println("stop fehlerhaft");
          sequencer.stop();
      }else{
          System.out.println("fehler");
      }


  }    


  public void jButton1_ActionPerformed(ActionEvent evt) throws MidiUnavailableException, InvalidMidiDataException, IOException, InterruptedException {
      System.out.println(chossen);


      test(2);






  } 

  public void jButton2_ActionPerformed(ActionEvent evt) throws     MidiUnavailableException, InvalidMidiDataException, IOException {
     // test(3);

      }     
  public void jButton3_ActionPerformed(ActionEvent evt) {
      File chossen2 = new File("C:/Users/Eric/workspace/Komplex/midi/"+    (dic()));
      chossen = chossen2;
  } 



  public static String dic() {

    File f = new File("C:/Users/Eric/workspace/Komplex/midi");
    JFileChooser fc = new JFileChooser();
    fc.setCurrentDirectory(f);


    int state = fc.showOpenDialog( null );

    if ( state == JFileChooser.APPROVE_OPTION )
    {    
      File file = fc.getSelectedFile();

      return file.getName();
    }
    else

    return "";


  }    
}

When I print the information to console it works.

Vogel612
  • 5,620
  • 5
  • 48
  • 73
Eric
  • 11
  • 2
  • 2
    You need to do long lasting operations out of the event dispatch thread. While that thread is blocked, no drawing can happen. See [about worker threads](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html). – kiheru Oct 27 '15 at 10:46
  • 1
    Suggestions: 1) As @kiheru says, make sure that you do long-running tasks off of the Swing event thread and in a background thread, such as by properly using a SwingWorker. Code to get in there includes your `Thread.sleep(...)` and your `while (x == 1)` block. 2) Avoid over-use of the static modifier as you're currently doing. Over-use leads to creation of hard to debug, enhance and re-use programs. 3) Same for use of the `null` layout and `setBounds` as this leads to creation of rigid GUI's that are very difficult to enhance or modify. Use the layout managers instead. – Hovercraft Full Of Eels Oct 27 '15 at 19:15

1 Answers1

-2

Call the repaint() method to force the JTextField to repaint itself using the current options set.

bdparrish
  • 3,216
  • 3
  • 37
  • 58
  • 2
    This is almost always wrong. Calling `setText(...)` updates the JTextField's model, it's Document, which automatically tiriggers a painting of the JTextField. – Hovercraft Full Of Eels Oct 27 '15 at 10:55