Results 1 to 6 of 6
  1. #1
    ea1
    ea1 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Array of buttons, which one is pressed?

    I'm trying to create an array of JButtons, and I want to know whick one is pressed. So, if my array is named vertical[k], how can I get the k of the button that is pressed?
    And also, why can't I do something with the button when I try to with for example vertical[1].setBackground(Color.BLACK);

    This is the code
    Java Code:
    import java.awt.Color;
    import java.awt.Container;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class test extends JFrame implements ActionListener {
    	private Container c;
    	private JButton b, gumb;
    	
    	public ArrayList<JButton> vertikala;
    	public test ()
    	{vertikala = new ArrayList<JButton>();
    	JButton[] vertikala = new JButton[60];
    		
    		setSize (1000, 1000);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible (true);
    		
            Container c =new Container ();
            c = getContentPane ();
    		c.setLayout (null);
    		
    		int x,y,y1,k = 0; x = 0; y=0; ; k=0; y1=1;
    		
    		
    	for (int i=1; i <=6; i++){
    		x=70; y+=70;
    	k++;
    		vertikala[k] = new JButton ();
    		vertikala[k].setSize(60,60);
    		vertikala[k].setLocation(x,y);
    		vertikala[k].addActionListener(this);
    		
    				c.add(vertikala[k]);
    }}
    	public void promjeni(){
    	   vertikala[1].setBackground(Color.BLACK);	
    		}
    		public void actionPerformed (ActionEvent e)
    		{ 
    			
    			promjeni();
    		}	
    	public static void main (String [] s)
    {
    	test window = new test ();
    }}

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Array of buttons, which one is pressed?

    how can I get the k of the button that is pressed?
    In your actionPerformed method you could use getSource to get the object/button which was pressed. You have to cast it to e.g. JButton if you want to call methods of the JButton class
    And also, why can't I do something with the button when I try to with for example vertical[1].setBackground(Color.BLACK);
    Because your JButton array is only visible in your constrcutor! The other vertikala is a list! Why do you use a button array and a list of buttons?

    Btw: Read the naming conventions : Code Conventions for the Java Programming Language: Contents

  3. #3
    ea1
    ea1 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Array of buttons, which one is pressed?

    I used it by mistake.
    Sorry, but I don't understand. How can I achieve that I make a list of buttons for example
    button[1], button[2] etc. so that I know which one was pressed and so that I change the color of the one that was pressed and remeber it's number[].

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Array of buttons, which one is pressed?

    Example:
    Java Code:
    	public void promjeni(AbstractButton button) {
    		button.setBackground(Color.BLACK);
    	}
    
    	public void actionPerformed(ActionEvent e) {
    		promjeni((AbstractButton)e.getSource());
    	}

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: Array of buttons, which one is pressed?

    When you store the button in the array, if you were to save its index value in the button object, then in the listener you could get a reference to the button with getSource() and from the button you could get the index value that was save there.

  6. #6
    ea1
    ea1 is offline Member
    Join Date
    Mar 2012
    Posts
    3
    Rep Power
    0

    Default Re: Array of buttons, which one is pressed?

    Thanks a lot guys!

Similar Threads

  1. Kep Pressed
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 09:07 PM
  2. Non functional radio buttons and messed up array
    By flameofSuzaku in forum New To Java
    Replies: 2
    Last Post: 01-10-2011, 06:38 PM
  3. Creating Jradio Buttons from a given array
    By Zeo7 in forum AWT / Swing
    Replies: 5
    Last Post: 12-17-2010, 05:31 AM
  4. Multidemensional array of Buttons
    By macwadu in forum AWT / Swing
    Replies: 32
    Last Post: 08-29-2010, 01:25 AM
  5. Replies: 2
    Last Post: 04-30-2010, 04:06 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
  •