Solved

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

Posted on 2004-04-26
11
325 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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
 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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