Solved

Modifing code with buttons

Posted on 2004-04-11
3
229 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 81
hibernate example using maven 12 41
jsp login check 12 29
mysql jsp example issue 32 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

786 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