Solved

objects: question about this class

Posted on 2004-04-26
22
227 Views
Last Modified: 2011-09-20
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;
   }
}
0
Comment
Question by:Drop_of_Rain
  • 11
  • 7
  • 4
22 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
go back to how the code was before you asked that question :)
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
is that the same code?
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
but that doesn't use MyListener either, I'm confused :)
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
Wasn't the main question actually to combine the lists into one
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Expert Comment

by:Javatm
Comment Utility
> 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
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
> 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
 
LVL 92

Expert Comment

by:objects
Comment Utility
> So where do we go from here.

I've lost track what changes u originally required :)
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
what tabbedpane?
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
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
 

Author Comment

by:Drop_of_Rain
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
no you'll use:

DefensiveMoves moves = DefensiveMoves(mylistener);
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Thanks for all the support,
Christopher
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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

12 Experts available now in Live!

Get 1:1 Help Now