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 5971315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:29:59+00:00 2026-05-22T20:29:59+00:00

Hey guys, I have a problem with a code that I’ve been writing. I

  • 0

Hey guys, I have a problem with a code that I’ve been writing.

I have a JFrame that contains two buttons. Each of these buttons has an action. The problem I’m having is with a JButton called "btnDone" that’s supposed to get back to a previous screen. If I I keep pushing the button repeatedly, eventually the "btnDone" would stop doing the logic it’s supposed to do. My code is as follows:

For the frame:

public class ItemLocatorPnl extends JPnl
{
    private static final long serialVersionUID = 1L;
    private Pnl pnl;
    private JButton btnDone;
    private JButton btnRefreshData;

    public void setPnl(Pnl pnl) {
        this.pnl = pnl;
    }

    public ItemLocatorPnl(Pnl pnl)
    {
        super();

        this.pnl=pnl;
        initialize();   
    }


    private void initialize()
    {
        this.setSize(300, 200);

        JPanel jContentPane = new JPanel();
        jContentPane.setLayout(new MigLayout());

        // (1) Remove window frame
        setUndecorated(true);


        // (3) Set background to white
        jContentPane.setBackground(Color.white);


        // (5) Add components to the JPnl's contentPane
        POSLoggers.initLog.writeDebug("ItemLocator: Adding icon");
        jContentPane.add(wmIconLabel, "align left");
        POSLoggers.initLog.writeDebug("ItemLocator: Adding global controls");
        jContentPane.add(createUpperPanel(), "align right, wrap");
        POSLoggers.initLog.writeDebug("ItemLocator: Adding main panel");
        jContentPane.add(pnl,"width 100%,height 100%, span 3");

        // (6) Attach the content pane to the JPnl
        this.setContentPane(jContentPane);

    }

    private JPanel createUpperPanel()
    {
        JPanel upperPanel=new JPanel();


        MigLayout mig = new MigLayout("align right", "", "");
        upperPanel.setLayout(mig);
        upperPanel.setBackground(Color.WHITE);


        // Create the Done button
        btnDone= GraphicalUtilities.getPOSButton("<html><center>Done</center></html>");
        btnDone.addActionListener(new ButtonListener());

        // Create the Refresh Data button
        btnRefreshData = GraphicalUtilities.getPOSButton("<html><center>Refresh<br>Data</center></html>");
        btnRefreshData.addActionListener(new ButtonListener()); 

        //Addiing buttons to the Panel
        upperPanel.add(btnRefreshData, "width 100:170:200, height 100!");
        upperPanel.add(btnDone, "width 100:170:200, height 100!");

        return upperPanel;

    }

    public class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            try {
                if (e.getSource() == btnRefreshData) {

                    Actual.refreshData();
                } else if (e.getSource() == btnDone) {
                    Actual.backToMainScreen();

                }
            }
            catch (Exception ex)
            {

            }
        }

    }

}

This is the method that the btnDone button calls upon clicking:

public static void backToMainScreen()
{

    frame.setVisible(false);
    frame.dispose();
}

This is the code that displays the JFrame:

public static void displayItemLocatorFrame()
{
    pnl = new Pnl();
    frame = new Frame(pnl);

    frame.setVisible(true);
    pnl.getSearchCriteria().requestFocus();

}

Please note that the “frame” object is static, and all of my methods are static, and they exist in a static class called Actual.

So in short, I just want to make sure that no matter how many times a user clicks on the button, and no matter how fast the clicks were, the frame should act normally.
Any suggestions? (I tried synchronizing my methods with no luck..)

  • 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-05-22T20:30:00+00:00Added an answer on May 22, 2026 at 8:30 pm

    I would generally prefer to use an Action for what you’re trying to do.

    So your code might look like this:

    btnDone = new JButton(new CloseFrameAction());
    
    ...
    
    private class CloseFrameAction extends AbstractAction
    {
        public CloseFrameAction()
        {
             super("Done");
        }
    
        public void actionPerformed(ActionEvent e) 
        {
            frame.dispose();
            setEnabled(false);
        }
    }
    

    Notice the setEnabled(false) line – this should disable the button and prevent the user clicking on it again. Obviously I don’t know what your exact requirements are but this is the general approach I would take.

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

Sidebar

Related Questions

No related questions found

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.