Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default Button click GUI question

    I am new to the GUI part of Java. I have created a board with 9 buttons in a 3x3 fashion. My question is... How do I activate a button click with the following code

    Java Code:
        	public BoardLayout(){
    	    	setLayout(new GridLayout(3,3));
    
    			for (int i=1;i<10;i++){
    				add(new JButton("" + i));
    			}
        	}
    I know how to do it, if I create the buttons with unique names, such as
    Java Code:
    JButton jbtOne = new JButton("1");
    But, my instructor insists on using a loop to create the 9 buttons. This is the only way I can figure out how to create the layout, but now, I don't know how to create an event when a button is clicked, as each button doesn't have a unique name like jbtOne. Are there any suggestions that may be able to point me in the right direction?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You should create a Button variable inside the loop, add an ActionListener to it, and add it to the container:

    Java Code:
          for (int i=1;i<10;i++){
              //add(new JButton("" + i));
             JButton button = new JButton("" + i);
             button.addActionListener(new ActionListener() {
                // *** your action listener my be different
                public void actionPerformed(ActionEvent e) {
                   myButtonsAction(e); // a method of the class
                }
             });
             add(button);
          }

  3. #3
    Join Date
    Feb 2009
    Posts
    6
    Rep Power
    0

    Default

    I appreciate the help. I was able to complete what I was trying to accomplish with your help.

    Not sure if you want me to post what I did, but this is how I set it up.

    Java Code:
    			for (int i=1;i<10;i++){
    
    	        	final JButton button = new JButton("" + i, filler);
             		add(button);
    
             		button.addActionListener(new ActionListener() {
             			public void actionPerformed(ActionEvent e){
             				XO = (int)(Math.random()*3);
             				if ( XO == 1)
    							button.setIcon(showcross);
    						else if ( XO == 2)
    							button.setIcon(shownot);
    						else
    							button.setIcon(filler);
             			}
          			});
    
          			button.addActionListener(printButton);
    
        		}
        	}
        }
    
    
    
       	static class PrintButtonClick implements ActionListener{
       		public void actionPerformed(ActionEvent e){
       			System.out.println(e.getActionCommand() + " was clicked.");
       		}
       	}

Similar Threads

  1. How to run a jar file from a Button click in swing.
    By killerf2006 in forum New To Java
    Replies: 1
    Last Post: 07-23-2010, 01:55 PM
  2. SaveAs button click
    By kasiram.p@gmail.com in forum AWT / Swing
    Replies: 2
    Last Post: 07-06-2010, 09:35 AM
  3. How can I display on Button click?
    By ntagrafix in forum New To Java
    Replies: 3
    Last Post: 11-04-2009, 01:05 AM
  4. How we can open a file with a click in button
    By kostinio in forum AWT / Swing
    Replies: 2
    Last Post: 11-01-2009, 05:46 PM
  5. deselecting a button after the click.
    By ramsrocker in forum New To Java
    Replies: 10
    Last Post: 02-15-2009, 07:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •