Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modifing code with buttons

Posted on 2004-04-11
3
Medium Priority
?
235 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.
Suggested Courses

971 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