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

Similar Messages

  • Change button names

    Hi Experts,
    Is it possible to change the button name in SAP documents? Ex. SO button 'ADD' changed to 'SAVE'.
    Thanks.
    Don

    Hi Don,
    Unfortunately, it is not possible to change button names, we can only change label names. If you want to change button names, you need to customize an addon.
    Hope it helps.
    Chinho

  • Change Button Name in JOptionPane.showMessageDialog

    Hello,
    I've written the following code that invokes a JOPtionPane messageDialog, when the delete button is hit in the following frame and the selectedItem in the JComboBox is Default.
    I want to change the JOPtionPane button name from OK to Close. Is there a way to do this with error message dialoges, as their is with confirmDialoges?
    Please help out.
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    public class MyJOptionPaneTest {
         public MyJOptionPaneTest()
              JFrame frame = new JFrame("My Frame");
              JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
              final JComboBox myComboBox = new JComboBox();
              myComboBox.setPreferredSize(new Dimension(100,20));
              myComboBox.getModel().setSelectedItem("Default");
              myComboBox.addItem("Default");
              myComboBox.addItem("Other");
              JButton deleteButton = new JButton("Delete");
              deleteButton.addActionListener(new ActionListener()
                   public void actionPerformed(ActionEvent e)
                        if(myComboBox.getSelectedItem().equals("Default"))
                             JOptionPane.showMessageDialog(null, "Default cannot be deleted", "Error", JOptionPane.ERROR_MESSAGE);
              panel.add(myComboBox);
              panel.add(deleteButton);
              frame.getContentPane().add(panel);
              frame.setSize(200,100);
              frame.setResizable(false);
              frame.setVisible(true);
         public static void main(String [] args)
              MyJOptionPaneTest test = new MyJOptionPaneTest();
    }

    Hi,
    yes, I've looked at the tutorials. But there is no way to change button title for JOptionPane.showMessageDialog. Only JOptionPane.showOptionsDialog provides you w/ that feature, becuz it provides the parameter in the method.
    I was wondering if there was another way to do this for showMessageDialog.

  • I have changed the name and password for my Time Capsule and now the Time Machine will not connect

    Mac Book Pro
    1TB Time Capsule.
    Yesterday I changed the name and password on my time capsule and also the name and password of the wifi network as I was having problems with the speed of the internet when using Safari on any of my devices (1 Macbook pro, 1 macbook air, 27" iMac, iPhone and iPad) all other internet services were working fine (email etc.) This seems to have solved the problem.
    However, since then, my time machine back ups will not reconnect and there is no option for me to reconnect to the existing back up for my macbook pro.
    When I enter the time machine preferences it shows as below that there are no back ups.
    Then, when I go to select a drive to use which should be the MacBook under the Backup disks the only option is for me to Remove Disk...
    So, my question is, how do I reconnect to an existing Time Machine back up for my MacBook pro as I really do not want to erase and start all over again.
    Warning - In my naiveté, I did click on the MacBook in 'Available Disks' but I got the impression that it was about to start a whole new back up and not work with the existing one.
    (And does anybody have an inkling as to why there was a problem with Safari on all my devices? I downloaded Google Chrome and that worked fine).

    Simply reset the Time Machine.
    See A4 here http://pondini.org/TM/Troubleshooting.html
    You can then mount the TC disk in finder after you reboot the computer.
    You should be able to reselect the TC disk and then it will say it is going to start a new backup.
    Ignore this.. when the TM actually loads the TC disk it will discover the existing backup and continue the backup.. but it will give no hint until it actually starts that it won't start a new backup. you just have to believe it won't.. and mostly it works..
    Please also install the widget A1 so you have some idea what TM is doing.

  • Change domain name BPC 10 for MS

    Hi,
    We will change domain name so I will need to change that in BPC for all the end users, which should not be a problem but for the service users running BPC, BPCInstall, BPCAdmin and BPCUser I guess it can/will be a problem.
    Option 1 - reinstall SAP BPC
    I have read a sap note(1451175) where they suggest to reinstall SAP BPC,http://service.sap.com/sap/support/notes/1451175
    and also sap note(1692721),http://service.sap.com/sap/support/notes/1692721
    If I do that the question is can I restore my environment(s) I have today or do they contain the "old" domain name users or is that only stored in the "appserver".
    Option 2 - modify SAP BPC
    Not recommended according to above note.
    Do anyone have any experience in this or suggestions please let me know.
    Brgds
    Mattias Ferling

    Multiple primary zones work fine within you private DNS services, and would be the usual approach for the local DNS server.
    The external (public) DNS services gets another translation and an MX (mail exchange) record that points at your public IP.
    If you're renaming the host itself, you'll reset the name through Server Admin, and then verify the change by launching Terminal.app and issuing sudo changeip -checkhostname command.  If that doesn't work, you might need to reset the host name via the changeip command; see man changeip for details.
    One key piece of all this: your public mail server needs to be an A record, which means your public forward and reverse DNS for your mail server and your MX record and your public static IP address all match.
    Your mail server will need to enable the new domain as a virtual host; that'll all it to receive mail for the new domain.

  • Want to change button background using UIManager for WindowsLookAndFeel

    Hi first i used ocean look and feel in my application. i used UIManager to change to color of the button. so all the buttons colors are change. fine
    But if i change the look and feel to windows look and feel it shows the windows look only. i sant to change the color to white. how?

    de to set the look and feel:
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");code to change button property
    UIManager.get("Panel.background");
    UIManager.put("Button.background", Color.WHITE);
    UIManager.put("Button.border", javax.swing.BorderFactory.createLineBorder(Color.black));
    UIManager.put("Button.opaque", true);change button properties are not affecting the jbutton. if i create.
    But if i remove the setLookAndFeel line properties are applying. why? remedy? is changing the ButtonUI is only option?

  • How to load delta, changing the name of file for every day with open hub?

    Hi gurus,
    I load daily delta data from ODS to flat file, using open hub.
    The file is located on server. Now, if I do delta load from ODS to flat file, every day the file is overwritten, because it has the same name.
    I want to load delta data everyday to different file name, for example BW_20060101 , BW_20060102, BW_2006_01_03 ....
    How do I do that?
    Best regards,
    Uros

    Hi Uros,
    This thread may give you some idea
    Changing the file name n Flat file Extraction
    https://websmp107.sap-ag.de/~form/sapnet?_FRAME=CONTAINER&_OBJECT=011000358700002201112003E
    Regards,
    BVC

  • How to change button names linked with the 'connect command?'

    Hi,
    I have linked a button in the sequence view to the Exeution entry point 2 (Single Pass) using the 'Connect command'. The button now displays 'Single Pass' as its caption name while running. I want to give a custom name to this button, but it should still be connected to the single pass function. How can rename it?
    Regards
    Gopal

    Hi,
    see this link: http://forums.ni.com/ni/board/message?board.id=330&message.id=7318&query.id=380755#M7318
    You will find the Text for the buttons in one of the ini files. Use the CustomStrings.ini to define your new text, which will override the text defined in the other ini file located in the NI folder when the Operator Interface or SeqEditor first launched and loads the various resources.
    Hope this helps
    Regards
    Ray Farmer
    Regards
    Ray Farmer

  • Changing personal name on software for editing?

    Hello,
    I am having problems with changing the license name of the software. I am using my sons old computer - when editing proofs his name is appearing even though I have changed it to mine in the preferences.
    I have also changed the PC name through the control panel with no luck.
    I am using the trial version currently, but planning to purchase after its up - Any help with this would be great.
    Edit: Under identity in preferences the login name is greyed out with sons name which I can't edit...
    Many thanks,
    Peter

    This forum is only to discuss how the forums operate, not products
    If you go to the Forums Index http://forums.adobe.com/index.jspa
    You will be able to select a forum for the Adobe product(s) you use

  • How do i change my name on airdrop for the iPhone? It shows up as the name on the account not the iPhone owner (which is me)

    I would really like to change my airdrop name but i have no idea how

    Our iPad wasn't showing the name of the person signed in to Find My Friends under ME.  It was showing someone else.  We deleted that person's contact from Contacts.  Removed Find My Friends.  Rebooted iPad.  Reinstalled Find My Friends.  Now it says My Location.   Better.   Added the contact back in - still working fine.

  • Need help to change id name and password for second hand ipad payyed good money for it bought it at swapmeet cant find the old owner very fusterating please help

    please help me set my password and id name for my ipad its not fair i bought it from a person for a high price and apple wont help you out. but you can reset anyother computer by your self with no problem

    I am afraid you cannot activate the iPad without the Apple ID and Password of previous owner.

  • Change Event Name in iPhoto for iOS

    I've bought in to Apple's promise of the "PC free" lifestyle with an iPad and the camera connectivity kit. A major problem is the fact that you couldn't manage events on the iPad with the standard "Photos" app.
    With the new "iPhoto for iOS", will I finally be able to restore organization to my library? I would love it if I could simply name events and perhaps do a little split/merge here and there.
    Thanks for your help,
    Mitch

    I agree. I bought the camera connect kit and was disappointed that I couldn't rename the events at all.

  • Change  buttons name

    Hi guys, i'm trying to make an application for a Q&A format, but i'm having difficulties with one last thing.
    I have 10 buttons made, what i need is -
    I want to click on a button, and it should exchange the text in it from Answer 1  to XPTO
    same for button 2 to YYYY and so on.
    Can you help please?

    You could do something like this:
    <mx:Button id="Button1" label="My Initial Label" click="Button1.label='My Answer'"/>
    or look into ToggleButton: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/components/Toggle Button.html

  • 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]

  • I have changed the name I use for my Apple ID.  How do I change it on my iPhone?

    I resolved this and do not know how to remove or cancel it.  Please ignore.

    Did you change the email address of your existing Apple ID, or did you create a totally new Apple ID? Assuming that you did it correctly and just changed the email address on your Apple ID, go to Settings/iCloud and turn off Find my iPhone. You will need the passcode for the OLD email address to do this. Then log out of iCloud on the phone. Log in with the changed address. Do the same for Settings/messages, Settings/Facetime and Settings/iTunes&App Store.

Maybe you are looking for

  • How to start service as specific user!!!

    Hi all, I need to create an apache service script. All is fine with this and I've imported etc but its running as root and I want to run as apache. Is there an easy way of doing this other than an su -c??? Cheers

  • Search Help Entries

    Hi,    When i try to manipulate the entries in a search help created in a customized field on MM01 screen, the search box is not showing the values.   well, i have created a search help exit by F4IF_SHLP_EXIT_EXAMPLE function, but when a try to manip

  • Does Pages have a password function to protect my files from kids?

    Hi all, I want to protect my Pages files from tampering by our kids if I get distracted by someone at the front door and don't log out of all my software and computer. I downloaded the trial version, and it doesn't seem to have the Password option in

  • Error message - library won't open

    error messages: There was an error opening the database for the library. How do I restore.  this is not the kind of message that leads to restoring the library. 

  • Short bursts of white no

    I have a Zen Touch and when I rip CD's using Creative Media Source (or Real Player) I end up with seemingly random bursts of white noise that last 2 or 3 seconds. This noise is in the files (since I get it if I play them back on my PC too) so I don't