Solved

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

Posted on 2004-04-26
11
332 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android development question 2 80
How to determine if a string is a valid SHA value 7 43
Glassfish admin console not working 1 59
Crystal Reports Licensing Questions 4 63
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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