objects: question about this class

Last night you said: The codes there, but he's removed all use of it unless I'm missing something.

 Can you straighten out this.


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 java.lang.Integer;
import java.io.*;

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
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList defensiveMovesArrayList;
     ArrayList theList ;

     public DefensiveMoves()
     {
          MyListener myListener = new MyListener();
          defensiveMovesArrayList = 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("           Defensive Moves");
          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 onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(myListener());
                    gridPanel.add(onOffButton);
               }
          }
     }

     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)
{
          MyListener x = new MyListener();

          JColoredButton button = (JColoredButton)e.getSource();

          int index = defensiveMovesArrayList.indexOf(button) ;

          if (index > -1)
          {
               defensiveMovesArrayList.remove(index); // removal of button
               defensiveMovesArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               defensiveMovesArrayList.add(button);
               defensiveMovesArrayList.add("timing.wav");

               button.setPressedColor();
          }

         
}

}

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);
     }
}

class MyListener implements ActionListener {

    private ArrayList theList = new ArrayList();

    public MyListener() {
    }

    public void addButton(JButton theButtonToAdd) {
       theButtonToAdd.addActionListener(this);  // Listen for the button to be pressed
    }

    public void actionPerformed(ActionEvent e) {

       JButton button = (JButton)e.getSource();
       String command = button.getActionCommand();

       File file = new File("Sound.wav");
       String x = ""+file;
       String y = "File1";

       AudioDetails details = new AudioDetails(x, y);

       if (theList.contains(details))
       {
          theList.remove(details);
       }
       else
       {
          theList.add(details);
       }
    }
}

class AudioDetails
{
   private String AudioFile;
   private String Timing;
   // could add other fields as needed

   public AudioDetails(String file, String timing)
   {
      AudioFile = file;
      Timing = timing;
   }

   public String getAudioFile()
   {
      return AudioFile;
   }

   public String getTiming()
   {
      return Timing;
   }
}
Drop_of_RainAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
go back to how the code was before you asked that question :)
0
Drop_of_RainAuthor Commented:
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 Kempos extends JPanel implements ActionListener
{
     JCheckBox check;
     JLabel label;
     JPanel gridPanel;
     ArrayList kemposArrayList;
     ArrayList arrayList;
     public Kempos()
     {
          kemposArrayList = 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("              Kempos");
          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 onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    onOffButton.setActionCommand("kp" + n + ".wav");
                    gridPanel.add(onOffButton);
               }
          }
     }

     public static void main(String[] args)
     {
          JFrame frame = new JFrame();
          frame.getContentPane().add(new Kempos());
          frame.setSize(400, 400);
          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

     public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = kemposArrayList.indexOf(button) ;

          if (index > -1)
          {
               kemposArrayList.remove(index); // removal of button
               kemposArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               kemposArrayList.add(button);
               kemposArrayList.add("timing.wav");
 
               button.setPressedColor();
          }
         
          System.out.println(kemposArrayList);
}  

}

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);
     }
}          
0
Mick BarryJava DeveloperCommented:
is that the same code?
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Drop_of_RainAuthor Commented:
Everything is the same just the name is different. Here is the other code.

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 dmArrayList;
     ArrayList arrayList;
     public DefensiveMoves()
     {
          dmArrayList = 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);
                     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();
         
          int index = dmArrayList.indexOf(button) ;

          if (index > -1)
          {
               dmArrayList.remove(index); // removal of button
               dmArrayList.remove(index); // removal of timing.wav

               button.setDefaultColor();
          }
          else
          {
               dmArrayList.add(button);
               dmArrayList.add("timing.wav");
 
               button.setPressedColor();
          }
         
          System.out.println(dmArrayList);
}  

}

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);
     }
}          
0
Mick BarryJava DeveloperCommented:
but that doesn't use MyListener either, I'm confused :)
0
JavatmCommented:
> but that doesn't use MyListener either, I'm confused :)

Because you asked for the original codes =-) he told me to
combine the codes that's why he's giving you the original
one w/ no modification.
0
Mick BarryJava DeveloperCommented:
> combine the codes

that doesn't mean to simply put them all in one file. Especially if you don't use the classes you are adding. Unused private class serve no purpose.

0
JavatmCommented:
> that doesn't mean to simply put them all in one file. Especially if you don't use the classes you are adding. Unused private class serve no purpose

Its not my fault if I combined the codes I just want to answer the question and the good thing about it is I made a way
to have the codes work even though it gave me errors when I first tested it and the other thing is that, how did
you know that the other classes serve's no purpose, have you tested it ?
0
Mick BarryJava DeveloperCommented:
> how did you know that the other classes serve's no purpose

Because you didn't use them. I just don't understand why you didn't just delete them if you didn't think they were necessary.
0
Mick BarryJava DeveloperCommented:
Wasn't the main question actually to combine the lists into one
0
Mick BarryJava DeveloperCommented:
By Chris's own admission his Java knowledge is limited so if he asks for something that doesn't make sense, like making classes private, then it is often necessary to clarify his reasoning and possibly offer alternatives.
0
JavatmCommented:
> Because you didn't use them. I just don't understand why you didn't just delete them if you didn't think they were necessary

I didnt thought put in my mind that there were no use of the other classes but instead I tried to add them in the program since
our friend ask for it but that doesnt mean that there are no use of the codes since I gave some modification for it to serve its
purpose to the program.


> By Chris's own admission his Java knowledge is limited so if he asks for something that doesn't make sense, like making
> classes private, then it is often necessary to clarify his reasoning and possibly offer alternatives.

That's why I gave that clarrification and a reminder to the previous question.



0
Drop_of_RainAuthor Commented:
OK I'm back, I am learning from you guys and I really appreciate the care and time you give me.. You are my teachers. It feels to me that we have esablished a relationship now, and that I'm not just a person on the web asking a question any lomger. I need your help, guidance and direction, that is why i am coming to you. I think you guys look for my questions, if that is so, I feel honored.

Javatm I hope you don't feel offended by the question I asked objects if so I am sorry for hurting your feeling. I respect objects knowledge in Java and look forward towards his help. If he says something I pay attention. I have seen from all the questions he has answered for me the difference between his code and others. His code is simplier and uses less code to do the same thing.

So where do we go from here.
0
JavatmCommented:
> I respect objects knowledge in Java and look forward towards his help. If he says something I pay attention.

I sure do respect objects alot since he gave me a lot of answers to my questions before and the other
thing is his my Grandmaster and Mentor so I sure do try 2 follow his steps.

> Javatm I hope you don't feel offended by the question I asked objects if so I am sorry for hurting your feeling.

I'm not actually offended in fact I feel honored to be questioned by objects and also one other thing is that
everyone has the right to answer or to question an answer so its okey.

> So where do we go from here.

As I said before, know your problems 1st then try to code it by hand after that if you find problems search on the
web for any solutions if you cant find one and you feel that you cant answer it then thats the time you could ask
us back and in fact there are alot of solutions here in our mother site experts-exchange.

By doing that will give a much more better understanding on the real situation of the problem and dont always
relay on the codes which are already made sometimes they are made for an exact purpose but if you find it
usefull then you could extract it peace by peace to get the point.

Hope that helps . . .
Javatm
 
0
Mick BarryJava DeveloperCommented:
> So where do we go from here.

I've lost track what changes u originally required :)
0
Drop_of_RainAuthor Commented:
What I know is that MyListener needs to be used to listen to the buttons in each class. I need to add it to all of the classes I'm using in the TabbedPane.

What I needed is to get is that ever button is being listened to and that a single list can be created from the buttons that are being pressed.

The wrapper class makes a single list from the buttons that have been pressed. It takes args it will need to be modified some as well.
0
Mick BarryJava DeveloperCommented:
what tabbedpane?
0
Drop_of_RainAuthor Commented:
I am using a TabbedPane for the program GUI thats all. The reason I said that was to give an overview of the listeners needed to create the single list from.  Thats all.

If Mylistener is used in each class that makes up ever button panel for the custom section of the program, will a single arraylist be able to have all the actioncommands go into it using the wrapper class as well.
0
Mick BarryJava DeveloperCommented:
You need to pass a single myListener instance to any class that needs it:

     public DefensiveMoves(MyListener myListener)
     {
          defensiveMovesArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, 3));
          ...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Drop_of_RainAuthor Commented:
What a simple way, to do that.

I still will call the class from another class like  DefensiveMoves(); because it is passed in the class itself.
0
Mick BarryJava DeveloperCommented:
no you'll use:

DefensiveMoves moves = DefensiveMoves(mylistener);
0
Drop_of_RainAuthor Commented:
Thanks for all the support,
Christopher
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.