[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

need a part functioning in a class

Posted on 2004-04-24
10
Medium Priority
?
286 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
Industry Leaders: 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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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…
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 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:
Suggested Courses

656 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