How to disable a Jbutton on certain clause?

Dark Star

ex-Mod
I am making a small project, and in it I have a JFrame with 5 buttons. 3 buttons are of primary concern and are enabled by default.

What I want is until and unless any of those 3 buttons are pressed rest of the 2 should remain disabled.

I tried with ActionListner and MouseListener but to no avail.

Check the multiple code that i tried.

Code:
 public void mousePressed (MouseEvent me){    

         if (me.getButton() == MouseEvent.NOBUTTON ){

             

            proceedBtn.setEnabled(false);

            proceedBtn.setToolTipText("Please Enter A Button Before Proceeding");

        }

        else {                   

            proceedBtn.setEnabled(true);        

        }

    

    }

Code:
 public void mousePressed (MouseEvent me){    

         if (me.getClickCount == 0 ){

             

            proceedBtn.setEnabled(false);

            proceedBtn.setToolTipText("Please click a button Before Proceeding");

        }

        else {                   

            proceedBtn.setEnabled(true);        

        }

    

    }

What am I doing wrong here ? I even tried mouseClicked method for the same code but nothing happened.

Please help.
 
I achieved this, actually the problem was fairly easy. What I did is disabled the checkout button in the frame constructor, and added a mouseListener to all the 3 shop buttons. So if you click you can checkout.

But then another condition applies. What if a person enter the shop and didn't purchased anything, thus I called the shoplist constructor and used the clause there as well. So if a person buys a thing it will proceed else not. Here is the working code.

Code:
 public ShoppingList (int x){

        

        initComponents();

        proceedBtn.setEnabled(true);

        shpbtn1.addMouseListener(this);

        shpbtn2.addMouseListener(this);

        shpbtn3.addMouseListener(this);   

   }

    

    public  void mouseClicked(MouseEvent me) {   

               

        

       if (me.getSource() == shpbtn1){

           

             Shop shp = new Shop("bigbazar");

            shp.setVisible(true);

            shp.setLocationRelativeTo(null);

            this.dispose();            

        }

        

        else if (me.getSource() == shpbtn2){

            

            Shop shp = new Shop("pantaloon");

            shp.setVisible(true);

            shp.setLocationRelativeTo(null);

            this.dispose();

        }

        

        else if (me.getSource()==shpbtn3) {

            

            Shop shp = new Shop("reebok");

            shp.setVisible(true);

            shp.setLocationRelativeTo(null);

            this.dispose();            

        }

        

        

        

    }

I am not actually satisfied with MouseListener, will probably revert to ActionListener for better results.

Here is the code for the next frame

Code:
 ShoppingList spl;

        

        if (Integer.parseInt(total.getText()) ==0) {

        spl = new ShoppingList();

        spl.setLocationRelativeTo(null);

        spl.setVisible(true);

        this.dispose(); }

        

        else {

            

        spl = new ShoppingList(1);

        spl.setLocationRelativeTo(null);

        spl.setVisible(true);

        this.dispose();          

            

        }
 
Back
Top