Making JButton name a varible for use in for loops

Ok I've got some 98 buttons which are laid out in a grid, and a for loop would work to make thing easier when it came time to make adjustments or changes. Therefore, I would only have to make changes once instead of editing some 196 lines (Making the button, then adding for each button) Both the button names are incremental and their lables
Here is what I got:
// for loops to do the increase of Char Letter and Int Number
String buttonName = "rowButton" + Letter + "" + Number;
JButton buttonName = new JButton( Letter + " " + Number);
// closing of for loop
What do I need to do to make a string value to be the new button's name?

String buttonName = "rowButton" + Letter + "" + Number;
JButton buttonName = new JButton( Letter + " " + Number);
What language allows this? There has to be some language that allows >this, otherwise why do so many people think you can do it in java. Uhmm, actually it works fine in Java. One method to convert an integer to a string is to do:
int i = 10;
String new_string = "" +i;
//new_string == "10"

Similar Messages

  • Change button name in a for-loop

    Hi all,
    I would like to implement a 'for loop' to add buttons to my panel depending on the value of max_number.
    The postion of these buttons will also change with each passing of the loop.
    The code below implements this. But when I click the buttons they only return the action listener of the last button drawn. This is because the JButton my_button is reused in each pass through the loop.
    What I need is to change the name of the button on each passing of the loop so that I create a new button each time with it's own unique action listener.
    Is there a way of adding/appending the value of 'i' to the button name each time the for loop is run?
    For example
    When i = 0 -> JButton my_button0 = ...
    When i = 1 -> JButton my_button1 = ...
    etc, etc,
    for (i = 0; i < max_number; i++)
    JButton my_button = new JButton(Integer.toString(i));
    my_button.addActionListener( new ActionListener()
    { public void actionPerformed(ActionEvent event)
    { do something depending on the value of 'i' }});
    addComponent(my_button, (2*i+1) , 4 , 1 , 1); //add newly created button to position x = (2*i+1), y = 4
    } //end for

    What?! Are you talking about the variable name? You can't, shouldn't, needn't use different variable names in a for loop, since the variables "expire" each time the loop is run. For instance:
    for(int i = 0; i < 10; i++) {
       //j doesn't exists even if this loop is on its 10'th lap!
       int j = i;
    }And you will have a problem with the ActionListener as well, since it depends on the "i" variable. "i" can't be accessed from an anonymous class unless it has been declared final, in which case it won't work for you.
    So: the my_button variable will contain a new JButton each time!
    Nille

  • JButton not working with for loop...

    I am still very new to Java and am having trouble grasping it. I am trying to put together a program that paints a graphic and then each time the "Press Me" button is pressed, an oval randomly appears giving the impression that the picture is 'disappearing' a little at a time. When I run it now, 20 ovals randomly appear all at once instead of waiting for the button to be pushed. Does anyone have any advice for me on how to modify the code? Here is what I have so far...
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class JEraseImage extends JApplet implements ActionListener
    private ImageIcon image1 = new ImageIcon("bear.jpg");
    int startPosX = 80;
    int startPosY = 80;
    int imageWidth = 275;
    int imageHeight = 250;
    Container con = getContentPane();
    private JButton pressMeButton = new JButton("Press Me");
    public void init()
    image1 = new ImageIcon("bear.jpg");
    con.setLayout(new FlowLayout());
    con.add(pressMeButton);
    pressMeButton.addActionListener(this);
    public void paint(Graphics gr)
    gr.drawImage(image1.getImage(), 80, 80, (this));
    Graphics2D gr2D = (Graphics2D)gr;
    pressMeButton.repaint();
    gr.setColor (Color.white);
    for(int count = 0; count < 20; ++count)
    int x = (int) (Math.random() * imageWidth) + startPosX;
    int y = (int) (Math.random() * imageHeight) + startPosY;
    gr.fillOval(x, y, 50, 40);
    public void actionPerformed(ActionEvent e)
    Object source = e.getSource();
    if(source == pressMeButton)
    repaint();

    Here ya go, I think this should do it:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.util.*;
    import javax.swing.*;
    import java.awt.geom.*;
    public class Junk extends JApplet implements ActionListener
        private ImageIcon image1;
        BufferedImage bi;
        Graphics g;
        int x;
        int y;
        int startPosX = 80;
        int startPosY = 80;
        int imageWidth;
        int imageHeight;
        Container con = getContentPane();
        JButton pressMeButton = new JButton("Press Me");
        public void init()
           image1 = new ImageIcon("bear.jpg");
           bi = new BufferedImage(image1.getIconWidth(), image1.getIconHeight(), BufferedImage.TYPE_INT_RGB);
           g = bi.getGraphics();
           g.drawImage(image1.getImage(), 0, 0, null);
           con.setLayout(new FlowLayout());
           con.add(pressMeButton);
           pressMeButton.addActionListener(this);
        public void paint(Graphics gr){
            super.paint(gr);
            gr.drawImage(bi, startPosX, startPosY, this);
        public void actionPerformed(ActionEvent e)
           g.setColor(Color.WHITE);
           Object source = e.getSource();
           if(source == pressMeButton)
           for(int count = 0; count < 20; ++count)
                     int x = (int) (Math.random() * imageWidth) + startPosX;
                     y = (int) (Math.random() * imageHeight) + startPosY;
                     g.fillOval(x, y, 50, 40);
                     repaint();
    }Note: I've not run this.

  • Is it possible to create variables with an incremented name in a for loop?

    I want to create a number of variables according to the following:
    MyClass var_1 = new MyClass();
    MyClass var_2 = new MyClass();
    MyClass var_3 = new MyClass();
    MyClass var_n = new MyClass();
    Is there any way to do this in a loop ? Such as
    for ( int iii = 0 ; iii < 10 , iii++ ) {
    MyClass var_(iii) = new MyClass();
    Thanks

    ArrayList<MyClass> list = new ArrayList<MyClass>();
    for(int i = 0; i < 10; i++){
    MyClass c = new MyClass();
    list.add(c);
    MyClass [] myClasses = new MyClass[list.size()];
    list.toArray(myClasses);
    //when use the MyClass instance, use the element of the myClasses array by the index, as myClasses[0],myClasses[1]

  • Name of String var passed to a for loop?  Array question

    I have a series of String variables named q1String where 1 can be replaced by a value from 1 to 17.
    So I have a for loop:
    for (var i=1;i<18;i++){
    var question_caption_mc:Question_caption_mc = new Question_caption_mc;
    *question_caption_mc.caption_question_string.text=q+i+String;*
    The bold line is causing an error because q is being evaluated separately, i is being evaluated separately, and String is being evaluated separately.  I probably should use a different identifier in my variable definition than String but I was wondering how to properly pass the variable name into the for loop so that
    "question_caption_mc.caption_question_string.text" will equal the value of the variable q1String through q17String.  I know this is simple but I can't seem to figure out how to query this on a web search for a similar thread or solution.
    Can anyone help me solve this?
    Regards,
    -markerline

    Okay, I came up with something that produces the start of the result:
    for (var i=1;i<18;i++){
    var newQStringArray:Array = new Array("q",i,"String");
    var question_caption_mc:Question_caption_mc = new Question_caption_mc;
    var tempArray=newQStringArray;
    var tempString=tempArray.join("");
    question_caption_mc.caption_question_string.text=tempString;
    trace(tempString);
    the trace command comes up with all of the variable names.  However I would like
    question_caption_mc.caption_question_string.text
    to evaluate to the value of tempString or rather q1String through q17String.

  • ? on How to Load Variable Within For Loop

    Hello,
    I'm a PL/SQL newbee so I apologize up front if this is a basic question. In summary, I'm trying to build a field name within a FOR LOOP and also load this field with it's associated data from an input record and finally load the data to the output record and pipe the row. I can successfully build the field name but don't know how to load it with data from the input record.
    Here is a sample from the code.
    The input table is shown with the 6 different Fnma codes. Instead of checking each code for data, I want to build the field name, load this field name with data from the input record and move the data to the output record.
    CURSOR cur_loan(prod_date_in IN DATE) IS
    Select
    Ln.Ln_No,
    Ln.Prod_Date,
    Ln.Ln_Fnma_Ftr1_Cd,
    Ln.Ln_Fnma_Ftr2_Cd,
    Ln.Ln_Fnma_Ftr3_Cd,
    Ln.Ln_Fnma_Ftr4_Cd,
    Ln.Ln_Fnma_Ftr5_Cd,
    Ln.Ln_Fnma_Ftr6_Cd,
    from LOAN Ln
    where PROD_DATE = '16-FEB-11';
    Here is the FOR LOOP that successfully builds the field name in l_feat_code_name. Then I "assume" by moving this field to l_output that l_output will contain the actual data, not the field name. However, it appears to contain the field name instead of the data.
    FOR l_To IN 1 .. 6
    LOOP
    l_feat_code_name := ('l_loan_rec.ln_fnma_ftr'||l_to||'_cd');
    l_output := l_feat_code_name;
    IF (l_output is not null or
    l_output <> ' ') THEN
    l_out_rec.Inv_Type := l_Fnma_Cd;
    l_out_rec.feat_code := l_output;
    l_out_rec.Src_Delete_Flag := l_src_delete_flag_n;
    PIPE ROW(l_svc_ln_rec);
    END IF;
    END LOOP;
    How can I move the data itself into the l_out_rec.feat_code output record? Thanks in advance.

    Hi,
    l_output is defined as follows:
    l_output varchar2(20) ;
    I appreciate your example.
    Again, after I build l_feat_code_name it contains the column name from the input record, Ln_Fnma_Ftr1_Cd, ....Ftr2...., .....Ftr3..... and so on up to Ftr6. However, I also need the **data value** of Ln_Fnma_Ftr1_Cd, ....Ftr2...., .....Ftr3..... and so on up to Ftr6. My question is how to get the data value. You see below when I move l_feat_code_name to l_output I'm expecting l_output to contain the **data value**. Instead, when I display it in DBMS_output, it shows the column name.
    CURSOR cur_loan(prod_date_in IN DATE) IS
    Select
    Ln.Ln_No,
    Ln.Prod_Date,
    Ln.Ln_Fnma_Ftr1_Cd,
    Ln.Ln_Fnma_Ftr2_Cd,
    Ln.Ln_Fnma_Ftr3_Cd,
    Ln.Ln_Fnma_Ftr4_Cd,
    Ln.Ln_Fnma_Ftr5_Cd,
    Ln.Ln_Fnma_Ftr6_Cd,
    from LOAN Ln
    where PROD_DATE = '16-FEB-11';
    Here is the FOR LOOP that successfully builds the field name in l_feat_code_name. Then I "assume" by moving this field to l_output that l_output will contain the actual data, not the field name. However, it appears to contain the field name instead of the data.
    FOR l_To IN 1 .. 6
    LOOP
    l_feat_code_name := ('l_loan_rec.ln_fnma_ftr'||l_to||'_cd');
    l_output := l_feat_code_name;
    IF (l_output is not null or
    l_output ' ') THEN
    l_out_rec.Inv_Type := l_Fnma_Cd;
    l_out_rec.feat_code := l_output;
    l_out_rec.Src_Delete_Flag := l_src_delete_flag_n;
    PIPE ROW(l_svc_ln_rec);
    END IF;
    END LOOP;

  • Returning multiple values with SELECT FOR LOOP

    Oracle EE 11gR1
    PL/SQL
    OEL 5.8
    ===========
    Would like to know how to do the following?
    for x, y in (select name, street
    from employees
    where hire_date < sysdate - 100)
    loop
    In other words, is there a way to handle 2 (or more) values in a SELECT FOR LOOP structure? If not, then how to accomplish the same task.
    The above does not work of course. :-)
    Appreciate any and all advice.

    Yes, just have one name in your for loop and that becomes the equivalent of a table name and reference the columns as that name.column name
    for t in (select name, street
       from employees
      where hire_date < sysdate - 100)
    loop
      dbms_output.put_line('name = '||t.name);
      dbms_output.put_line('street = '||t.street);
    end loop;

  • Print a value outside the for loop.

    for (int j=0; j< 4; j++)
              String name = "AAAA";
              String add="BBBB";
              LogManager.logStatus("  NAMe.. ............   "+name);
    LogManager.logStatus("  Name ;;;;;;;;;;;   "+name);I want to how to print the "Name ;;;;;;;;;;; ". after the for loop.

    If i declared as
    private static String name;
    then the output is coming null in 2nd log statement.
    But i need the the print of 2nd log should be same as log in 1st .

  • Hi. I am using a time capsule for few PC s. I have made 5 different account to access time capsule. but in windows when i enter account name and password for one account, i cannot access other accounts, because windows saves username

    Hi. I am using a time capsule for few PC s. I have made 5 different account to access time capsule. but in windows when I enter account name and password for one account, i cannot access other accounts, because windows saves username. how can i prevent this from happenning. I really need to access all my accounts and dont want it to save automaticlly.

    Why have 5 accounts if you need to access all of them.. just have one account?
    Sorry I cannot follow why you would even use the PC to control the Time Capsule. Apple have not kept the Windows version of the utility up to date.. so they keep making it harder and harder to run windows with apple routers.

  • We purchased a new iPad2 and registered it using a 'new' iCloud email/ID. We are unable to send email from the iPad and iPhone. The error is: Cannot send mail. The user name or password for iCloud is incorrect.

    We purchased a new iPad2 and registered it using a 'new' iCloud email/ID. We are unable to send email from the iPad and iPhone. The error is:>> Cannot send mail. The user name or password for iCloud is incorrect.

    About ~20 hours later, this ended up solving itself. We can send email using the '.icloud' email from both the iPad and iPhone.  Advise would be 'wait' before you start seeking alteranatives like yahoo, hotmail, etc.  This definitely is a convenient way to keep all your 'cloud' information in a centralized place, including the common email...

  • Hi All.. i want to use my iphone 4 to play poker on facebook with the zynga..  idont know the apps name,, thank's for ur help, and how to upgrade ios 4.3.3, now i'm still using ios 4.1.1, thank's

    hi All.. i want to use my iphone 4 to play poker on facebook with the zynga..  idont know the apps name,, thank's for ur help, and how to upgrade ios 4.3.3, now i'm still using ios 4.1.1, thank's

    when you connect your phone with your computer and use itunes you can click on the phone
    and click on an update button
    about facebook games then think they are all flash based and iphones cant do flash so thats not possible

  • Using a for loop to populate JButton values with letters of Alphabet

    I am trying to create a column of 26 JButtons, with the text of the button being a letter of the alphabet, but I want to find a faster way to do it. I've tried using the Character class, but it won't let me use an int variable with the constructor. The primitive char class won't allow the '++' operand. I was thinking of using the Unicode values, but I haven't figured out a way to use them in a for loop. Any ideas?

    Hope this helps :-)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class CreateButtons {
         private static JPanel ToolPanel;
         final static JFrame ToolFrame = new JFrame("Drawing Tools");
         public static void main(String[] args) {
              ToolPanel = new JPanel(new GridLayout(17, 1));
              ToolPanel.setBorder(BorderFactory.createTitledBorder("Tools"));
              for(int i= 0; i < 26; i++) {
                   char c = (char)('a' + i);
                   makeButton(c);
              ToolFrame.getContentPane().setLayout(new BorderLayout());
              ToolFrame.getContentPane().add(ToolPanel, BorderLayout.CENTER);
              ToolFrame.setLocation(100, 100);
              ToolFrame.pack();
              ToolFrame.show();
         static public void makeButton(char buttonName){
              JButton button = new JButton( Character.toString(buttonName) );
                   button.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent e){
                             System.out.println(e.getActionCommand());
         ToolPanel.add( button );
    }

  • I have registered for Cloud in my own name but should have used my Business name to reclaim the VAT

    I have registered for Cloud in my own name but should have used my Business name to reclaim the VAT, how can I change my details

    This is an open forum with a mix of program users and Adobe staff, not Adobe support... you need Adobe support
    Adobe contact information - http://helpx.adobe.com/contact.html may help
    -Select your product and what you need help with
    -Click on the blue box "Still need help? Contact us"

  • Firefox will not remember multiple user names and passwords for same site, yet it used to do so. Any ideas?

    I belong to several publishers website, and I often have to access more than one author's account, which requires that I use their user name (usually an email address) and their password. In the past, whenever I double clicked on the space for user name, I would see all of those user names appear that I use for that site. When I clicked on the one I wanted, the stored password would appear in the password window, and I could log in. Now, it only shows me the most recent user name that I have used. I don't understand how to make it do what it used to do. Any ideas?

    Is it possible that that name is stored in a "remember me" cookie ?
    *"Remove the Cookies" from that site: Firefox > Preferences > Privacy > Cookies: "Show Cookies"

  • Making a folder with a Japanese name from Reader for Android

    I couldn't make a new folder with a Japanese name from Reader for Android but I can make one from the Web UI. What's wrong?

    I don't know if any language-specific version available on Google Play Store. I've just installed the Reader from the official Google Play Store.
    For your information, I can move files into any Japanese-named folder even on Android. Making a new one is the only problem I saw.

Maybe you are looking for

  • Extensions Gallery

    Fairly recently, I think following a Safari upgrade, a row of icons appeared on my Safari toolbar, below the main address input box and above the Bookmark tags. I have concluded that these are icons from the extensions gallery. I am at a loss to know

  • Help need iTunes 7

    I am overseas and unable to find a computer that will allow me download iTunes 7. I need to update my ipod so I can repair the corrupt database file. I am lost without my ipod. Please email to [email protected]

  • How to create an array using reflection.

    How to create an array using reflection. I want to achive something like this,Object o; o = (Object)(new TestClass[10]);but by use of reflection. To create a single object is simple:Object o; o = Class.forName("TestClass").newInstance();But how do I

  • Broken Link to 3rd Gen iPod Manual

    just FYI. http://manuals.info.apple.com/en_US/iPodDockConnectorUserGuide.PDF

  • Active Directory Group membership based on OIM Role

    In OIM 11g, is it possible to determine additional AD group membership based on role membership? If it is, could someone point me to documentation or give me a brief description of what to do in order to make this work? Thanks!