Solved

modify this class

Posted on 2004-04-25
9
192 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
ID: 10914526
           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
ID: 10914531
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
ID: 10914562
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 92

Expert Comment

by:objects
ID: 10914572
that just prints the contents of the list to standard output, doubt it is necessary.
0
 
LVL 92

Assisted Solution

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

that line is also unnecessary
0
 

Author Comment

by:Drop_of_Rain
ID: 10914580
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
ID: 10914643
y
0
 

Author Comment

by:Drop_of_Rain
ID: 10914688
I wanted to know that is all.
0
 
LVL 92

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
why cannot we forward request once the response is committed 2 42
hibernate example using maven 12 42
String array comparison 4 34
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 last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

810 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