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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:12:42+00:00 2026-05-13T07:12:42+00:00

I got a little problem with a JOptionPane which I use to warn user

  • 0

I got a little problem with a JOptionPane which I use to warn user if wrong input is found. It works correct first time. But when I close down the JFrame which calls for that JOptionPane, and open it again it will this time call for it twice. And it will stack for every close down I do.

I have tried to look for the problem without any luck. I can provide the code, but it is quite large though.

Third EDIT: I have found and solved the problem now.

Ok, I provided the code I use. I have cut it down so it only show the necessary one. I dont think it will compile, but this is how I use the addActionListener();

public class BorderLayoutDemo extends JFrame implements ActionListener {

    private JButton button1 = new JButton("L?gg till kund");
    private JButton button2 = new JButton("Ta bort kund");
    private JButton button3 = new JButton("Visa kund");
    private JButton button4 = new JButton("Lista alla kunder");
    private JButton button5 = new JButton("Avsluta");
    private JButton button6 = new JButton("Change");

    private JTextArea TextWindow = new JTextArea("Hej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\nHej\n");
    private JScrollPane scrollPane = new JScrollPane(TextWindow);   //l?gger in TextWindow s? att det f?r en scroll-bar

    private JPanel aPanel = new JPanel();
    private JFrame aFrame = new JFrame();

    private JTextField aTextfield1 = new JTextField();
    private JTextField aTextfield2 = new JTextField();

    private JButton aButton1 = new JButton("L?gg till kund");
    private JButton aButton2 = new JButton("St?ng");

    public BorderLayoutDemo() {

            setTitle("Bankregister");

            setLayout(new BorderLayout());

            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10));
            panel.setLayout(new GridLayout(6,1,55,5)); //row, cols, hgap, vgap

            button1.addActionListener(this);
            button2.addActionListener(this);
            button3.addActionListener(this);
            button4.addActionListener(this);
            button5.addActionListener(this);
            button6.addActionListener(this);

            panel.add(button1);
            panel.add(button2);
            panel.add(button3);
            panel.add(button4);
            panel.add(button5);
            panel.add(button6);

            JPanel panel2 = new JPanel();
            panel2.add(panel);

            add(panel2,BorderLayout.WEST);
            add(scrollPane,BorderLayout.CENTER);

            setJMenuBar(menu());

            setSize(600,300);

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);

    }

    public void addCustomer(boolean status) {


            if(status) {    

                aFrame.setTitle("L?gg till kund");
                aFrame.setSize(200,300);
                aFrame.setLayout(new GridLayout(3,1));

                aPanel.setLayout(new GridLayout(2,1)); //rad, kolumn

                aPanel.add(aTextfield1);
                aPanel.add(aTextfield2);

                aButton1.addActionListener(this);
                aButton2.addActionListener(this);
                System.out.println("Foo!!!!!!!!!!!!!");

                aFrame.add(aPanel);
                aFrame.add(aButton1);
                aFrame.add(aButton2);

                aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                aFrame.setLocationRelativeTo(null);
                aFrame.setVisible(true);
            }
            else {

                aFrame.setVisible(false);


            }
    }

    public static void main(String[] args) {

        new BorderLayoutDemo();
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == button1) {
            setEnabled(false);
            addCustomer(true);
        }

        //IFs f?r addCustomer();
        else if(e.getSource() == aButton1) {

            if((aTextfield1.getText().isEmpty() || aTextfield2.getText().isEmpty())) {
                JOptionPane.showMessageDialog(null, "You miss to fill out the fields");
            }
            else {
                JOptionPane.showMessageDialog(null, "Added");
                Kund kund = new Kund(aTextfield1.getText(),aTextfield2.getText());
                setEnabled(true);
                register.add(kund);
            }
        }
        else if(e.getSource() == aButton2) {
            setEnabled(true);
            addCustomer(false);
        }
  • 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-13T07:12:43+00:00Added an answer on May 13, 2026 at 7:12 am

    Sounds like you are adding the “validation listener” every time you open the JFrame. So check your “addListenerXXX” code to make sure it is only added/created once.

    Which also leads to the question why are you using a JFrame for this? Typically an application has a single JFrame. Then, if you need a window to enter data you create a JDialog.

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

Sidebar

Related Questions

Got a strange problem created a little stored proc which need to execute a
Got a little problem in my code, the query works fine if I manually
I have got a little problem using the CodeIgniter route function. I use the
I've got a little problem, I'm stuck with. I got two tables. The first
I got a little problem with the OAuth for Google, I use the Jim
I've got a little problem with uitextfield. I use two of them in a
I've got a little problem. Well, let me first state what I'm trying to
I've got a little problem with correct query and hope you can help me.
i`ve got a little problem with LINQ. I read out some information via XML-RPC.
I got a little problem. Sometimes, when I try to call the following code,

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.