Solved

Modifing code with buttons

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 42
Understanding websocket example in spring 1 42
pairs challenge 5 45
Java Loop 4 20
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now