0

I have created a for loop that detects the amount of devices connected to the computer. This then creates 2 JFrames, so every devices has it's own window. I now want to add data to those windows, so I created a while loop inside the for loop. But this stops the for loop from completing, meaning that only one JFrame shows data, and the second JFrame is not visible. If I remove the while loop from the for loop then 2 windows appear, but only one window is showing data, and the other window is blank. So the question is how do I get multiple while loops running to show data on multiple JFrames, based on a for loop.

for(int i = 0; i < controllers.length; i++) {
        if(controllers[i].getType() == Controller.Type.STICK) {
            window = new JFrameWindow();

    while(true)
    {
  • 1
    Remove the `while(true)` and use a real condition that can evaluate to `false` and let the loop end at some point. – BackSlash Mar 19 '17 at 11:46
  • Maybe you can look into [Observer Pattern](https://en.wikipedia.org/wiki/Observer_pattern) – Shinigami Mar 19 '17 at 11:46
  • @BackSlash But the data is constantly changing. The while loop only breaks when the the device is removed from the computer. –  Mar 19 '17 at 11:47
  • 2
    @AndrewMontague Use a thread then. Or use an Observer Pattern as Shinigami suggested. A while loop like this will make your program hang forever, and you don't want this to happen. – BackSlash Mar 19 '17 at 11:57
  • Consider having one `JFrame` holding multiple components to display the data. – c0der Mar 19 '17 at 13:21

1 Answers1

0

every while loop have test conditions when it is true it will continue to loop when you don't have a control variable that will stop the application or make the test condition to be false an infinite loop will occur..same with other loop(for loop, do while loop) think of another method so that your while loop will not continue to loop forever so that you can execute your for loop again

0xDEADBEEF
  • 590
  • 1
  • 5
  • 16