• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Modify code so no names (numbers) are in buttons

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
Drop_of_Rain
Asked:
Drop_of_Rain
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
objectsCommented:
    ...
     public JColoredButton(String label, Color background, Color foreground)
     {
          super();
          setActionCommand(label);
          ...
0
 
sudhakar_koundinyaCommented:
listening
0
 
CEHJCommented:
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
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!

 
WebstormCommented:
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
 
objectsCommented:
   public JColoredButton(String label, Color background, Color foreground)
     {
          super();
          setActionCommand(label);
          setPreferedSize( new Dimension( 50, 10 ) );
0
 
CEHJCommented:
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
 
objectsCommented:
> 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
 
CEHJCommented:
>>and doesn't break anything.

Then you obviously don't understand...
0
 
WebstormCommented:
:-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now