As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

Java: Changing text on JButtons?

Steel AngelSteel Angel Registered User regular
edited July 2007 in Help / Advice Forum
I've been working on a little application using Swing, part of which involves a set number of JButtons that have only text on them and no icons. I want the text on the buttons to change their text under certain conditions (not simply when pressed mind you, more like change when certain files are loaded). Googling hasn't yielded anything useful to me (trying to use .setText() creates errors even with .repaint() and .revalidate()) so I'm wondering if this is even possible or if I should try to find some other component to use for this.

Big Dookie wrote: »
I found that tilting it doesn't work very well, and once I started jerking it, I got much better results.

Steam Profile
3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
Steel Angel on

Posts

  • Options
    BenboBenbo Registered User regular
    edited July 2007
    What's the error being thrown when you use setText("some text goes here"); ?
    You shouldn't need to be using repaint(), revalidate() or invalidate() just to set the text on a button.

    It's definitely possible

    Benbo on
  • Options
    SerpentSerpent Sometimes Vancouver, BC, sometimes Brisbane, QLDRegistered User regular
    edited July 2007
    If it isn't possible, just create multiple jbuttons and set them to be visible and invisible and whatnot as you desire.

    Serpent on
  • Options
    ValkunValkun Registered User regular
    edited July 2007
    When you ask for help debugging something, it's often helpful to post the code. There's no way for us to guess what misconceptions or errors you are making, but code is always honest. Failing to actually post code, at least include the error message.

    That said, if I had to take a guess, I'd say you're trying to use buttons that aren't initialized. Swing has a habit of accepting null parameters pretty well, but when you try to actually change the text it'll yell at you. You don't need to repaint or revalidate for changes to GUI components like that, it's only needed for when you're drawing stuff using the paint functions.

    Valkun on
  • Options
    Steel AngelSteel Angel Registered User regular
    edited July 2007
    Benbo wrote: »
    What's the error being thrown when you use setText("some text goes here"); ?
    You shouldn't need to be using repaint(), revalidate() or invalidate() just to set the text on a button.

    It's definitely possible

    This is the error message I'm getting during attempts to change the button's text. I'll also add that currently the buttons don't have any actions tied to clicking them since I'm trying to deal with this first before adding functionality.
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at novel.transition(novel.java:96)
    at novel$2.actionPerformed(novel.java:52)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    Steel Angel on
    Big Dookie wrote: »
    I found that tilting it doesn't work very well, and once I started jerking it, I got much better results.

    Steam Profile
    3DS: 3454-0268-5595 Battle.net: SteelAngel#1772
  • Options
    mindlarmindlar Registered User regular
    edited July 2007
    Something is null at line 96 of your novel class in the transition method. At least that's what the exception says. Now what is null there could be almost anything given that we haven't seen the code. The problem doesn't appear to be with the button.

    mindlar on
  • Options
    ValkunValkun Registered User regular
    edited July 2007
    Even money says he did the following:

    JButton newButton;

    newButton.setText("Blahblah");

    Which would cause you that null pointer exception, you have to call the constructor for every object you create besides for the primitive data types (int,boolean,double, etc).

    The proper form would be:

    JButton newButton = new JButton(whatever parameters you desire);

    newButton.setText("Blahblah");

    Also, this gets a little a more complicated when you work with arrays of objects:

    JButton[] buttonList = new JButton[n];

    This does not initialize each element in the array, you have to make a loop structure and call the constructor for each individual element:

    int i=0;
    while(i<buttonList.length)
    {
    buttonList=new JButton();
    i++;
    }

    Voila!

    Valkun on
Sign In or Register to comment.