A question about how to change a button in a JPanel during runtime

I am a beginner of GUI. Now I am trying to change a specific component, a button, when the application is running. For example, I have 3 buttons in a JPanel. Each button has its onw icon. If I click one of them, it will change its icon, but the other two don't change. I don't know if there is any method for changing a specific component during runtime. If any one knows please let me know, I will appreciate that very much!!!

What you're going to have to do is loop inside the actionlistener but still have accessability to click while its looping. I don't know much about it, but I think you're going to need a thread. Try something like this... (it doesn't work yet, but I have to take off)
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class buttonxdemo extends JFrame implements ActionListener{
  Buttonx mybutton;
  //set it all up, make it look pretty  =]
  public buttonxdemo()
       mybutton = new Buttonx("default");
       getContentPane().add(mybutton.thebutton);
       mybutton.thebutton.addActionListener(this);
       this.setDefaultCloseOperation(3);
       this.setSize(200,200);
  public void actionPerformed(ActionEvent ae){
    if (ae.getSource() == mybutton.thebutton)
         if (mybutton.keepGoing)
           mybutton.keepGoing = false;
            else if (!mybutton.keepGoing)
                 mybutton.keepGoing = true;     
      mybutton = new Buttonx(/*Icon,*/"My Button");
      //getContentPane().remove(mybutton);
      //getContentPane().add(mybutton.thebutton);
      mybutton.startstop();
  }//actionperformed
  static void main(String args[])
       new buttonxdemo().show();
} //movingicondemo
class Buttonx extends Thread{
  public boolean keepGoing;
  //public Icon ICx;            //perhaps an array, so you can loop through?
  public String strbuttonx;
  public JButton thebutton;     //may have to extend JFrame?
  public Buttonx(/*Icon IC,*/ String strbutton){
    //ICx = IC;
    strbuttonx = strbutton;
    thebutton = new JButton(strbuttonx);
  public void startstop()
    int i = 0;
    while (keepGoing)
      thebutton.setLabel(strbuttonx.substring(0,i));
      //if an array of Icons ICx
      //thebutton.setIcon(ICx);
i++;
if (i > strbuttonx.length() - 1)
i = 0;
try
     Thread.sleep(1000);
catch (InterruptedException ie)
     System.out.println("sleep caught: " + ie);
}//startstop()
}//buttonx
kev

Similar Messages

  • Some questions about how to change scenes

    i have something like the following:
    fxml (view/main/MainMenu.fxml):
    <Scene fx:controller="view.main.CtrlMainMenu" xmlns:fx="http://javafx.com/fxml" stylesheets="view/main/main.css">
        <GridPane xmlns:fx="http://javafx.com/fxml" alignment="center" id="backgorund" hgap="10" vgap="10">
            <!--other panes -->
            <VBox spacing="8" GridPane.columnIndex="0" GridPane.rowIndex="1">
                <Button text="Start" onAction="#Start"/>
                <Button text="Options" onAction="#Options"/>
                <Button text="Exit" onAction="#Exit"/>
            </VBox>
        </GridPane>
    </Scene>main loop (cotroller/main.java):
    public class main extends Application{
        public static void main(String[] args){
            launch(main.class, args);
        public void start(Stage stage) throws Exception{
            stage.setTitle(CtrlLanguage.get(CtrlLanguage.TITLE));
            CtrlMainMenu main = new CtrlMainMenu();
            Scene scene = main.main();          //don't know how to make Scene scene = (Scene)FXMLLoader.load(getClass().getResource("MainMenu.fxml")); work on here since MainMenu.fxml is in view/main while the main class is in controller and ended up putting CtrlMainMenu in the view although is not very correct
            stage.setScene(scene);
            stage.setWidth(1080);
            stage.setHeight(720);
            stage.show();
    }view controller (view/main/CtrlMainMenu.java):
    public class CtrlMainMenu{
        //other buttons actions
        public Scene main() throws Exception{
            return (Scene)FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
        @FXML protected void Start(ActionEvent event){
        @FXML protected void Options(ActionEvent event){
        @FXML protected void Exit(ActionEvent event){
            System.exit(0);          //is there any other way to finish the program than using a system.exit?
    }got few questions currently:
    1. i'd like to be able to keep using CtrlMainMenu for other buttons while the ones that appear there go to the main (since will be reusing those classes from other scenes)
    tried adding fx:controller on panes instead of the scene, but it doesnt work at all (it crashes) else directly in CtrlMainMenu change the scenes of the stage, but then, i have to send the stage to every controller i use? (or how do i know the stage of the scene?)
    and in the case of options, i'd like after done, go back to the previous scene, is that possible? if so, how?
    2.if i want to implement languages, how do i do to change the text? tried with setText() but depending on where do i put it, it crashes, so dont know where do i have to put it
    3.im doing a game that may have different number of players, so, is there some way to erase/hide/add data from a gridpane with fxml?

    1. ok, found the way to change scenes, with this way:
    @FXML protected void Start(ActionEvent event) throws Exception{
         Node node = (Node)event.getSource();
         Stage stage = (Stage)node.getScene().getWindow();
         Scene scene = (Scene)FXMLLoader.load(getClass().getResource("../view/main/MainMenu.fxml"));     //this is just an example, will change to go to another scene
         stage.setScene(scene);
         stage.show();
    }but now i have another problem, when i click on the button the stage scene is changed, but the scene is transformed to a very small one at the top left (the size seems the same as when i dont put the set width/height on the stage when creating it), the stage size remains the same, and when i resize the window, it gets "repaired"
    the options, i thought on using a pane over the current pane so this way instead of going back would be just removing that new pane, would that work well?
    2. found about languages that is by using <Label text="%myText"/> and java.util.ResourceBundle but don't know how to use it, can someone provide me an example, please?
    3. still haven't looked on it...

  • Very simple question about how to change a specific field in a table

    Hi,
    I'm working with a simple JSF web application, I have just drag a table, the "travel" standard example, and I just had 3 columns, one with a drop down list, other with a static text field and a Button.
    Then I introduced the following java code in the Button:
    public String button1_action() {
    int quantity=Integer.parseInt(dropMenu.getValue().toString());
    quantityChosen.setValue(quantity);
    When the button is pressed, I can print and check that the value obtained is the one selected in the drop down list, of the respective row.
    The problem that I would like to write it in the respective static text field (in the same row) where the button was pressed.
    What happens is that it write the value in all the column. So it reads from the write place and then write in every column?
    How to write in the respective row?
    Thanks in advance, Junkeira

    Hi,
    I'm working with a simple JSF web application, I have just drag a table, the "travel" standard example, and I just had 3 columns, one with a drop down list, other with a static text field and a Button.
    Then I introduced the following java code in the Button:
    public String button1_action() {
    int quantity=Integer.parseInt(dropMenu.getValue().toString());
    quantityChosen.setValue(quantity);
    When the button is pressed, I can print and check that the value obtained is the one selected in the drop down list, of the respective row.
    The problem that I would like to write it in the respective static text field (in the same row) where the button was pressed.
    What happens is that it write the value in all the column. So it reads from the write place and then write in every column?
    How to write in the respective row?
    Thanks in advance, Junkeira

  • A few questions about how ZPM works.

    We have patch management (in ZCM 11.2.2), but honestly don't use it much. I have a few questions about how it works that might make me use it more, if I understand it more.
    If I deploy a patch (or a set of patches), it creates a bundle for that deployment. That bundle seems to include actions that deploy the actual patch bundles (correct?). Do I have to recreate a new deployment bundle every time I want to push a new patch? i.e. If I push a Java update, and a month later, a new one comes out, do I build out a new bundle with the new patch in it, or do I modify the old one?
    Once the patch is deployed, can I safely delete that deployment bundle, or should they just pile up?
    Is there a way to "auto-approve" patches? Lets say I always want a group of machines to have the latest Adobe Flash Player patches. Can I set up ZPM to automatically cache and push the latest patches for a specific product, or do I have to manually remediate each patch? (I'm thinking of how MS's WSUS does "auto-approval")
    I see most packages aren't cached in list, but occasionally, a patch is cached without me touching it. Why? Can I change what gets automatically cached?
    Thanks for any help/answers you can provide.
    -Adam

    Originally Posted by adrockk
    We have patch management (in ZCM 11.2.2), but honestly don't use it much. I have a few questions about how it works that might make me use it more, if I understand it more.
    If I deploy a patch (or a set of patches), it creates a bundle for that deployment. That bundle seems to include actions that deploy the actual patch bundles (correct?). Do I have to recreate a new deployment bundle every time I want to push a new patch? i.e. If I push a Java update, and a month later, a new one comes out, do I build out a new bundle with the new patch in it, or do I modify the old one?
    Once the patch is deployed, can I safely delete that deployment bundle, or should they just pile up?
    Is there a way to "auto-approve" patches? Lets say I always want a group of machines to have the latest Adobe Flash Player patches. Can I set up ZPM to automatically cache and push the latest patches for a specific product, or do I have to manually remediate each patch? (I'm thinking of how MS's WSUS does "auto-approval")
    I see most packages aren't cached in list, but occasionally, a patch is cached without me touching it. Why? Can I change what gets automatically cached?
    Thanks for any help/answers you can provide.
    -Adam
    For #1, (assuming you're not using baselines), you would check the new version of the patch (vulnerability) and do a new deployment.
    #2 - once you're satisified that the machines are deployed (or the best to your ability) you can delete the DEPLOYMENT package. It doesn't delete the actual vulnerability bundles to my knowledge. That's why it's a good idea to name your bundle deployments with something meaningful, IMO (and maybe include a nice desription).
    #3 - currently I don't believe this is possible. I know you can probably configure it to auto-download the patches, but not auto-deploy everything. Given the propensity for software to wreck other things (hello MS .NET patches), this is probably not a good idea. At least I'd never auto-download and auto-deploy any patches without testing them first, and certainly take my servers a little more cautiously than my workstations.
    #4 - I think you can configure what's cached, but I could be wrong.
    I know there's a lot of improvements coming in the pipeline, and it doesn't hurt to "vote" for your enhancements via the enhancement system (more work for Shaun--haha)
    --Kevin

  • Hi,sorry for my english.My question is how to change country when i have credit in my account.My account is US and i've 0.5 USD.Now,one frind from Canado bought some credit cards and i can't recharge my account.Tank you for helping me.

    Hi,sorry for my english.My question is how to change country when i have credit in my account.My account is US and i've 0.5 USD.Now,one frind from Canado bought some credit cards and i can't recharge my account.Tank you for helping me.

    BellaMichelle wrote:
    For some reason, I have had misgivings about partioning my internal HD.. not sure why.. sort of feels like I'm creating my own computer sybil ;-)  Truthfully, it seems weird to split it.. I don't think I can articulate it really.. but are there complications that can come with doing this?
    It's a common way to run multiple OSs, if there's room.  I've had two on my internal HD for a couple of years.  Plus a small partition for a copy of my Snow Leopard Install disc.  Just be sure to leave at least 10% to 15% free on each partition.
    Running Lion on a partition of the disk that also contains your backups is not a good idea; if that drive fails, you risk losing both copies of your stuff!
    OSX will also run somewhat more slowly, because access to an external is slower.  That probably won't be very significant, though.
    Also, would the decision be effected by how long I want to maintain the SL section?  I am assuming once I sort out what to replace Quicken with, I won't want to keep the SL section.  So would it be easy enough to 'clear out the SL data, and open the partition?
    Review #3 in Formatting, Partitioning, Verifying, and Repairing Disks. 
    All things considered, your best bet may be to  "clone" your SL partition to the second one, then (once you're sure everything is cool), erase the original (top) SL partition.  You can use the Restore tab of Disk Utility for that (#7 in the same link), or a "cloning" app such as CarbonCopyCloner or SuperDuper.  Then install Lion on the top partition.
    When you're done with SL, just delete it's partition and expand the Lion partition down into the vacated space.

  • How to change the button's clicked color

    Dear all,
    How to change the button's clicked color
    I want it to be white in color as my button's background color is white
    Thanks

    How to change the button's clicked colorvia the UIManager

  • Round trip workflow with proxies between Premiere and AE / a more general question about how these programs work?

    Hi all,
    I'm new to editing with proxies in Premiere and have some questions about how to handle the relationship with After Effects. Say I have a rough cut (with proxies) and I want to send it to AE to do some effects. The simplest thing to do seems to be to replace the proxies with the originals in Premiere, send the project to AE and do my thing. But if I then want to send the project back to Premiere, it will involve the original files, which are too machine-intensive for me to edit. In theory, it seems like there should be a way to send the project with original footage to AE, do effects, send it back to Premiere, and then replace it with proxies to do further editing while retaining the effects.
    This leads to a confusion about how AE works. When I do an effect, am I correct in assuming that it does nothing to the original file? If that's the case, it seems like it shouldn't matter (in the editing stage) what file AE "applies" an effect to (proxy or original). But I feel like there's a hitch in this workflow. The same question could also be asked about going back and forth in Speed Grade.
    Perhaps there is no real answer to this and the best option is to save effects and grading for after I have picture lock, but sometimes that's not entirely possible or convenient, etc.
    Hopefully this makes some sense—It's a little hard to explain.
    Thanks.

    Hi Mark,
    Here are the specific sections of the manual that should give you the best explanation regarding the Check out/in workflow for FCP projects in Final Cut Server:
    Check out:
    http://documentation.apple.com/en/finalcutserver/usermanual/#chapter=7%26section =5
    Editing:
    http://documentation.apple.com/en/finalcutserver/usermanual/#chapter=7%26section =6
    Check in:
    http://documentation.apple.com/en/finalcutserver/usermanual/#chapter=7%26section =7
    -Daniel

  • Hello! I have a huge question about how can I get connected my iphone 4 with my car Is there any opption on find my car application? I have a Volvo xc60 and I want to be connected all the time with my iphone. Can I do that?

    Hello! I have a huge question about how can I get connected my iphone 4 with my car Is there any opption on find my car application? I have a Volvo xc60 and I want to be connected all the time with my iphone. Can I do that?

    Hello. I can say that you have a quite strange „huge question”… It’s non-sense to stay connected with your car which is hundreds miles away. Unless…
    I have a theory. You don’t want to controll your car, you want to controll somebody who is driving the car. Volvo XC 60 is a nice family car, usually used by married men between age of 35-45, probably with small children, so it’s very unlikely that you want to controll your teanage kid, mainly because probably even if you would give him/her to drive the car in the neighborhoud, I don’t think that he/she would be „several hundred miles away”…  If your child is not young teneage anymore, and he/she has his/her own life, but you want to control him/her, that is sick… So I am convinced that you want to controll your husband who probably travelling often! Am I wright?
    Isnt’t nice at all! Would you like if you would be monitorized in such way? I bet you don’t!
    Anyway, iPhone is smart, you can use for many things, but come on, you really were thinking that there is such kind of application???
    What could you do it's to put in the car a GPS survelling system, however I don't think that you could do it without your husband knowledge, otherwise he won't be able to start the engine...

  • HT201303 I forget answer security questions. How to changer answer your security questions?.Please help me!

    I forget answer security questions.
    How to changer answer your security questions.
    Please help me!. Thankyou very much!

    Try going to https://appleid.apple.com/, click 'Manage your account' on the right-hand side of that page and log into your account. Then click on 'Password and Security' on the left-hand side of that page and see if you can change your questions/answers, and then click Save at the bottom right.

  • How to change the line item storage location during the sales order creatio

    How to change the line item storage location during the sales order creatio

    Hi Kumar,
    I think you can just delete it in the sales order directly, if you are using make-to-order scenario, then there will be special stock left for the sales order as the production has been goods receipt, you need to use MM transaction move the stock to unrestricted use stock. If you are using make-to-stock scenario, there should be no further problem. If you are using assembly order, please try to reject the sales order item to see if it could fullfill your requirement.
    Regards,
    Rachel

  • How to change a connection with the database in Runtime?

    How to change a connection with the database in Runtime?
    My connection was made using ADF Business component (ApplicationModule).
    ADF Swing.
    JDeveloper Studio 11.1.1.4.0.

    When deploying ADF applications with database connection, you should be using JDBC data sources configured in the weblogic server.
    You could change the JDBC data sources to a different DB instance or location - by changing the JDBC data source and restarting the weblogic server.
    For more details, check
    http://techiecook.wordpress.com/2010/12/02/oracle-weblogic-adf-datasources/
    Thanks,
    Navaneeth

  • Change values in File physical schema during runtime?

    Is there any way to change the Directory in physical schema
    during runtime?
    This is needed so that we can dynamically get the files from
    different locations.
    Any help is highly appreciated.

    Hi,
    Yes there is:
    Create your physical schema to a parent directory and, at Physical Resourse (File Datastore) use a variable before the name of the file.
    Compose the variable with the "child" directory. I like to create the path on execution time.
    Does it help you?

  • Question about how the Robocopy /B switch works...

    I've been experimenting with robocopy recently, and most switches seem self explanatory for the most part; although I have a question about the /B switch.
    The help says the following:
    /B :: copy files in Backup mode.
    I've checked multiple forums and websites to try and elaborate on this a little more, and from my understanding this switch enables backup mode that essentially gives the ability to change the acls of a file/folder when the account running robocopy does
    not have sufficient privileges, to perform a successful copy.
    My question is, when the acls are changed when using this "backup mode" switch, with the /copyall switch, will the file still retain the same user access that it originally had - or does it change all of the security settings?
    I'm hoping it will somehow change the security settings, create a copy, and retain the same acls as the original - so users can still access necessary files.
    Can someone please explain how this "backup mode" works?
    Thanks in advance.

    This is a very useful thread with a lot of hostility in it.
    I have to agree with Jonathan and hazymat: there are few other forums that will discuss the exact function of /b switch, the TechNet article just says "copies in Backup Mode" (I have the article bookmarked), and when doing a google search, THIS page comes
    up as the top result! Kinda difficult to tell people they should go search, when you are the exact destination of that search, isn't it? ;)
    No offense to anybody working hard to help out here, but this is why sites like Stack Exchange will overshadow every traditional tech forum very soon. More concise question/answer format, more civil, and they actually encourage old questions developing new
    answers! All q&a is cataloged for future reference. Their focus is on being a 'resource'.
    The negativity here is unfortunate, you guys have great potential to be a very helpful resource for people. Who are you trying to be, the guys with all the answers, or the guys who say "get out of here, your answer isn't in here. Go search for it."?
    Regardless, many thanks to jrv! for helping me with my robocopy batch SCRIPT which I just used to create an automated backup! Take care boys. And yes, I just bumped your old-old thread.
    Unfortunately I think you miss the point.  RoboCopy is an application.  It is not a script.  This is a scripting forum.  Many people come here nd never read the forum guidelines and do not ask script related questions.
    The /b switch is documented in RoboCopy help.  Of course it assumes the reader is a trained Windows tech and knows what backup mode is.  It is used by nearly all backup software when backing up a live system..
    ¯\_(ツ)_/¯

  • I have a question about how to contact HR with e-mail.

    I read the rules about anything about HR is not allowed, but I also read to contact HR directly, so I won't ask anything further here. My question is, how? I searched for an e-mail, but all I have been able to find is a phone number I can't ring since I have no phone, just internet. Thank you.

    Hello Dennis,
    If you're a Best Buy or Geek Squad employee and do not have access to a telephone, then two alternative contact options exist for you: speak with your direct supervisor or use the My HR app within Employee Toolkit. If you are not a Best Buy employee, however, our HR support team will not be able to assist you -- these contact channels are for employee use only.
    Thank you for your understanding, as well as for taking the time to post.
    Aaron|Social Media Specialist | Best Buy® Corporate
     Private Message

  • How To Change InputDialog Buttons?

    hi there
    i made an InputDialog
    and i want to change the button names ,how to do that?
    and how to react with the user clicks a button?
    here's what i did
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Custom_Dialog
        public static void main(String[] args) {
    String Text=JOptionPane.showInputDialog(null,"Input Something","Search",JOptionPane.QUESTION_MESSAGE);                                   
    }

    hi i made it but i have a problem
    is that when i press the Search Button The JDialog disappears and loses focus
    and i want it to still focused i used requestfocus() but it doesn't work
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.*;
    class Custom_Dialog
      public static void main(String[] args)
           JTextField textfield=new JTextField(5);
        Object[] options =
        {"Search", "Cancel"};
    Object[]array={"Input Something",textfield};
        JOptionPane pane = new JOptionPane(array,
            JOptionPane.QUESTION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, options);
        JDialog dialog = pane.createDialog(textfield, "Search");
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
        if(pane.getValue().equals("Search")){
             System.out.println("Search Pressed");
             System.out.println(textfield.getText()+" Is Entered");
             dialog.requestFocus();
        else{
             System.out.println("Cancel Pressed");
    }

Maybe you are looking for

  • Video player for ipod touch

    I am fun of downloading videos through the internet and the videos that I mostly downloaded can be played using a real player. Does ipod touch support this kind of video format,the type of video file that can be played using a real player?

  • Urgent need for Tutorials Financials, Purchasing & Distribution/Logistics

    Hi all, This post is off the topic, but since it was urgent, thought this might be helpful.. If you could guide me towards any tutorial for the Financials, Purchasing and Distribution/Logistics Modules it would be great. I have an metalink id and i t

  • Locks

    Hi I am facing a locks problem. I know how lock, but I don't know why. The user that locked is trying to insert row , and not to update . I query the row and object that has been requested by the locked user By query ROW_WAIT_OBJ#, dbms_rowid.rowid_c

  • Kernel Panics with Brand New Macbook Pro

    Hey guys, I've experience two kernel panics since purchasing my Brand new MBP three days ago. I really just want the problem to end, this is a new piece of equipment, it should function perfectly. At the time of the panic I had a Lacie 1TB USB HD plu

  • Speech analysis languages disappeared?

    The Speech Analysis feature in CC is a crucial feature that I use for my work, and as it has been REMOVED from CC2014 I have to work with the previous version. but today I notice it is unusable as the options in the analysis window are greyed out and