Solved

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

Posted on 2004-04-26
11
309 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
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.

 

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

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
Exception after setting jdbc session management 2 51
starter POM and spring-boot-starter,  spring-boot-web 2 40
maven project error 5 44
replace a word with other 1 40
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 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.

939 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

16 Experts available now in Live!

Get 1:1 Help Now