Question

Using Jinput and Java in Netbeans, I'm working on a very small project that simply Pops up a JFrame alarm window when lets say a user presses down on the 'K' on the keyboard and terminates the JFrame alarm window when the user lets go of 'k'. In my code, I seemed to get stuck in the while loop as the JFrame opened on the first press down and couldn't seem to close. I researched and I found that using javax.swing.Timer was the better way to do it. However, since I'm a newbie at this, all the different ways to use timer just made me even more confused. Could someone please see my code and point me in the right direction?

Here is my code;

public void startPolling() {
    while(true) {
        ControllerEnvironment.getDefaultEnvironment().getControllers();                
        ca[index].poll();  
        EventQueue queue = ca[index].getEventQueue();
        Event event = new Event();
        while(queue.getNextEvent(event)) {
           StringBuffer buffer = new StringBuffer(ca[index].getName());
           buffer.append(" at ");
           buffer.append(event.getNanos()).append(", ");
           Component comp = event.getComponent();


           buffer.append(comp.getName()).append(" changed to ");
           float value = event.getValue(); 
           if(comp.isAnalog()) {
              buffer.append(value);
           } else {
              if(value==1.0f) {
                 buffer.append("On");
                 if ("K".equals(comp.getName())){
                    alarmBox();
                 }
              } else {
                 buffer.append("Off");
                 if ("K".equals(comp.getName())){
                    alarmBox.setVisible(false);
                 }
              }
           }
           System.out.println(buffer.toString());
        }
  }      
}

alarmBox() is my JFrame.

I was working on it and here is my updated code:

public void startPolling() {

    Timer timer = new Timer(50, new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            ca[index].poll();  
        EventQueue queue = ca[index].getEventQueue();
        Event event = new Event();
        while(queue.getNextEvent(event)) {
           StringBuffer buffer = new StringBuffer(ca[index].getName());
           buffer.append(" at ");
           buffer.append(event.getNanos()).append(", ");
           Component comp = event.getComponent();

           buffer.append(comp.getName()).append(" changed to ");
           float value = event.getValue(); 
           if(comp.isAnalog()) {
              buffer.append(value);
           } else {
              if(value==1.0f) {
                 buffer.append("On");
                 if ("K".equals(comp.getName())){
                    alarmBox();

                 }
              } else {
                 buffer.append("Off");
                 if ("K".equals(comp.getName())){
                    alarmBox.dispose();
                 }
              }
           }
           System.out.println(buffer.toString());
        }

     try {
        Thread.sleep(20);
     } catch (InterruptedException f) {
        f.printStackTrace();
     }


        }

}); timer.start();
Était-ce utile?

La solution

if you just want to open and close window,y to use timer? you have a very complicated code,for a simple task. you can add a ComponentListener to your JFrame to hide,somthing like this:

frame.addComponentListener(new ComponentAdapter(){

 public void componentMoved(ComponentEvent e) {
     if (popup.isVisible()){
         popup.setVisible(false);
     }
  }
  });
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top