Solved

Modify code so no names (numbers) are in buttons

Posted on 2004-04-23
9
240 Views
Last Modified: 2010-03-31
Can I get this code so no numbers (names for buttons) are in the buttons. But by doing this how would I identify the buttons to name them. still as button #1 through the last button?




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 javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class EightPointBlocks extends JPanel implements ActionListener
{
     JCheckBox onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;

public EightPointBlocks(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);
          looprow : for (int row = 0; row < nrows; row++)
          {
               for (int col = 0; col < ncols; col++)
               {
                    int n = (20 * col + row + 1);
                    if (n > 50)
                    {
                         break looprow;
                    }
                    JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
                    onOffButton.addActionListener(this);
                    //EightPointBlocksArrayList.add(onOffButton);
                    gridPanel.add(onOffButton);
               }
          }
     }


     public static void main(String[] args)
     {
          JFrame frame = new JFrame();
     
          // Instantiating the size . . .

          frame.getContentPane().add(new EightPointBlocks(2,2));
          frame.setSize(400, 400);
          frame.setVisible(true);

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }

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

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

               button.setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add(button);
               EightPointBlocksArrayList.add("timing.wav");
 
               button.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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 65 total points
ID: 10906504
    ...
     public JColoredButton(String label, Color background, Color foreground)
     {
          super();
          setActionCommand(label);
          ...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 10906575
listening
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 65 total points
ID: 10906934
If you don't want them to be numbered, breaking the three-arg constructor is not the way to do it if you genuinely want them caption-free. If this is the case, provide a new two-arg ctor

Another way you can approach this is to number their names instead of their captions. So not

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

but

JButton onOffButton = new JColoredButton("Click", Color.green, Color.black); // or whatever caption
onOffButton.setName("bOnOff" + n);

then in an ActionListener

public void actionPerformed(ActionEvent e) {
      Object o = e.getSource();
      if (o instanceof JColoredButton) {
            System.out.println("Button " + ((JColoredButton)o).getName() + " was clicked");
      }
}


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!

 
LVL 13

Accepted Solution

by:
Webstorm earned 70 total points
ID: 10907409
Hi Drop_of_Rain,

If you want to access buttons in order to change their states (color, disabled/enabled, label, ...) you can use a java.util.HashMap to retrieve them :
    HashMap hm_buttons=new HashMap();

    ....


           JButton onOffButton = new JColoredButton("" + n, Color.green, Color.black);
           hm_buttons.put("button"+n, onOffButton);

    ....

          JColoredButton button = (JColoredButton)hm_buttons.get("button2");


     
0
 
LVL 92

Expert Comment

by:objects
ID: 10909772
   public JColoredButton(String label, Color background, Color foreground)
     {
          super();
          setActionCommand(label);
          setPreferedSize( new Dimension( 50, 10 ) );
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10912337
As i mentioned before, breaking the 3-arg constructor is not the way to do it. You can do it like this:

public JColoredButton(Color background, Color foreground)
{
      // do it
}

.......

JColoredButton cb = JColoredButton(Color.red, Color.blue);
String label  = "X";
cb.setActionCommand(label);
0
 
LVL 92

Expert Comment

by:objects
ID: 10914028
> As i mentioned before, breaking the 3-arg constructor is not the way to do it.

We heard you the first time. Doing it as I have proposed is a far simpler approach, and doesn't break anything.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10914098
>>and doesn't break anything.

Then you obviously don't understand...
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 11013335
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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
check java version using powershell 13 304
spring jars download 1 53
ejb on wildfly 5 48
SequenceInputStream example 3 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

726 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