What's wrong with my Button?

Hello everyone,
I been trying to get my Button working, but I don't understand what's wrong. I've tried a bunch of stuff, each with failure. Can any of you guys help me out here? I know that it's a little bit of a mess, but for some reason, it gives me a NullPointerException when I execute it. If I comment out 4 lines of code, about the getContentPane, it works without making a button. Problem is, I want to keep the Button in there. What should I do?
Thanks
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(startButton);
if (frame.getContentPane() instanceof JComponent)
     ((JComponent)frame.getContentPane()).setOpaque(false);
}

here is your original code, stripped of all the non-button parts
you will see that it does not generate a NPE
you may also note it is easy to read (code tags used)
suggestion - start again
start with a small section of code, ***compile/run/test/fix/compile/run/test
when all OK, add another section of code (repeat ***)
etc
etc
import java.awt.*;
import javax.swing.*;
class Graphic2
  public static void main(String[] args)
    new Graphic2().run();
  private JButton startButton;
  public void run()
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//added
    frame.setLocation(400,300);//added
    startButton = createButton("Start.gif");
    startButton.setLocation(460, 500);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(startButton);
    frame.pack();//added
    frame.setVisible(true);//added
  public JButton createButton(String name)
    String imagePath = name;
    ImageIcon iconRollover = new ImageIcon(imagePath);
    int w = iconRollover.getIconWidth();
    int h = iconRollover.getIconHeight();
    Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
    // create the button
    JButton button = new JButton("btn");//added "btn"
    //button.addActionListener(this);
    button.setIgnoreRepaint(true);
    button.setFocusable(false);
    button.setBorder(null);
    button.setContentAreaFilled(false);
    button.setCursor(cursor);
    button.setIcon(iconRollover);
    button.setRolloverIcon(iconRollover);
    button.setPressedIcon(iconRollover);
    return button;
}

Similar Messages

  • What's wrong with this button code

    Dear All,
    I am trying to set up a button handler for many buttons
    rather just one. The
    intention of this code is to say if I press Load1 to Load40
    that button and
    only that button's alpha should change to 50%. This code
    makes them all 50%
    alpha when the frame loads rather than on the onPress event.
    Can someone please help me out with this one?
    i = 0;
    while (i<40) {
    this["Load"+i].onPress = btnEvaluate(this["Load"+i]);
    ++i;
    function btnEvaluate(target_btn:Button) {
    target_btn._alpha = 50;
    Thanks again.
    Alastair MacFarlane

    kglad,
    Thanks again. What would this group do without your help.
    Alastair
    "kglad" <[email protected]> wrote in message
    news:gh0sq0$cbv$[email protected]..
    > :
    >
    >
    >
    > for (var i = 0;i<40;i++) {
    > this["Load"+i].onPress =function(){
    > this._alpha=50;
    > }
    > }

  • Can't gotoAndStop ...what's wrong with this button code?

    Hi,
    I'm totally stumped as to why I can't navigate to another frame.
    I have put 2 days into this simple problem now and I'm ready to give up and go back to AS2.
    Please help!
    I'm using CS4; publish settings AS3, 10.0
    I have a button on frame 2 that should take me to frame 3 when clicked.
    (So far I've tried a hundred different ways but I don't get to frame 3...)
    Different things I tried...
    > Creating the button dynamically from the library...
    > Inserting it statically from the library on frame 2...
    > referencing the mainTimeline through a variable...
    > wrapping the gotoAndStop action in another function...(if placed directly on the timeLine the movie correctly goes straight to frame 3)
    > naming frame 3...
    > using 'nextFrame' instead of 'gotoAndStop'...
    etc..
    In most cases I don't get an error in the output panel and no compiler error either; it just doesn't work!!!
    (Just stays on the same frame after I click the button)
    Here's the code I think should work; but doesn't:
    function moveTo3 (event:MouseEvent):void {
        trace("button_clicked"); // always traces correctly; but movie does not go to frame 3 !!!
        gotoAndStop(3);
    f_btn.addEventListener(MouseEvent.CLICK, moveTo3);
    WHAT AM I MISSING HERE?
    Thanks in advance ...

    Hi Ned,
    I have to run out and don't have the time to deal with it right now but you're RIGHT.
    I do have an 'onEnterFrame' listener on frame 1 !
    It's doing some animation with a symbol and I don't think I removed the listener itself.
    I just set the object to null when it reached [alpha:0].
    THANKS!
    I'll check it out as soon as I can but I really think you got me looking in the right direction now.
    Thanks again,
    Connor
    (I'll post an update when I get back to it)

  • What's wrong with my buttons, any one can figure out?

    I am creating a SleeperFace with Applet, there was a problem that my buttons does not appear normally, just when I point the mouse on, then the two buttons will be appeared.
    Here are my codes:
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class SleepyFace extends Applet implements ActionListener
    int x=0;
    public void init()
    JButton bAwake=new JButton("Awake");
    bAwake.addActionListener(this);
    JButton bAsleep=new JButton("Asleep");
    bAsleep.addActionListener(this);
    add(bAwake);
    add(bAsleep);
    public void actionPerformed(ActionEvent e)
    if(e.getActionCommand()=="Awake")
    x=1;
    repaint();
    else if (e.getActionCommand()=="Asleep")
    x=0;
    repaint();
    public void paint(Graphics g)
    setBackground(Color.blue);
    g.setColor(Color.yellow);
    g.fillOval(50,60,400,400);
    g.setColor(Color.black);
    if (x==1)
    g.fillOval(130,160,50,100);
    g.fillOval(312,160,50,100);
    g.fillOval(222,250,50,50);
    g.fillArc(150,270,200,120,-360,-180);
    g.setColor(Color.white);
    g.setFont(new Font("SansSerif", Font.BOLD, 24));
    g.drawString("Let's make some noise!", 120,520);
    else
    g.fillRect(100,240,100,4);
    g.fillRect(300,240,100,4);
    g.fillOval(222,250,50,50);
    g.fillRect(150,350,200,4);
    g.setColor(Color.white);
    g.setFont(new Font("SansSerif", Font.BOLD, 24));
    g.drawString("I'm sleeping, please do not disturb!", 50,520);
    }

    FlowLayout is generally not a good top ContainerLayout.
    I am not debating that. I agree BorderLayout is a
    better top level LayoutManager (which is why I made
    the earlier assumption that Applets also use a
    BorderLayout).
    I realize that you aren't - in fact, I did not take you to be debatinganything. I added that piece of information merely to explain why I changed the Layout Manager - in order to add the JButtons where I wanted them to be - which is admittedly not where the OP might want them.
    I am questioning how using a BorderLayout causes the
    children to be painted, even though the paint()
    method does not invoke super.paint().
    I have not intentionally avoided calling super.paint. As I said, I do not know the real reason. But this has worked before, and I believe it has to do with adding the two JButton Components to another Panel; because if you simply add them BorderLayout.NORTH and BorderLayout.SOUTH it won't help.
    See point 5 from the following link.
    http://java.sun.com/products/jfc/tsc/articles/painting
    /index.html#awt_summary
    I see it

  • My iPhone 4S won't turn on it's been off for two days now. I tried charging it but it makes a noise every 8 seconds,holding the buttons down and also plugging it to a computer. I need help on what to do or if anybody can tell me what's wrong with my phone

    My iPhone 4S won't turn on it's been off for two days now. I tried charging it but it makes a noise every 8 seconds,holding the buttons down and also plugging it to a computer. I need help on what to do or if anybody can tell me what's wrong with my phone

    Yes ive tried a different charger and it also nothing shows when i plug it in just makes a noise

  • I did`t saw any content of the menu button, what is wrong with my system Indesign

    I did`t saw any content of the menu button, what is wrong with my system Indesign

    more information will help.
    Try to select a frame and on the bottom of Buttons and Forms dailog box click on Covert to Button

  • When i touch the button of the phone while charging it, it sparkles.  What is wrong with it?

    When i touch the button of the phone while charging it, it sparkles.
    What is wrong with it?

    For your purposes?  Probably nothing.
    If you want Apple to service the device under warranty, it has to be done at an Apple Authorized Service Provider.
    Look here: http://www.apple.com/ae/contact/
    This is how you contact Apple to find out what to do.

  • What is wrong with my Macbook Pro? (distorted screen, beeping, slow)

    I have a late 2012 Macbook Pro (13-inch) that I got from Best Buy. It worked perfectly fine for a year, but then went crazy.
    First, it started shutting down here and there, and icons bounced a lot in the dock. Then, one day, I googled something and it froze, then flashed a strip of multicolored barcode-like lines. So, I shut it down and it overheated then began to beep 3 times and flash a light.
    I brought it in and they just wiped the hard drive. Then, it happened again while I was watching a video only a short time later. I was going to re-seat the RAM myself but the inside of the computer looked all messed up. I took it in and they gave me a new LCD but it has a dead pixel now. It worked fine for 1 month after that.
    Then, I was using the laptop on my bed and it shut off randomly a couple of times while I was on Facebook and started beeping. I rebooted it and tried to fix it but it happened again the next day. I brought it back to Best Buy and they said it was hot and sold me a $20 cooling pad.
    That worked for a few days, then I was watching TV online and it happened again. I brought it back to Best Buy and they replaced the RAM. Now, I was watching something online and it happened yet again. I was able to reboot and it worked okay now for a couple of hours but it did freeze again and I had to shut it down.
    Everytime I go to Best Buy, they tell me "If we have this issue 1 more time, we will replace your computer." But, then, they say the same thing every time and that it's their policy. I have paid for Office now and only have 1 download left. Also, this is ridiculous because I'm a student and I've had my Mac gone for 2 weeks at a time at least 3 or 4 times. When this happens, I get stuck using my iPad and can't use flash so I've spent tons of money of apps for flash and other things to make my iPad functional.
    Can somebody just tell me what's wrong with it? It "passes the hardware test" every time and the Geek Squad keeps asking me stupid questions like "Did you check your internet connection and reset your modem?" When I asked them the first time to do a PRAM test, they didn't know what that was and asked me to go on iTunes or clear my cache or something like that.
    I have had a bad experience with them altogether. I got my Macbook for $500 because I had a PC before that I paid $700 for and it eventually broke. They were unable to repair that one because the hard drive had failed, although they had previously been convinced that I needed to uninstall my Norton to fix that problem.
    Sorry if I'm ranting, but my Macbook is like a child to me (not to sound weird) and I'm sick of this problem. It's working fine at the moment but I know this will happen again randomly like always.

    If you're able to boot, launch the Console application by entering the first few letters of its name into a Spotlight search, and select the most recent panic log under "System Diagnostic Reports." Post the contents -- the text, please, not a screenshot. For privacy’s sake, I suggest you edit out the “Anonymous UUID” and "Kernel UUID," two long strings of letters, numbers, and dashes in the header and body of the report. Please don't post "shutdownStall" or "hang" reports.
    If you can't boot in the usual way, try a safe boot. The instructions provided by Apple are as follows:
    Be sure your Mac is shut down.
    Press the power button.
    Immediately after you hear the startup tone, hold the Shift key. The Shift key should be held as soon as possible after the startup tone, but not before the tone.
    Release the Shift key when you see the gray Apple icon and the progress indicator (looks like a spinning gear).
    During startup, you will see "Safe Boot" on the login screen, which appears even if you normally log in automatically. You must know your login password in order to log in. If you’ve forgotten the password, you will need to reset it before you begin.
    Safe mode is slower than normal, and some things won’t work at all, including your login items.

  • What is wrong with my IPhone? If you take a video you can't hear the audio but if I do a voice memo you can

    I don't know what's wrong with my iPhone, I can listen to music but if I try to take a video you can't hear my voice, if I take a voice memo you can hear my voice.

    try exiting the app and double clicking the home button. the multitasking apps will show up. delete all of them by holding them down and pushing the red circle (dont worry that doesnt remove them from the phone, just completely closes the app and makes your device run faster) then power off your phone by holding down thw lock button and sliding the red arrow when it comes up. wait about 5 seconds after its completely off and turn it back on. then try using the mic. hope this helps

  • What is wrong with my iphone4? and how can i turn it back on?

    About a week ago, i was using my iphone that I've had about 6 months, no problems. It was working perfectly fine and then out of nowhere it shut itself off. I plugged it in, nothing happened. I pressed the home and lock button, and held them in, and still nothing happened. The next day I decided to plug it in again, and it told me to leave it plugged in, like it was dead or something (the red battery symbol popped up, and said it was charging) and about 10 minutes later the iphone came back on. The only problem was that from here on out my home button does NOT work, at all.
    The phone was working................ until about 3 days later the same thing happened to it again, except the next day it did not come back on the same way. My mom took it to the AT&T store (living in west virginia, there aren't any Apple stores near by). She came home with it working again, but I have no clue how they got it back on.
    A few days after the second time it shut off, it happened again. We took it to AT&T again, and this time they couldn't get it back on, but they gave us some tips. We plugged it into itunes, and restored it. I was over the amount of space. I had no storage left!! So  I went through and deleted all my music, and about 500 photos (having around 1,200) . I now have about 700 photos, 20 songs, and roughly 15 apps. ONE day after I did all of this, my phone shuts off for the fourth time !!!! (It is off right now) Seeing how I have 3 GB of space left on my phone, I would think it's not a storage issue.
    I've plugged it into the wall charger, nothing! I have tried to reset it holding the home button (which still does not work) and lock button, still nothing. I have now plugged it into itunes. It says the device is found, but not identified please restore the device. So I restore it, but that doesn't help, Itunes still cant "identify" the device and it is still not on.
    I have checked for water damage, and there is not any! I just want to know what is wrong with it, and how to fix it. OR maybe if I just need to get a whole new phone, because mine is under warranty. The problem is AT&T won't give me a new one and there isn't an apple store near me! Can someone PLEASE help?

    I think you might get a new phone instead of repairing it as it takes time and you said the repairing center is far from your living place

  • What is Wrong with My html-el:reset Tag?

    In my JSP, I used <html-el:form ...>, <htim-el:checkbox ...>, <html-el:hidden ... >, <html-el:submit ... > without any problem.
    But, I got Error 500:(class: org/apache/strutsel?taglib/html/ELResetTag, method: release signature: ?V) Illegal use of nonvirtual method call
    when I used:
    <html-el:reset accesskey="C">Clear</html-el:reset>And the reset button does not get displayed when I
    <html-el:reset>Clear</html-el:reset>Would anybody point out what is wrong with the way I used the reset tag? Thank you.

    In my JSP, I used <html-el:form ...>, <htim-el:checkbox ...>, <html-el:hidden ... >, <html-el:submit ... > without any problem.
    But, I got Error 500:(class: org/apache/strutsel?taglib/html/ELResetTag, method: release signature: ?V) Illegal use of nonvirtual method call
    when I used:
    <html-el:reset accesskey="C">Clear</html-el:reset>And the reset button does not get displayed when I
    <html-el:reset>Clear</html-el:reset>Would anybody point out what is wrong with the way I used the reset tag? Thank you.

  • What is wrong with my Macbook?

    I dropped my macbook (2007 model) and the screen is so cracked that I can not see anything. I got a mini-dvi - vga cord and plugged it into a monitor, but when i turn it on, it just goes to the mac background, and will not show the login screen. What is wrong with it?

    it's putting the monitor as second display, so the login screen (i'm guessing) is only displaying on the first—main—screen, the the one that is cracked.
    restart the macbook and don't touch anything. when the background appears on the secondary monitor, you know the login screen must be up on the first monitor. so type in your username, hit TAB once, then your password.
    you should be able to log in now. then follow these instructions:
    (bare in mind i'm running 10.6.8)
    1) press ALT+F2 to open up displays in system preferences.
    2) you should be able to see a dialog open up in the secondary monitor
    3) if this has a "Gather Windows" button, press it
    4) if step 3) works, you should be able to switch around the main displays, or turn on mirror mode, or whatever

  • What is wrong with my log in display screen?

    Hey There guys,
    This problem started occurring right after i updated to Yosemite, which was quite a while back. Every time I turn on my computer and I get to the log in screen where I put my password, half the screen is normal and the other half is black with green lines running through it(this was the latest) other times it is white rectangles with little black ones running through them. Im not sure what is wrong with it, I never dropped my mac or anything.
    I have attached an image.

    Try this  - Reset 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 - let go of the buttons. (This is equivalent to rebooting your computer.) No data/files will be erased. http://support.apple.com/kb/ht1430http://support.apple.com/kb/ht1430
    Not normal. Take it to an Apple Store for evaluation.
    Make a Genius Bar Reservation
    http://www.apple.com/retail/geniusbar/http://www.apple.com/retail/geniusbar/
     Cheers, Tom

  • What is wrong with my battery?

    What is wrong with it? I have nothing open, other than this one app. It is only a month old, and Mountain Lion seems to have made it this way?
    http://i49.tinypic.com/28hkard.png
    Only 3:20 of battery life?

    1. If the computer is on, turn it off.
    2. Reset the power manager by simultaneously pressing and then releasing Shift-Control-Option-Power on the keyboard. Do not press the fn (Function) key while using this combination of keystrokes.
    3. Wait 5 seconds.
    4. Press the Power button to restart the computer.
    I followed these directions, but the computer turned on when I initially pressed the power button while holding Option, Shift, and Control, so I don't know if it worked properly or not. I'm currently letting my battery drain, though.

  • What is wrong with my iBook?

    I have a G4 1.xx ghz iBook. It's about 14 months old, and it just stopped working a few days ago.
    It no longer powers up. I can hear what sounds like the fan when I push the power button, but nothing happens. The display worked just fine right before this happened, but now the display doesn't even turn on when I click on the power. It doesn't even make the startup sound.
    I have to click on shift control alt and power to shut it down. It once started up normally since this happened, but it turned itself off after about 10 minutes.
    What is wrong with my iBook? I've been using Windows again in the past week or so at home, and I really hate it.
    Please provide me some suggestions...
    Is this the infamous logic board problem? I thought those would only occur on the G3.
    iBook G4   Mac OS X (10.4.7)  
    iBook G4   Mac OS X (10.4.7)  

    Now it's working just fine. I am using my iBook now.
    I thought that the Apple Care was only good for 12 months. I am on my 14th or 15th month of owning it.
    iBook G4   Mac OS X (10.4.8)  

Maybe you are looking for

  • HR-ABAP(Payroll Management)

    Hi All, I am new to HR-ABAP. I wanted to know more about HR-ABAP(Payroll Management). Please send me the links or documents if u have collected. Regards, Mihira

  • Trouble editing text within form in pdf

    We have created pdf fillable forms and was able to add text fields that come with red borders (intentional) inside and outside the forms. However, when trying to edit the texts inside the form, we are not able to click on it to select and delete/move

  • Everything crashes after FCS Remover

    Hi Everyone, So I think FCS Remover has buggered up my Final Cut Studio... I wanted to do a fresh install of FCS 2.0 so that I could put all the extra content on an external HD (i work on my G4 powerbook so space is important). I used FCS Remover to

  • How to redownload photoshop cc

    My hard drive crashed and i have to redo all of my programs. How do I download photoshop cc again? i already have a subcription.

  • Slideshow in iWeb won't work on my server - not .Mac,

    My Photos page, created with a template, is giving me a 404 msg. (SlideShow.html not found on this server) whenever I go to click on the SlideShow icon. My site is http://www.theweetman.net. However, when I run the site from my desktop files, the sli