Solved

need a part functioning in a class

Posted on 2004-04-24
10
271 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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
Android development question 2 54
Java Restore security prompts not working 10 67
add projects t working set in maven 2 23
Notify sent to other threads in Java 9 33
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

829 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