Drop_of_Rain
asked on
Modify code to add actincommands
Hello everyone out I'm back asking for some more help Could this code be modified to be able to add actionCommands in like a for statement, so I don't have to write alot of repetive code. I have an example that was given to me by CEHJ
Example:
for(int i = 0; i < checkboxes.length; i++) {
checkboxes[i].setActionCom mand("kp" + (i + 1) + ".wav");
levels.add(checkboxes[i]); //go through checkboxs and add them to levels
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListe ner;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DefensiveMoves extends JPanel implements ActionListener
{
JCheckBox check;
JLabel label;
JPanel gridPanel;
ArrayList myArrayList;
public DefensiveMoves()
{
myArrayList = new ArrayList();
label = new JLabel();
//Call it like this . . .
gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(0, 3));
// Setting MY layout...
setLayout(new BorderLayout());
add(label, BorderLayout.NORTH);
add(gridPanel, BorderLayout.CENTER);
label.setText(" DefensiveMoves");
label.setForeground(Color. red);
setBackground(Color.white) ;
looprow : for (int row = 0; row < 20; row++)
{
for (int col = 0; col < 3; col++)
{
int n = (20 * col + row + 1);
if (n > 50)
{
break looprow;
}
JButton check = new JColoredButton("" + n, Color.green, Color.black);
check.addActionListener(th is);
//myArrayList.add(check);
gridPanel.add(check);
}
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add (new DefensiveMoves());
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOpera tion(JFram e.EXIT_ON_ CLOSE);
}
public void actionPerformed(ActionEven t e)
{
JColoredButton button = (JColoredButton)e.getSourc e();
if (myArrayList.contains(butt on))
{
myArrayList.remove(button) ;
button.setDefaultColor();
}
else
{
myArrayList.add(button);
button.setPressedColor();
}
System.out.println(myArray List);
}
}
class JColoredButton extends JButton
{
private Color foreground;
private Color deafultForeground;
private Color background;
private Color defaultBackground;
public JColoredButton(String label, Color background, Color foreground)
{
super(label);
this.background = background;
this.foreground = foreground;
defaultBackground = getBackground();
deafultForeground = getForeground();
}
public void setPressedColor()
{
setBackground(background);
setForeground(foreground);
}
public void setDefaultColor()
{
setBackground(defaultBackg round);
setForeground(deafultForeg round);
}
}
Example:
for(int i = 0; i < checkboxes.length; i++) {
checkboxes[i].setActionCom
levels.add(checkboxes[i]);
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent
import java.awt.event.ActionListe
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DefensiveMoves extends JPanel implements ActionListener
{
JCheckBox check;
JLabel label;
JPanel gridPanel;
ArrayList myArrayList;
public DefensiveMoves()
{
myArrayList = new ArrayList();
label = new JLabel();
//Call it like this . . .
gridPanel = new JPanel();
gridPanel.setLayout(new GridLayout(0, 3));
// Setting MY layout...
setLayout(new BorderLayout());
add(label, BorderLayout.NORTH);
add(gridPanel, BorderLayout.CENTER);
label.setText(" DefensiveMoves");
label.setForeground(Color.
setBackground(Color.white)
looprow : for (int row = 0; row < 20; row++)
{
for (int col = 0; col < 3; col++)
{
int n = (20 * col + row + 1);
if (n > 50)
{
break looprow;
}
JButton check = new JColoredButton("" + n, Color.green, Color.black);
check.addActionListener(th
//myArrayList.add(check);
gridPanel.add(check);
}
}
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.getContentPane().add
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOpera
}
public void actionPerformed(ActionEven
{
JColoredButton button = (JColoredButton)e.getSourc
if (myArrayList.contains(butt
{
myArrayList.remove(button)
button.setDefaultColor();
}
else
{
myArrayList.add(button);
button.setPressedColor();
}
System.out.println(myArray
}
}
class JColoredButton extends JButton
{
private Color foreground;
private Color deafultForeground;
private Color background;
private Color defaultBackground;
public JColoredButton(String label, Color background, Color foreground)
{
super(label);
this.background = background;
this.foreground = foreground;
defaultBackground = getBackground();
deafultForeground = getForeground();
}
public void setPressedColor()
{
setBackground(background);
setForeground(foreground);
}
public void setDefaultColor()
{
setBackground(defaultBackg
setForeground(deafultForeg
}
}
Or you could even set it in your JColoredButton constructor.
What string exactly did u want to set it to?
What string exactly did u want to set it to?
ASKER
("kp" + (i + 1) + ".wav");
Like what was in the for:
checkboxes[i].setActionCom mand("kp" + (i + 1) + ".wav");
Like what was in the for:
checkboxes[i].setActionCom
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> checkboxes[i].setActionCom mand("kp" + (i + 1) + ".wav");
You can also use it in your buttons :
for(int i = 0; i < check.length; i++) {
check.setActionCommand("kp " + (i + 1) + ".wav");
}
You can also use it in your buttons :
for(int i = 0; i < check.length; i++) {
check.setActionCommand("kp
}
Or by just declaring it above on your for loop like what objects said.
ASKER
Its working, thanks everyone you make this look so simple. I used objects code
Where always happy as long as we can resolve your problem :)
:)
{
for (int col = 0; col < 3; col++)
{
int n = (20 * col + row + 1);
if (n > 50)
{
break looprow;
}
JButton check = new JColoredButton("" + n, Color.green, Color.black);
check.setActionCommand(xxx
check.addActionListener(th
//myArrayList.add(check);
gridPanel.add(check);
}
}
}