There are 2 sets of 4 buttons being created.

The ImageIcon of the 1st set of buttons are to be dragged onto the 2nd set of buttons.

The boolean enab variable is for determining if the 2nd set of 4 buttons are enabled or not. I initially set the boolean to false, only when the ImageIcon from the 1st set of buttons are dragged over, will the enab value change to true.

However, the 2nd set of buttons remain disabled while and after I dragged the images over to it.

I inserted System.out.println statements to see where did it go wrong, the result is that the enab variable is updated but the stateChanged method doesn't run at all.

How do I get the stateChanged method to detect that the buttons it listens to are already enabled for it run ?

    private boolean enab = false;

    // creates 4 buttons   
        for(int a=0; a<4; a++){

            button[a] = new JButton(new ImageIcon(img)); // any img  
            TransferHandler transfer = new TransferHandler("icon");
            button[a].setTransferHandler(transfer);

            button[a].addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                JButton button = (JButton)e.getSource();
                TransferHandler handle = button.getTransferHandler();
                handle.exportAsDrag(button, e, TransferHandler.COPY);
                name1 = e.getComponent().getName();
                System.out.println(name1);
                enab = true;
                System.out.println("enabled0:" + enab); // prints true
                }       

            }
        }
    // adds the 4 buttons to panel
    leftg.add(button[a]);

    // creates another 4 buttons for stuff to be dragged to
    for(int b=0; b<4; b++){

         rg[b] = new JButton();
         id2 += Integer.toString(++cc2);
         // unique name for each button as an ID
         rg[b].setName(id2); 
         TransferHandler transfer1 = new TransferHandler("icon");
         rg[b].setTransferHandler(transfer1);
         rg[b].setEnabled(enab);
         System.out.println("enabled00:" + enab); // prints false 


         ChangeListener clistener = new ChangeListener(){                 
             public void stateChanged(ChangeEvent ce) {

                 JButton source = (JButton)ce.getSource();
                 ButtonModel mod = source.getModel();
                 System.out.println("enabled1:" + enab); // no output

                   if (mod.isEnabled()){
                    System.out.println("enabled2:" + enab); // no output
                        if(name1 == source.getName()){

                        }
                        else{
                            source.setIcon(null);
                        }
                   }  
                   else{        
                   }
         };
         };
    rg[b].addChangeListener(clistener);
    // adds the 4 buttons to panel
    rightg.add(rg[b]);
    }        

Recommended Answers

All 5 Replies

I haven't been able to find any clear documentation on exactly what causes a javax.swing.event.ChangeEvent, except that it is supposed to be all changes. Without documentation, I was forced to toy around with it to find out just exactly when I could expect to get ChangeEvents, and it turns out that many things will cause ChangeEvents, but changing a JButton's icon by dropping another icon over it will not cause a ChangeEvent. Fortunately, it will cause a java.beans.PropertyChangeEvent so you can use that instead.

It looks like you may be mistakenly thinking that changing the value of enab will cause your buttons to be enabled or disabled. That won't happen. You need to call setEnabled every time you want to enable or disable a button.

thanks a lto bguild. I didn't know that I've to call setEnabled once again even though I set enab to true.

Will consider using PropertyChangeEvent if things still doesn't work out with ChangeEvent :)

I got it to work already, but the weird thing is that the changeListener only works after I hover over the listened to buttons after dragging the images over. =/.

Any help is appreciated.

Hi, may I know what's the downvote for ?

Do I have to address any issue ? =/.

Oh, is it because I didn't include the updated code ?

    private boolean enab = false;

    // creates 4 buttons   
        for(int a=0; a<4; a++){

            button[a] = new JButton(new ImageIcon(img)); // any img  
            TransferHandler transfer = new TransferHandler("icon");
            button[a].setTransferHandler(transfer);

            button[a].addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent e){
                JButton button = (JButton)e.getSource();
                TransferHandler handle = button.getTransferHandler();
                handle.exportAsDrag(button, e, TransferHandler.COPY);
                name1 = e.getComponent().getName();
                System.out.println(name1);
                enab = true;
                System.out.println("enabled0:" + enab); // prints true
    //<-------  changes here ----------------->
                enabtest();
    //<-------  changes here ----------------->          
                }       

            }
        }
    // adds the 4 buttons to panel
    leftg.add(button[a]);

    // creates another 4 buttons for stuff to be dragged to
    for(int b=0; b<4; b++){

         rg[b] = new JButton();
         id2 += Integer.toString(++cc2);
         // unique name for each button as an ID
         rg[b].setName(id2); 
         TransferHandler transfer1 = new TransferHandler("icon");
         rg[b].setTransferHandler(transfer1);
         rg[b].setEnabled(enab);
         System.out.println("enabled00:" + enab); // prints false 


         ChangeListener clistener = new ChangeListener(){                 
             public void stateChanged(ChangeEvent ce) {

                 JButton source = (JButton)ce.getSource();
                 ButtonModel mod = source.getModel();
                 System.out.println("enabled1:" + enab); // no output

                   if (mod.isEnabled()){
                    System.out.println("enabled2:" + enab); // no output
                        if(name1 == source.getName()){

                        }
                        else{
                            source.setIcon(null);
                        }
                   }  
                   else{        
                   }
         };
         };
    rg[b].addChangeListener(clistener);
    // adds the 4 buttons to panel
    rightg.add(rg[b]);
    }
//<-------  changes here ----------------->
        public void enabtest(){
        if(enab == true){
        System.out.println("aaa");
        for(int b=0; b<4; b++){
        rg[b].setEnabled(enab);
        }
    }

//<-------  changes here ----------------->  

Hi, may I know what's the downvote for ?
Do I have to address any issue ? =/.
Oh, is it because I didn't include the updated code ?

Your request for help was pretty pointless without posting the code that displays the problem, but I don't think an unexplained downvote was justified. The way it works is that these votes are subjective, so I wouldn't worry too much about votes with no explanation unless you get a lot of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.