简体   繁体   中英

Buttons are not displaying on Frame while waiting for a Process to complete in JAVA

//Frame got visible but buttons on it are not displaying. ProcessBuilder to execute a.sh and we need to wait for completion of this process . Call to showFrame() display the Frames without button and labels but as soon as the Process completes complete Frame become visible.

    showFrame();
            Process test=null;
            try {
                test = new ProcessBuilder("sh" ,"config/a.sh").start(); 

                test.waitFor();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
   }
        private void showFrame() {
            JFrame fr = new JFrame("Operations");
            fr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            fr.setBounds(100,100,600,500);
            fr.setSize(500, 500);
            JButton b1 = new JButton("PORT30");
            JLabel l1 = new JLabel("TRACKER");
            l1.setBounds(100,70,100,100);
            fr.setLayout(null);
            JLabel l2 = new JLabel("NODE");
            l2.setBounds(100,170,100,100);
            b1.setBounds(300,100,160,50);
            JButton b2 = new JButton("PORT70");
            b2.setBounds(300,200,160,50);
            fr.add(b1);
            fr.add(b2);
            fr.add(l2);
            fr.add(l1);
            fr.setVisible(true);
            l1.setVisible(true);
            l2.setVisible(true);
            b1.setVisible(true);
            b2.setVisible(true);

test.waitFor(); is a blocking call, if executed from within the context of the Event Dispatching Thread, this will prevent the UI from been updated as it's unable to process events on the Event Queue

Swing is single threaded, meaning that anything that blocks the EDT will prevent it from process new events from the Event Queue, it is also not thread safe, meaning that you should never update or modify the UI from any other thread other then the EDT

See Concurrency in Swing for more details.

A solution would be to allow the Process to execute within it's own Thread and use some kind of Observer Pattern to obtain results of the Process

Something like this for example or even this for example.

Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM