?
Solved

Modify code so no names (numbers) are in buttons

Posted on 2004-04-23
9
Medium Priority
?
247 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 195 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 195 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 13

Accepted Solution

by:
Webstorm earned 210 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

770 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