[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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

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
Drop_of_Rain
Asked:
Drop_of_Rain
  • 6
  • 5
1 Solution
 
objectsCommented:
b.setHorizontalAlignment(SwingConstants.LEFT);
0
 
Drop_of_RainAuthor Commented:
             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
 
objectsCommented:
whats b1?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Drop_of_RainAuthor Commented:
I thought b1 was the first button. What are the names of the buttons anyway?
0
 
objectsCommented:
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
 
Drop_of_RainAuthor Commented:
buttons.put("b"+(i+1), b);

This is where I got the idea that the first button was b1, was that right?
0
 
objectsCommented:
its keyed on "b1" in the map yes.
0
 
Drop_of_RainAuthor Commented:
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
 
objectsCommented:
yes
0
 
Drop_of_RainAuthor Commented:
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
 
Drop_of_RainAuthor Commented:
Thanks for all the support,
Christopher
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now