Solved

modify this class

Posted on 2004-04-25
9
194 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
Opening PDF on button click and fill new document 2 47
eclipse buid path vs tomcat lib path 10 34
hashmap order 17 36
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

808 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