1

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();
Humble Hermit
  • 125
  • 1
  • 8

1 Answers1

0

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);
     }
  }
  });
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • 1
    Im actually working on a bigger project but I scaled it down so that I could understand it better. What I'm aiming for is the user to press a letter and a JFrame pops up, when the user lets go the JFrame is disposed. I have been working on it since the last post and this is my code: – Humble Hermit Nov 14 '12 at 13:39
  • so cant u dispose it when it looses focus? – joey rohan Nov 14 '12 at 13:41
  • I cant dispose of the JFrame from inside my ActionPerformed block. I posted my updated code in my first post...apologize if these sounds silly, Im a newbie. – Humble Hermit Nov 14 '12 at 13:48