Stanko
Stanko

Reputation: 4475

JDialog opening very slow

I'm having some problems while working on a project for school. The JDialog renders very slow. Let me show you.

This is the GUI:

GUI

When I click "Advance time" it takes around 3 seconds to open the JDialog. When it's open I get this (which is fine):

Save screen

But when I drag that JDialog around, I get this (which is not ok):

Slow

It also takes very long to close the JDialog. It closes, but you can still see it:

close

This is the snippet of code where I think the problem lays, it's the code for the menu-bar:

private JMenu editMenu()
{
    JMenu editMenu = new JMenu("Edit");
    editMenu.setMnemonic(KeyEvent.VK_E);

    JMenuItem advanceTimeMenuItem = new JMenuItem("Advance time");
    advanceTimeMenuItem.setMnemonic(KeyEvent.VK_A);
    advanceTimeMenuItem.setToolTipText("Advance the internal clock");
    advanceTimeMenuItem.addActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent event) 
        {
            //We first create panel to add to the dialog.
            JPanel panel = new JPanel();
            //Calendar
            AdvanceTimePanel calendar = new AdvanceTimePanel(internalClockController);

            //Button that will be used to confirm the system time change.
            JButton btnSave = new JButton("Save");
            //Add actionlistener to the save button
            btnSave.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    Date selectedDate = calendar.getDate();
                    try
                    {                           
                        internalClockController.updateDateTime(selectedDate);                           
                    } catch (InvalidInternalClockException e1)
                    {
                        System.out.println("InvalidInternalClockException: " + e1.getMessage());
                    }
                }
            });
            //Add the components to the panel.
            panel.add(btnSave);
            //This is the calendar added to the panel.
            panel.add(calendar);
            //Create the dialog and add the panel to it.
            JDialog jDialog = new JDialog();
            jDialog.add(panel);
            jDialog.setBounds(100, 100, 400, 200);
            jDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            jDialog.setVisible(true);
        }


    });

    editMenu.add(advanceTimeMenuItem);

Does someone have an idea how to speed things up? Thanks in advance.

Upvotes: 0

Views: 742

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109593

Events are handled on one single thread. (Also repaint events.) For the application to remain responsive, perform longer actions a bit later by using invokeLater, as below:

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ... // All code here
            }
        });
    }

This is quite wordy; Java 8 allows:

advanceTimeMenuItem.addActionListener(
        (event) -> {
            EventQueue.invokeLater(
                () -> {
                    ... // All code
                });
        });

Upvotes: 1

Related Questions