Solved

Modify code so no names (numbers) are in buttons

Posted on 2004-04-23
9
221 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
 
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
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.

 
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

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
Java asynchronous logging 4 50
mockito example issue 8 64
starter POM and spring-boot-starter,  spring-boot-web 2 43
ForLoop Example 3 40
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This video teaches viewers about errors in exception handling.
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 …

863 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

26 Experts available now in Live!

Get 1:1 Help Now