Solved

need a part functioning in a class

Posted on 2004-04-24
10
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
servlet web applications   metadata-complete="true" or false 3 191
iterator/ListIterator approach 17 52
null output 3 42
spring maven example issues 3 39
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 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