Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:28:50+00:00 2026-06-14T05:28:50+00:00

It seems that in my application, the amount of times a method is executed

  • 0

It seems that in my application, the amount of times a method is executed when clicking a JButton is related to the amount of times I resize the JFrame. In the example below, when I start the application and don’t resize at all, the method addPanel(Dimension dim) is executed 2 times. After resizing one time, even more at once. How is this explainable? Does the ResizeController has any influence on this?
(The outcommented lines are concerning two other “screens”, that in this case don’t matter).

Start.java

public class Start {

    public static void main(String[] arg){

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GroundFrame(Toolkit.getDefaultToolkit().getScreenSize());
            }
        });

    }
}

GroundFrame.java

public class GroundFrame extends JFrame{

    public static final long    serialVersionUID = 55;
    private Container           contentPane;
    public static Flyer         screenFlyer;
    public static Points        screenPoints;
    public static Sponsoren     screenSponsoren;
    public static JFrame        parent;

    public GroundFrame(Dimension screenDimension){
        createAndShowGUI(screenDimension);
        setVisible(true);
    }

    private void createAndShowGUI(Dimension screenDimension){
        setExtendedState(JFrame.MAXIMIZED_BOTH); 

        parent = this;

        contentPane = getContentPane();
        contentPane.setLayout(null);

        //screenFlyer = new Flyer(screenDimension);
        //screenSponsoren = new Sponsoren(screenDimension);
        //screenSponsoren.setVisible(false);
        screenPoints = new Points(screenDimension);
        //screenPoints.setVisible(false);

        ResizeController controller = new ResizeController();
        controller.start();

        //contentPane.add(screenFlyer);
        //contentPane.add(screenSponsoren);
        contentPane.add(screenPoints);
    }

    public static void exit(){
        System.exit(0);
    }
}

Points.java

public class Points extends JPanel{

    public static final long            serialVersionUID = 55;

    private JButton                     buttonNext, buttonExit, buttonAdd;
    private JLabel                      copyright;

    private Vector<PlayerPointsPanel>   panels = new Vector<PlayerPointsPanel>();
    final Color                         BACKGROUND_COLOR = new Color(60,60,60);                         
    private int                         pressed = 0;


    public Points(Dimension screenDimension){
        setLayout(null);
        setBackground(BACKGROUND_COLOR);
        setPreferredSize(screenDimension);
        initialize(screenDimension);
        setupElements(screenDimension);
    }

    private void initialize(Dimension dim){
        copyright = new JLabel("\u00a9 by Valentino Rugolo (2012)", SwingConstants.CENTER);
        buttonAdd = new JButton("+");
        buttonNext = new JButton("");
        buttonExit = new JButton("X");
        /*for(int i=0; i<8; i++){
            PlayerPointsPanel panel = new PlayerPointsPanel(dim, i);
            add(panel);
            panels.add(panel);
        }*/
    }

    private void setupElements(Dimension dim){
        setBounds(0,0,dim.width, dim.height);
        setLayout(null);
        dimensionCP = dim;

        copyright.setBounds((int)(dim.width*0.82), (int)(dim.height*0.9), (int)(dim.width*0.15), (int)(dim.height*0.05));
        copyright.setFont(new Font("Times New Roman", Font.PLAIN, (int)(dim.height*0.017)));
        copyright.setForeground(Color.white);

        buttonNext.setBounds((int)(dim.width*0.01), (int)(dim.height*0.9), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonNext.setBackground(Color.DARK_GRAY);
        buttonNext.setForeground(Color.white);
        buttonNext.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GroundFrame.screenPoints.setVisible(false);
                GroundFrame.screenFlyer.setVisible(true);
            }
        });

        buttonExit.setBackground(Color.DARK_GRAY);
        buttonExit.setForeground(Color.white);
        buttonExit.setBounds((int)(dim.width*0.95), (int)(dim.height*0.01), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                GroundFrame.exit();
            }
        });

        buttonAdd.setBackground(Color.DARK_GRAY);
        buttonAdd.setForeground(Color.white);
        buttonAdd.setBounds((int)(dim.width*0.01), (int)(dim.height*0.01), (int)(dim.width*0.04), (int)(dim.height*0.04));
        buttonAdd.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg1) {
                System.out.println("pressed " + pressed);
                pressed++;  
            }
        });

        add(buttonExit);
        add(buttonNext);
        add(copyright);
        add(buttonAdd);
    }

    public void resizeContent(Dimension dim){
        /*for(int i=0; i<panels.size(); i++){
            panels.elementAt(i).resizeContent(dim, i);
        }*/
        setupElements(dim);
    }
}

ResizeController.java

public class ResizeController extends Thread{

    public int sizebeforeX = GroundFrame.parent.getWidth();
    public int sizebeforeY = GroundFrame.parent.getHeight();

    public ResizeController(){

    }

    public void run(){
        while(true){

            int actualX = GroundFrame.parent.getWidth();
            int actualY = GroundFrame.parent.getHeight();

            if(sizebeforeX != actualX || sizebeforeY != actualY){
                //GroundFrame.screenFlyer.resizeContent(GroundFrame.parent.getSize());
                GroundFrame.screenPoints.resizeContent(GroundFrame.parent.getSize());
                //GroundFrame.screenSponsoren.resizeContent(GroundFrame.parent.getSize());
                sizebeforeX = actualX;
                sizebeforeY = actualY;
            }

            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:28:52+00:00Added an answer on June 14, 2026 at 5:28 am

    Whenever you’re resizing the content of the frame you’re calling setupElements() which is adding listeners to the buttons, over and over again.
    Just move the code of adding the listeners in the initialize() method of the frame.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a windowless Ogre application, but it seems that the method
It seems that when a WPF application starts, nothing has focus. This is really
I am planning to write a Swing-based application (using Netbeans 6.8). It seems that
The current application that I'm working on seems to recycle the application pool very
Using local variables seems advisable in a partial that could be used application-wide to
It seems that there is a large amount of information about saving Activity state,
I want to build an application that can set up a set amount of
Seems that This will be an easy question for you but this problem is
Seems that even after unchecking the option in the PyDev/Debug preferenecs pane to launch
It seems that we can show layers and even use a different zPosition for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.