Solved

Modifing code with buttons

Posted on 2004-04-11
3
233 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

623 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