0

Well i got a problem guys and request your help. I added in my own kind of JPopupMenu to allow changing calculation methods, but now my whole application is slowen down significantly and I narrowed it down to this classes i made :

class PopUpTimeCalcMethodChangeClickListener extends MouseAdapter {
    PopUpMenuTimeCalcMethodChange menu;
    public void mousePressed(MouseEvent e){
        if (e.isPopupTrigger())
            doPop(e);
    }

    public void mouseReleased(MouseEvent e){
        if (e.isPopupTrigger())
            doPop(e);
    }

    private void doPop(MouseEvent e){
        if(menu == null)
            menu = new PopUpMenuTimeCalcMethodChange();         
        menu.show(e.getComponent(), 0, 0);
    }
}

class PopUpMenuTimeCalcMethodChange extends JPopupMenu {
    public PopUpMenuTimeCalcMethodChange(){
        final String[] calctyps = {"Calculate by last download","Calculate by average speed"};
        for(int i = 0;i<calctyps.length;i++){
            final JCheckBox setCalcMethod = new JCheckBox(calctyps[i]);
            setCalcMethod.setIconTextGap(15);
            if(Main.TimeLeftCalculationMode == i)
                setCalcMethod.setSelected(true);
            setCalcMethod.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(Component c : getmenu().getComponents()){
                        if(c.getClass() == JCheckBox.class){
                            ((JCheckBox) c).setSelected(false);
                        }
                    }
                    setCalcMethod.setSelected(true);
                    System.out.println("lol we are running");
                    int index = -1;
                    for (int i=0;i<calctyps.length;i++) {
                        if (calctyps[i].equals(setCalcMethod.getText())) {
                            index = i;
                            break;
                        }
                    }

                    Main.TimeLeftCalculationMode = index;
                }
            });
            add(setCalcMethod);
        }
    }

    public JPopupMenu getmenu(){
        return this;
    }
}

the main problem is that the code is slowing down the ui after its first execution even though its not visible anymore.I assume its something about the actionlisteners but im not sure.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jan b
  • 29
  • 7

1 Answers1

0

Are you sure the slowdown is caused by the classes you put in your question? If I take your PopUpMenuTimeCalcMethodChange class and add a simple Main class, the application is not slowing down for me. How much slowdown do you notice? You could print the time at certain points in your application to get some measurements. Can you add more of your code to make sure the issue is not somewhere else? (Your PopUpTimeCalcMethodChangeClickListener class does not seem to be used by the PopUpMenuTimeCalcMethodChange class.)

This is the code I tried:

// Main class:

import javax.swing.*;

public class Main {
    protected static int TimeLeftCalculationMode;

    public static void main(final String[] arguments) {
        new Main().launchGui();
    }

    private void launchGui() {
        final JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setComponentPopupMenu(new PopUpMenuTimeCalcMethodChange());
        frame.getContentPane().add(panel);

        frame.setVisible(true);
    }
}


// PopUpMenuTimeCalcMethodChange class:

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

public class PopUpMenuTimeCalcMethodChange extends JPopupMenu {
    public PopUpMenuTimeCalcMethodChange(){
        final String[] calctyps = {"Calculate by last download",
                                   "Calculate by average speed"};
        for(int i = 0;i<calctyps.length;i++){
            final JCheckBox setCalcMethod = new JCheckBox(calctyps[i]);
            setCalcMethod.setIconTextGap(15);
            if(Main.TimeLeftCalculationMode == i)
                setCalcMethod.setSelected(true);
            setCalcMethod.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(Component c : getmenu().getComponents()){
                        if(c.getClass() == JCheckBox.class){
                            ((JCheckBox) c).setSelected(false);
                        }
                    }
                    setCalcMethod.setSelected(true);
                    System.out.println("lol we are running");
                    int index = -1;
                    for (int i=0;i<calctyps.length;i++) {
                        if (calctyps[i].equals(setCalcMethod.getText())) {
                            index = i;
                            break;
                        }
                    }

                    Main.TimeLeftCalculationMode = index;
                }
            });
            add(setCalcMethod);
        }
    }

    public JPopupMenu getmenu(){
        return this;
    }
}
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • After a whole afternoon of try and error im pretty sure the mentiont class isn't the only source of trouble its more like the last drop that crushes my application. Thank you for offering your time :) – jan b Nov 21 '15 at 18:43
  • Good luck investigating the issue. There are several good Java profilers that could be helpful. See for example [Open Source Java Profilers](http://stackoverflow.com/questions/948549/open-source-java-profilers) for suggestions. – Freek de Bruijn Nov 24 '15 at 10:08