Solved

need a part functioning in a class

Posted on 2004-04-24
10
254 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
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.

 

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

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

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 124
Error with Java/Cache JDBC Classpath 2 30
oracle 11g 23 49
Oracle SQL syntax check  without executing 6 34
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 how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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 …

910 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

23 Experts available now in Live!

Get 1:1 Help Now