Solved

modify this class

Posted on 2004-04-25
9
190 Views
Last Modified: 2010-03-31
I want to get your advice and help in setting up this class and another question regarding this class.

This class is a basic class I have been using over again in different ways, to get the basics the the program functioning. This class will be used to set a timing cycle for the workouts. I need to modify a couple areas here.

I will not need to remove (timecyclesArrayList.remove(index); // removal of timing.wav) any longer I will just need to add to the arraylist.

The color change needs to stay.

If the for loop can be changed to start at 2 and end at 12 with the set text as well and any other changes fine tuning that needs to be done. I will then be able to reuse this class over again with only slight changes.


Thanks for the support.

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

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

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

    }







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.util.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class TimeCycles extends JPanel implements ActionListener
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList timecyclesArrayList;
     ArrayList arrayList;
     private Map buttons = new HashMap();

     public TimeCycles()

     {
          timecyclesArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout());
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("8 Point Blocks");
          label.setForeground(Color.red);
          setBackground(Color.white);
                for (int i=0; i<11; i++)
              {
                 JButton b = new JColoredButton( Color.green, Color.black);
                b.setPreferredSize( new Dimension( 40, 20 ) );
                buttons.put("b"+(i+1), b);
               b.setText("" + (i+1) "seconds")
                                b.addActionListener(this);
               gridPanel.add(b);
              }

     }

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

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

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

    }

   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
Comment
Question by:Drop_of_Rain
  • 6
  • 3
9 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
           for (int i=2; i<13; i++)
            {
               JButton b = new JColoredButton( Color.green, Color.black);
               b.setPreferredSize( new Dimension( 40, 20 ) );
               buttons.put("b"+(i+1), b);
               b.setText("" + (i+1) "seconds")
               b.addActionListener(this);
               gridPanel.add(b);
            }
0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
Comment Utility
actually u don't need that setText() call:

            for (int i=2; i<13; i++)
            {
               JButton b = new JColoredButton((i+1) "seconds", Color.green, Color.black);
               b.setPreferredSize( new Dimension( 40, 20 ) );
               buttons.put("b"+(i+1), b);
               b.addActionListener(this);
               gridPanel.add(b);
            }
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
This change will work what is this for  (System.out.println(timecyclesArrayList);)

public void actionPerformed(ActionEvent e)
     {
                               
          JColoredButton b = (JColoredButton)e.getSource();
         
          int index = timecyclesArrayList.indexOf(b) ;
          timecyclesArrayList.add(b);
          b.setPressedColor();
          System.out.println(timecyclesArrayList);
    }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
that just prints the contents of the list to standard output, doubt it is necessary.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
Comment Utility
>  int index = timecyclesArrayList.indexOf(b) ;

that line is also unnecessary
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
Will this (import javax.swing.*;) replace all below



import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
y
0
 

Author Comment

by:Drop_of_Rain
Comment Utility
I wanted to know that is all.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
thats ok, feel free to ask any q's u have :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

762 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

14 Experts available now in Live!

Get 1:1 Help Now