Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to change action on 2nd JButton click

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to change action on 2nd JButton click

    I'm stacked!! I am relatively new to java and I have been days searching the web to solve my problem without success, its now time to ask for your help.

    I cannot...(actually do not know) how to change the actionlistener (actionPerformed) after the second click in an array of JButtons. Please find the code below so that you can guide me.

    This is how the program performs right now.

    On the 1st click on the grid, the border of the button clicked will change to 'blue' and another button will change its boarder to 'red'. on the next click the same action is performed.

    What I need is to change action when the 'red' bordered button is clicked, lets say change the color of the buttons from the 'red' to the 'blue' buttons.(x 3 buttons).

    The logic about how to perform the final result I think I can do on my own but my problem is how to change the action when the red bordered button is clicked.

    Your help is much appreciated!

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
     
     
    public class ChangeGrid
    {
     
        private JButton[][] bu1 = new JButton[10][10];
        public ChangeGrid(JFrame frame)
        {
            super();
     
            JPanel pCenter = new JPanel();
            JPanel pTop = new JPanel();
     
            frame.add(pCenter, BorderLayout.CENTER);
            frame.add(pTop, BorderLayout.NORTH);
     
            JPanel Grid10x10 = new JPanel(new GridLayout(10,10));
     
            pCenter.add(Grid10x10);
     
            for(int c = 0; c< 10; c++)
            {
                for (int r = 0; r< 10; r++)
                {
                    bu1[c][r] = new JButton("X");
                    Grid10x10.add(bu1[c][r]);
     
                    final int i = c;
                    final int j = r;
     
                    bu1[i][j].addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent e)
                        {
                            int f = (3-1);
     
                            bu1[i][j].setBorder(BorderFactory.createLineBorder(Color.BLUE, 2));
     
                            if ((j+f)<=9)
                            {
                                bu1[i][j+f].setBorder(BorderFactory.createLineBorder(Color.RED, 2));
                            }
                            else
                            {
                                JOptionPane.showMessageDialog(null,"move your 1st click more to the center");
                }
                        }
                    });
                }
            }
        }
    }
     
    import javax.swing.JFrame;
    public class GUIFrame extends JFrame
    {
        public GUIFrame()
        {
            super("Grid 10 x 10");
     
            this.setSize(1350, 700);
            this.setVisible(true);
            this.setResizable(true);
            this.setDefaultCloseOperation(GUIFrame.EXIT_ON_CLOSE);
        }
    }
     
    import javax.swing.JFrame;
    public class Main
    {
        public static void main(String args[])
            {
     
                JFrame frame = new GUIFrame();
                ChangeGrid pC = new ChangeGrid(frame);
                frame.pack();
            }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to change action on 2nd JButton click

    Create a field in your buttons that count the number of times they've been clicked. You may want to create your own custom button, an extension of JButton, to add this field if the number of clicks have to be monitored for each button. Then a simple if() statement in your actionPerformed() method will do (pseudo-code):
    if ( button.getNumberOfClicks() < 3 )
    {
        // do this
    }
    else
    {
        // do this
    }

Similar Threads

  1. Mouse Click Action Listener for JTextField?
    By Catlitter68 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 12th, 2013, 10:11 AM
  2. Mimicking a JButton Click
    By tim8w in forum AWT / Java Swing
    Replies: 1
    Last Post: January 26th, 2013, 02:44 AM
  3. [SOLVED] trying to change image by mouse click
    By leonne in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 5th, 2013, 08:16 PM
  4. Checkbox doesnt change when click on JTable
    By GodspeedPR in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 02:12 PM
  5. Waiting for a Second JButton Click
    By Firebert in forum Java Theory & Questions
    Replies: 0
    Last Post: March 16th, 2011, 08:15 PM

Tags for this Thread