Solved

example of setting text to the LEFT a button in this class

Posted on 2004-04-26
11
301 Views
Last Modified: 2010-03-31
Can someone give me an example of setting text to a button in this class which is set to the LEFT.


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 java.util.*;
import java.awt.Dimension;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class PointBlocks extends JPanel implements ActionListener
{
     JButton onOffButton;
     JLabel label;
     JPanel gridPanel;
     ArrayList EightPointBlocksArrayList;
     ArrayList arrayList;
     private Map buttons = new HashMap();
public PointBlocks(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);
        for (int i=0; i<10; i++)
              {
                JButton b = new JColoredButton("", Color.green, Color.black);
                b.setPreferredSize( new Dimension( 40, 20 ) );
                b.setHorizontalTextPosition(SwingConstants.LEFT);
                buttons.put("b"+(i+1), b);
               b.addActionListener(this);
               gridPanel.add(b);
              }
       b1.setText(Front Roundhouse Kick);
     }

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

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

                b.setDefaultColor();
          }
          else
          {
               EightPointBlocksArrayList.add(b);
               EightPointBlocksArrayList.add("timing.wav");
 
                b.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
  • 6
  • 5
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10925686
b.setHorizontalAlignment(SwingConstants.LEFT);
0
 

Author Comment

by:Drop_of_Rain
ID: 10925721
             for (int i=0; i<10; i++)
            {
              JButton b = new JColoredButton("", Color.green, Color.black);
              b.setPreferredSize( new Dimension( 40, 20 ) );
              b.setHorizontalTextPosition(SwingConstants.LEFT);
              buttons.put("b"+(i+1), b);
             b.addActionListener(this);
             gridPanel.add(b);
            }
       b1.setText(Front Roundhouse Kick);//  something like this where and how
   }


b.setHorizontalAlignment(SwingConstants.LEFT);

That is already there, I mean to get the text in the panel

b1.setText(text);  something like this and where should I place it in the class
0
 
LVL 92

Expert Comment

by:objects
ID: 10925745
whats b1?
0
 

Author Comment

by:Drop_of_Rain
ID: 10925757
I thought b1 was the first button. What are the names of the buttons anyway?
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10925775
technically they don't have names, they are stored in the buttons map by name.

JButton b1 = (JButton) buttons.get("b1");
b1.setText("Front Roundhouse Kick");
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Drop_of_Rain
ID: 10925828
buttons.put("b"+(i+1), b);

This is where I got the idea that the first button was b1, was that right?
0
 
LVL 92

Expert Comment

by:objects
ID: 10925877
its keyed on "b1" in the map yes.
0
 

Author Comment

by:Drop_of_Rain
ID: 10925905
this is where I would place them

for (int i=0; i<10; i++)
            {
              JButton b = new JColoredButton("", Color.green, Color.black);
              b.setPreferredSize( new Dimension( 40, 20 ) );
              b.setHorizontalTextPosition(SwingConstants.LEFT);
              buttons.put("b"+(i+1), b);
             b.addActionListener(this);
             gridPanel.add(b);
            }
  JButton b1 = (JButton) buttons.get("b1");             // this is where I would place them
  b1.setText("Front Roundhouse Kick");

   }

0
 
LVL 92

Expert Comment

by:objects
ID: 10925922
yes
0
 

Author Comment

by:Drop_of_Rain
ID: 10925965
Thanks again for all the support,
Christopher

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20967309.html

Go to this question and read the last comment by me
0
 

Author Comment

by:Drop_of_Rain
ID: 10925984
Thanks for all the support,
Christopher
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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:

708 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

21 Experts available now in Live!

Get 1:1 Help Now