Dynamically create a button, but what happened?

I am new to Swing GUI, and I want to create a new button in a panel named centerPanel, so my code like this:
public class TestButton extends JFrame {
     public static void main(String args[]) {
          try {
               TestButton frame = new TestButton();
               frame.setVisible(true);
          } catch (Exception e) {
               e.printStackTrace();
     public TestButton() {
          super();
          setBounds(100, 100, 500, 375);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          final JPanel center = new JPanel();
          getContentPane().add(center, BorderLayout.CENTER);
          final JPanel south = new JPanel();
          getContentPane().add(south, BorderLayout.SOUTH);
          final JButton button = new JButton();
          button.addActionListener(new ActionListener() {
               public void actionPerformed(final ActionEvent arg0) {
                    JButton newbutton = new JButton("Heeeeeeeee");
                    center.add(newbutton);
                    center.repaint();
          button.setText("New JButton");
          south.add(button);
}but why this does not work, I can not see it? Please give me a hand, thanks in advance!

Call center.revalidate () after add (...), not center.repaint ().
db
edit Just noticed who the question was addressed to, thought I'd clarify that I'm not really authorized to answer on His behalf.
Edited by: Darryl.Burke

Similar Messages

  • What will i do with my ipad stuck on white apple logo and itunes not recognized it. i'll always try to press and hold both the home botton and sleep/wake button but nothings happen.

    what will i do with my ipad 2 ios 5.0.1 stuck on white apple logo and itunes not recognized it. i'll always try to press and hold both the home botton and sleep/wake button but nothings happen.

    if that dose not work then you can always put it into DFU mode by connecting to your computer and holding down the home button and sleep/wake button for excatly 10 seconds and then relesing the sleep/wake button and keep holding down the homebutton till iTunes tells you it has detected an iPad in recovery mode then just do a restore

  • Creating a Function logic for dynamically created XML buttons

    Hi!
    It's me...... again! Now I've dynamically created some buttons using XML. They're spread around the stage and I've modified a tooltip script to give each button a tooltip on Mouse_Over. But to se the logic and make it work using AS3 is hard (for me). I want a function that accept to parameters: Tooltip text and  Object to tooltip.
    In my code I get this error msg when initiating the function on dynamically created buttons:
    1118: Implicit coercion of a value with static type flash.display:Sprite to a possibly unrelated type flash.display:MovieClip.
    I beleive there are more than one thing here needing a fix.
    Can someone have a look and give me a pointer?
    Thanks
    function contentTooltip(ttt:String, ttclip:MovieClip):void {
        ttclip.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
        function mouseOverHandler(e:MouseEvent):void {
            ttip.descr.text=ttt;
            ttip.x=stage.mouseX;
            ttip.y=stage.mouseY-15;
            ttclip.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            ttclip.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            ttip.visible = true;
        function mouseOutHandler(e:MouseEvent):void {
            ttip.visible = false;
            ttclip.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
            ttclip.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        function mouseMoveHandler(e:MouseEvent):void {
            ttip.x=stage.mouseX;
            ttip.y=stage.mouseY-15;
    contentTooltip("Scale button",scale_btn);
    contentTooltip("Hide button",hide_btn);

    I totally agree with what Ned says and suggests. Nevertheless, I would like to support your thinking process.
    From the way you wrote the tooltip functionality it is apparent to me that you conceptualize as a programmer. Again, as Ned said, nested functions are evil. BUT, in a way, what classes accomplish is encapsulation/nesting of properties and functions under the same umbrella. It actually feels that what timeline does in general is nesting named functions within a single function we have no access to.
    How you wrote the code is actually a blueprint for a class that could handle the functionality. You, perhaps, are very ready to start coding with classes - not on the timeline.
    With that said, for the sake of theory, here is how your functionality can be rewritten on timeline:
    scale_btn.toolTip = "Scale button";
    test_btn.toolTip = "Test button";
    hide_btn.toolTip = "Hide button";
    scale_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
    test_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
    hide_btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
    var overTarget:MovieClip;
    function mouseOverHandler(e:MouseEvent):void {
         overTarget = e.currentTarget;
         ttip.descr.text = overTarget.toolTip;
         ttip.x = stage.mouseX;
         ttip.y = stage.mouseY - 15;
         overTarget.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
         overTarget.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
         setChildIndex(ttip, numChildren - 1);
         ttip.visible = true;
    function mouseOutHandler(e:MouseEvent):void {
         ttip.visible = false;
         overTarget.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
         overTarget.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); 
    function mouseMoveHandler(e:MouseEvent):void {
         TweenMax.to(ttip, .5, { x:stage.mouseX, ease:Quart.easeOut } );
         TweenMax.to(ttip, .5, { y:stage.mouseY - ttclip.height / 2, ease:Quart.easeOut } );
         //ttip.x=stage.mouseX;
         //ttip.y=stage.mouseY-ttclip.height/2;

  • I can't delete a song from on-the-go playlist.  I've got a classic 80gb.  I've held down the button but nothing happens.  Any suggestions?

    I can't delete a song from on-the-go playlist.  I've got a classic 80gb.  I've held down the button but nothing happens.  Any suggestions?

    bostonshazzer,
    What model iPod do you have?  This feature is not supported on all models. You are positive you are holding down the Select (Center) button with the song highlighted until it disappears?  Are you sure you are in your On-the-Go playlist and not just a regular one?
    B-rock

  • HT4623 MY iPad screen is stuck on the "terms and conditions screen.  I tap the agree button and the send  email button, but nothing happens.  Please help.

    While trying to update my Ipad to the new operating systemm my screen is stuck on the "Terms and Conditions" screen.  I tried touching the "agree" button and the "send my email" button, but nothing happens.  I can't get out of the screen without missing my full update.  Please help.

    completely switch off your ipad with pressing the top power button  really long and then swipe the red swiper at the top. Then turn your  ipad back on, skip the icloud login screen and you should be good!  That's what i did so should work for you too

  • I am having trouble downloading Premier Pro, as i click the download button but nothing happens why is that?

    I am having trouble downloading Premier Pro, as i click the download button but nothing happens why is that?

    Do you have a 64bit computer, and what is it?
    Link for Download & Install & Setup & Activation problems may help
    -Chat http://www.adobe.com/support/download-install/supportinfo/
    OR
    Some download & install troubleshooting links
    -Comodo Security kills download http://forums.adobe.com/thread/1460361?tstart=0
    -http://helpx.adobe.com/x-productkb/policy-pricing/activation-network-issues.html
    -http://helpx.adobe.com/creative-cloud/kb/error-downloading-cc-apps.html
    -http://forums.adobe.com/community/download_install_setup
    -http://helpx.adobe.com/creative-cloud/kb/troubleshoot-cc-installation-download.html
    -http://helpx.adobe.com/x-productkb/global/errors-or-unexpected-behavior-websites.html
    -http://helpx.adobe.com/creative-cloud/kb/unknown-server-error-launching-cc.html
    -Server won't connect https://forums.adobe.com/thread/1233088
    -SPECIAL FOR MAC USERS
    -Mac 10.9.3 workaround https://forums.adobe.com/thread/1489922
    -more Mac 10.9.3 https://forums.adobe.com/thread/1491469
    -Enable Mac Root User https://forums.adobe.com/thread/1156604
    -more Root User http://forums.adobe.com/thread/879931
    -and more root user http://forums.adobe.com/thread/940869?tstart=0

  • I want to change my Apple ID because I have a new email address, but what happens when someone tries to facetime me using my old Apple ID?

    I want to change my Apple ID because I have a new email address, but what happens when someone tries to facetime me using my old Apple ID? Will they still be able to contact me on facetime using my old Apple ID?

    Not if you change the ID.  You would need to give them the new address.

  • How to transfer files like music from mac to ipad? tried it using mac but what happen was opposite!

    how to transfer files like music from mac to ipad? tried it few times but what happen was the opposite.

    If you want to sync your iTunes library on your Mac to your iPad ....
    Connect your iPad to the Mac and launch iTunes. Click on your iPad's name on the left side under the devices heading. Then click on the Music tab - and select the music that you want to transfer. Does your iTunes window look like this - with Sync Music checked and the playlists or albums checked as well like in the photo below?
    When you click on the apps tab do you have Sync Apps checked and all of the apps in the window below checked to sync as well?
    If you select all of the items under the different tabs in iTunes - music, apps, photos. etc. - when you sync - all of the selected content should sync over to the iPad.

  • I'm running Safari 6.0.5 - Mac OS 10.8.4 -- when I load my gmail the block around the DELETE and MOVE tabs are not usable - press buttons but nothing happens. ???

    I'm running Safari 6.0.5 - Mac OS 10.8.4 -- when I load my gmail the block around the DELETE and MOVE tabs are not usable - press buttons but nothing happens. ???
    I can see the block (flashes) and the buttons are no longer usable.
    I have Empted Cache but doesn't seem to help.

    From the Safari menu bar, select
    Safari ▹ Preferences ▹ Extensions
    Turn all extensions OFF and test. If the problem is resolved, turn extensions back ON and then disable them one or a few at a time until you find the culprit.
    If you wish, you may be able to salvage the malfunctioning extension by uninstalling and reinstalling it. Its settings will revert to their defaults. If the extension still causes a problem, remove it permanently or refer to its developer for support.

  • HT4854 Mountain Lion will not download. App just says "download" on purchase button, but nothing happens. Any ideas?

    Mountain Lion will not download. App just says "download" on purchase button, but nothing happens. Any ideas?

    My download took many many hours.  Not sure if that is because of our ISP or load on the network.  I also thought nothing was happening for my download.  Try this:
    Open App Store,
    click on Purchased
    You'll see Mountain Lion being downloaded and it will show the progress and estimated time to complete. 
    Take deep breaths and be patient. 

  • Hi i'm from turkey.ios developer program license agreement page i'm clicking i agree button but nothings happen

    Hi i'm from turkey.ios developer program license agreement page i'm clicking i agree button but nothings happen

    I see - [Error] ReferenceError: Can't find variable: $$
              canSubmit (licenseAgreement.js, line 19)
              onclick (index.action, line 1)
    var btnElements = $$('#submitBtn,#listAgreementForm_agreeButton'); ???

  • Clicked on Show or Hide HP Smart Web Printing button but nothing happened.

    I Clicked on Show or Hide HP Smart Web Printing button but nothing happened. I tried uninstalling and reinstalling... and i am already in a 32 bit browser on an 84 bit operating machine with adobe flash player. Please send me some advice for problem shooting... Thank you all!

    Hey HP ...  Does anyone from HP monitor these posts and provide answers, as one might expect?  Is anyone listening?  Is anyone there?  Does anyone CARE?

  • HT201269 Was downloading new software os 6.1.3 and the iPad seems to have frozen. Have tried the off button but nothing happens. Any suggestions?

    Was downloading the os 6.1.3 software and the iPad seems to have frozen. Have tried the off button but nothing happens. Doesn't indicate that the power is on.

    Then try this now.
    Reboot the iPad by holding down on the sleep and home buttons at the same time for about 10-15 seconds until the Apple Logo appears - ignore the red slider if it appears on the screen - let go of the buttons. Let the iPad start up.

  • On my iphone I no longer have an icon for messaging on one of my contacts. I can still text that person and have his phone number but what happened to the icon?

    One of my contacts no longer has an icon for texting. It show the phone icon and his number is still present. I can still send and receive texts from this person but what happened to the "balloon" icon?

    Can you delete the contact, reset your phone and then re add the contact again?
    (982)

  • HT1766 My ipad has the message "iCloud Backup This iPad hasn't been backed up in 2 weeks..." followed by the "OK" option. I press the "OK" button but nothing happens. I never backed up the iPad through iCloud though, so how do I restore it?

    How do I restore the icloud backup for my Ipad if I never set up the "back up" in the first place? My Ipad is in a frozen state. I press the OK button but nothing happens. The message on the iPad is "iCloud Backup This iPad hasn't been backed up in 2 weeks. Backups happen when this iPad is plugged in, locked, and connected to Wi-Fi."

    Perform a Reset...
    Reset  ( No Data will be Lost )
    Press and hold the Sleep/Wake button and the Home button at the same time for at least ten seconds, until the Apple logo appears. Release the Buttons.
    http://support.apple.com/kb/ht1430

Maybe you are looking for