1

I have been learning java GUI(swing to be precise) by referring online sources and practising. Code:(p is a JButton)

    p.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Welcome to Guess the number Game");
        System.out.println("You have 3 chances to guess a number between 0 and 10 excluding 10");
        ne.remove(p);
        ne.revalidate();
        ne.repaint();
        l.dispose(); //l is a JFrame
        gamer2 game=new gamer2();
        game.generatenum(); //works on the console
        l.setVisible(true);
       }}); 

Problems:

  • In some related questions I posted earlier,I learnt that any update/change done to java GUI will only be effective after the actionPerformed() is completely executed. But the thing is l.dispose() works or the Jframe l disappears even before generatenum() function is completely executed which means actionPerformed() is still not completed executing but the JFrame disappears. generatenum() runs on the console.The thing is button is removed only after actionPerformed() is completely executed but why is this different in case of the frame.
  • I am also aware that java control flows from line to line(atleast in the above example).The JFrame reappers since I have written l.setVisible(true);. But this happens before generatenum() is completely executed. The generatenum() will only stop running if I enter a suitable number on the console.So how is the control jumping to l.setVisible(true) before the previous line/function execution is completed.

What is generatenum()?

It is a function which accepts user input on the Eclipse console.It doesn't stop running unless it receives a valid input from the user.
void generatenum() 
{
    int ran=(int)(Math.random()*10);
    System.out.println("For developer-no.selected "+ran);
    getUserInput(ran);

}

void getUserInput(int k)
{

    i++;
    System.out.println("print now-Chance "+i);

    g.gotValue=k;
    InputStreamReader j=new InputStreamReader(System.in);
    BufferedReader s=new BufferedReader(j);
    try {
        int getIt1=Integer.parseInt(s.readLine());

        g.getIt=getIt1;
    }  catch (IOException e) 
    {
     e.printStackTrace();
        }
}

generatenum() works on the console.

Mathews Mathai
  • 1,707
  • 13
  • 31

1 Answers1

2

The frame visibillity cahnges (wich is set to false on dispose) takes place not in the GUI Message Loop (called AWT Event-Dispatching Thread for AWT/Swing). With the following test program one see the frame (window) disappear and reapear every second after the button is clicked once.

Everything on the frame (like the button in the test program) will not be repainted because the message loop is busy. The message loop (see link above) is the loop in wich all the GUI events are actually executed. In other frameworks like WPF framework or WinForms for C# or SWT for Java one gets an exception if a GUI is accesed from another thread.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    public static void main(String[] args) {
        final JFrame f1=new JFrame();
        f1.setBounds(100,100,100,100);
        final Button b = new Button();
        b.setLabel("Test");
        b.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e1) {
                while(true){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    f1.setVisible(!f1.isVisible());
                }
            }
        });
        f1.add(b);
        f1.setVisible(true);

    }
}
Community
  • 1
  • 1
Mikey
  • 629
  • 1
  • 11
  • 19
  • Can you add a bit more explanation?....Like how a GUI message loop works?...and what do you mean by `Everything on the frame (like the button) will not be painted because the message loop is busy.`??...Can you explain it with respect to my question?..Please! – Mathews Mathai Dec 30 '15 at 13:44
  • Added some explanation and a link: hope it helps – Mikey Dec 30 '15 at 14:06
  • Okay.I got it.But the problem is doesn't `setting visibility on/off` work in EDT?..Where does it run?...and in my case...The button removal works only after `actionPerformed()` is completely executed but `setting visibility on/off` works before that...Can you throw some light on this particular problem? – Mathews Mathai Dec 30 '15 at 14:18
  • I do not know it for sure, but i think the visibility changes of the frame are transmitted to the Window Manager of the OS and could be executed instantly (it may behave differently on other OS). The removal of a button on other hand is a change on the frame: so it have to wait for EDT. – Mikey Dec 30 '15 at 14:27
  • Wow...Thanks a lot @Mikey .This comment answers my question and your answer and the link you provided is very helpful. – Mathews Mathai Dec 30 '15 at 14:29