Solved

Modify code so no names (numbers) are in buttons

Posted on 2004-04-23
9
237 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
diffSum example 4 50
ejb stateless example 2 23
Chrome and Firefox Java 5 50
Is there a simpler dropbox system? 10 34
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

860 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