user1089146
user1089146

Reputation: 127

frame shows black screen

btnnew.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                System.out.println("Hello");
                packetListener.listener();
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });

I get a black screen when it runs. But when the packetListener.listener(); calls in the constructor it shows.

Can you please explain why this is happening?

Upvotes: 0

Views: 1371

Answers (2)

sonu thomas
sonu thomas

Reputation: 2161

I think the packetListener.listener(); method performs some complex operation which blocks your UI.

Better create a thread for listening the packet. ie,use it like this

 try {
    System.out.println("Hello");
    new Thread(new Runnable() {
        public void run() {
packetListener.listener();
            }
    }).start();         

        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

Hope this helps you

Upvotes: 1

camickr
camickr

Reputation: 324137

Code that is executed from a listener executes on the EDT. I'm guessing that the packetListner.listener() method blocks in which case the GUI will freeze. You should not be blocking the EDT.

Read the section from the Swing tutorial on Concurrency for a full description of this problem and a solution.

Upvotes: 3

Related Questions