Solved

Modify code to add actincommands

Posted on 2004-04-15
9
221 Views
Last Modified: 2010-03-31
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].setActionCommand("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.ActionListener;
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(this);
                    //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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
     {
          JColoredButton button = (JColoredButton)e.getSource();
         
          if (myArrayList.contains(button))
          {
               myArrayList.remove(button);
               button.setDefaultColor();
          }
          else
          {
               myArrayList.add(button);
               button.setPressedColor();
          }
         
          System.out.println(myArrayList);
     }
}

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(defaultBackground);
          setForeground(deafultForeground);
     }
}
0
Comment
Question by:Drop_of_Rain
  • 4
  • 3
  • 2
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10838244
         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.setActionCommand(xxx); // <- set your action command here to what u want it to be
                    check.addActionListener(this);
                    //myArrayList.add(check);
                    gridPanel.add(check);
               }
          }
     }
0
 
LVL 92

Expert Comment

by:objects
ID: 10838248
Or you could even set it in your JColoredButton constructor.
What string exactly did u want to set it to?
0
 

Author Comment

by:Drop_of_Rain
ID: 10838304
("kp" + (i + 1) + ".wav");

Like what was in the for:

checkboxes[i].setActionCommand("kp" + (i + 1) + ".wav");
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 10838426
        int i = 0;
         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.setActionCommand("kp" + (i + 1) + ".wav"); // <- set your action command here to what u want it to be
                    check.addActionListener(this);
                    //myArrayList.add(check);
                    gridPanel.add(check);
                    i++;
               }
          }
     }
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10838430
> checkboxes[i].setActionCommand("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");
}


0
 
LVL 15

Expert Comment

by:Javatm
ID: 10838449
Or by just declaring it above on your for loop like what objects said.
0
 

Author Comment

by:Drop_of_Rain
ID: 10838494
Its working, thanks everyone you make this look so simple. I used objects code
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10838512
Where always happy as long as we can resolve your problem :)
0
 
LVL 92

Expert Comment

by:objects
ID: 10838535
:)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
session migration servlets 2 38
tomcat not starting 6 45
Running JavaFX on JDeveloper 12C 1 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question