Solved

Modifing code with buttons

Posted on 2004-04-11
3
230 Views
Last Modified: 2010-03-31
How would code#2 be modified so that the setActionCommand to arraylist and removed
again work in that code. I will have several panels filled this way with checkboxs and I need all of the setActionCommands going to the same arraylist. Because there is a order being created by this.

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ActionCommandTest extends JFrame implements ActionListener
{
     private List actionCommandList = new ArrayList();

     public ActionCommandTest()
     {
          super();

          initialize();
     }

     private void initialize()
     {
          JPanel panel = (JPanel) getContentPane();

          panel.setLayout(new GridLayout(4, 4));

          for (int i = 0; i < 12; i++)
          {
               JButton button = new JButton("Hello - " + i);
               button.setActionCommand("./test/muruga/wavefile-" + i + ".wav");
               button.addActionListener(this);

               panel.add(button);
          }

          setSize(400, 400);
          setVisible(true);

          setDefaultCloseOperation(EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
     {
          String actionCommand = e.getActionCommand();

          if (actionCommandList.contains(actionCommand))
          {
               ((AbstractButton)e.getSource()).setForeground(Color.black);
               actionCommandList.remove(actionCommand);
          }
          else
          {
               ((AbstractButton)e.getSource()).setForeground(Color.blue);
               actionCommandList.add(actionCommand);
          }

          System.out.println("Action command list is: " + actionCommandList);
     }

     public static void main(String[] args)
     {
          new ActionCommandTest();
     }
}




#2
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;
/**
 * @author Tommy
 *
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Generation - Code and Comments
 */
public class DefensiveMoves extends JPanel {
     JCheckBox check;
     JLabel label;
     JPanel gridPanel;
     ArrayList myArrayList;
     /**
      * Constructor for the MyClass4 object
      */
     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);
                    myArrayList.add(check);
                    gridPanel.add(check);
               }
          }
     }
   
}
class JColoredButton extends JButton implements ActionListener {
     private Color foreground;
     private Color deafultForeground;
     private Color background;
     private Color defaultBackground;
     private boolean state = true;
     
     public JColoredButton(String label, Color background, Color foreground) {
          super(label);
          this.background = background;
          this.foreground = foreground;
          defaultBackground = getBackground();
          deafultForeground = getForeground();
          addActionListener(this);
     }
     /*
      * (non-Javadoc)
      *
      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
      */
     public void actionPerformed(ActionEvent e) {
          if (state) {
               setBackground(background);
               setForeground(foreground);
          } else {
               setBackground(defaultBackground);
               setForeground(deafultForeground);
          }
          state = !state;
     }
}
0
Comment
Question by:Drop_of_Rain
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 500 total points
ID: 10803830
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);
      }
}


Hope this helps.


Regards,
Muruga
0
 

Author Comment

by:Drop_of_Rain
ID: 10803849
That was great, such a small amount of code and so simple great idea.

Thanks
Christopher


I am going to post a big question next help me out , it will be right up.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10803856
Great!  Go ahead!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
tomcat not starting 6 68
Java Restore security prompts not working 10 56
Chrome and Firefox Java 5 50
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

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