Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

jButton as variable loaded into a ArrayList??

807598Jan 20 2006 — edited Jan 20 2006
Hi!

I'm trying to load an ArrayList with jbuttons .... when explicitly added like this , it works fine......
package AddRFSwitchPac;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JButton;

/**
 *
 * @author Richard
 */
public class ButtonArrayList {
    
    /**
     * Creates a new instance of ButtonArrayList
     */
    public ButtonArrayList() {
        
        ArrayList<JButton> buttons = new ArrayList<JButton>();

      
        buttons.add(AddRFSwitchMain.jButton1);
        buttons.add(AddRFSwitchMain.jButton2);
        buttons.add(AddRFSwitchMain.jButton3);
        buttons.add(AddRFSwitchMain.jButton4);
        buttons.add(AddRFSwitchMain.jButton5);
        
 
 
 
        JButton button = (JButton)buttons.get(3);
        if (button != null)
        button.setBackground(new Color(0,0,0));
 

        
        
    }
    
}
.... but when I try to load the jButtons from a loop .... It will not work ...
package AddRFSwitchPac;

import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JButton;


/**
 *
 * @author Richard
 */
public class ButtonArrayListLoad {
    
    /**
     * Creates a new instance of ButtonArrayListLoad
     */
    public ButtonArrayListLoad() {
        
        
            int rc = AddRFSwitchMain.jTable1.getRowCount();//jTable is 5 rows ... this equates to 5 jButtons
            
            ArrayList<JButton> buttons = new ArrayList<JButton>();
            
       for(int i = 0; i <= rc; i++) {
            
            String key = String.valueOf(i);
            
            JButton button = new JButton();
           // buttons.add("key");// this doesnt work as string "key" or an int (i)
            
        }
            // here I pick what button I want out of the Arraylist
            JButton button = buttons.get(2);
         
       if (button != null)
            button.setBackground(new Color(0,0,0));
    }
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 17 2006
Added on Jan 20 2006
24 comments
1,399 views