Solved

need a part functioning in a class

Posted on 2004-04-24
10
244 Views
Last Modified: 2010-08-05
I need to get this part functioning in this class?  I just added it from another class and am missing something!
All the code is provided below

JButton jcoloredbutton = new JColoredButton("" + n, Color.green, Color.black);


for (int i=0; i<10; i++)
      {
        JButton b = new JButton();
        b.setPreferredSize( new Dimension( 40, 20 ) );
        b.setHorizontalTextPosition(SwingConstants.LEFT);
        buttons.put("b"+(i+1), b);
        b.addActionListener(this);
        
                JButton jcoloredbutton = new JColoredButton("" + n, Color.green, Color.black);
      
                gridPanel.add(b);
      }

       }

     public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = EightPointBlocksArrayList.indexOf( jcoloredbutton ) ;

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

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

}

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);
     }



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 PointBlocks extends JPanel implements ActionListener
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;
     private Map buttons = new HashMap();
public PointBlocks(int ncols, int nrows)

     {
          EightPointBlocksArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, ncols));
          // 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<10; i++)
      {
        JButton b = new JButton();
        b.setPreferredSize( new Dimension( 40, 20 ) );
        b.setHorizontalTextPosition(SwingConstants.LEFT);
        buttons.put("b"+(i+1), b);
        b.addActionListener(this);
        JButton jcoloredbutton = new JColoredButton("" + n, Color.green, Color.black);
      gridPanel.add(b);
      }

       }

     public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = EightPointBlocksArrayList.indexOf( jcoloredbutton ) ;

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

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

}

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
  • 5
  • 5
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10910272
what is it exactly you want to do?

Is it you just want to create JColoredButton's instead of JButtons?

If so then replace:
 JButton b = new JButton();
with:
JButton d = new JColoredButton("", Color.green, Color.black);
0
 

Author Comment

by:Drop_of_Rain
ID: 10910319
That was easy, but I forgot something to ask.

I need to be able to have this ("timing.wav") replaceable with other wave files, that will come from a If statement or a JButton being pressed.
EightPointBlocksArrayList.add("timing.wav");




public void actionPerformed(ActionEvent e)
{
                               
          JColoredButton button = (JColoredButton)e.getSource();
         
          int index = EightPointBlocksArrayList.indexOf( jcoloredbutton ) ;

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

                jcoloredbutton .setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add( jcoloredbutton );
               EightPointBlocksArrayList.add("timing.wav");
 
                jcoloredbutton .setPressedColor();
          }
0
 
LVL 92

Expert Comment

by:objects
ID: 10910352
where would the name of the wav file come from?
0
 

Author Comment

by:Drop_of_Rain
ID: 10910395
That isn't decided yet. Would a ActionCommand be OK.
0
 
LVL 92

Expert Comment

by:objects
ID: 10910405
If its not being used for anything else already.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Drop_of_Rain
ID: 10910446
It won't be it will come from a button group for timing with different timing choices. Here is the code for that section. You might have to adjest it a little.

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 TimingChoices extends JPanel implements ActionListener
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;
     private Map buttons = new HashMap();
public TimingChoices ()

     {
          EightPointBlocksArrayList = new ArrayList();
          label = new JLabel();
          //Call it like this . . .
          gridPanel = new JPanel();
          gridPanel.setLayout(new GridLayout(0, ncols));
          // Setting MY layout...
          setLayout(new BorderLayout());
          add(label, BorderLayout.NORTH);
          add(gridPanel, BorderLayout.CENTER);
          label.setText("Timing Cycles");
          label.setForeground(Color.red);
          setBackground(Color.white);
                for (int i=0; i<10; i++)
              {
                JButton b = new JColoredButton("", Color.green, Color.black);
                b.setPreferredSize( new Dimension( 40, 20 ) );
                b.setHorizontalTextPosition(SwingConstants.LEFT);
                buttons.put("b"+(i+1), b);
               b.addActionListener(this);
               gridPanel.add(b);
              }

     }

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

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

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

}

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
 

Author Comment

by:Drop_of_Rain
ID: 10910452
I worgot to change the actionCommand wave files. They will be 2sec.wav - 11sec.wav
0
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 10910490
ok then using the action command should be fine.

EightPointBlocksArrayList.add(button.getActionCommand());
0
 

Author Comment

by:Drop_of_Rain
ID: 10910672
Thanks for all the support,
Christopher
0
 
LVL 92

Expert Comment

by:objects
ID: 10914079
No worries :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

707 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