Solved

modify this class

Posted on 2004-04-25
9
191 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
 
LVL 92

Expert Comment

by:objects
ID: 10914572
that just prints the contents of the list to standard output, doubt it is necessary.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 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