Problem AbstractAction - JButton.setRolloverIcon()

Hi,
Since i (and i don't understand why) couldn't really find anything in google, or in this forum search, i'm requesting you guys' help
Here's a brief description of the problem:
When just using a JButton (with some text on it), without any Actions assigned to it, it's no problem at all to assign a disabledIcon, rolloverIcon (setDisabledIcon, setRolloverIcon) to the button.
BUT
once you apply an action class to it (in my case i'm extending my action class from AbstractAction), things go wrong:
- Apparently, you CAN pass on an Icon (the default icon on the image) to the action class, and then calling the super(String command, Icon icon) constructor.
- But how to set a disabledIcon or rolloverIcon using this action class ? You can't pass this on the the super constructor like the default icon. I'm really not seeing what i 'm doing wrong here :-(
Thanks in advance for your replies and help,
Regards,
Sander
PS: i'm new to this forum, if i'm giving you guys to little information, please let me know.

but using action isn't the best idea. The benefit of using an Action is that it can be shared by multiple components. A typical GUI design would have a menu item and a toolbar button to invoke the same action. The coding is easy:
Action someAction = new SomeAction(...);
JMenuItem someItem = new JMenuItem( someAction );
JButton someButton = new JButton( someAction );
If you ever need to disable the Action you would do:
someAction.setEnable( false );
and the menu item and toolbar button would both be updated.

Similar Messages

  • Problem with JButtons

    Hello,
    I'm having a little bug : I created three "customized" jbuttons but the first does not look as the others (it seems to be cuted by a rectangle). It strange because they are the same.
    Image : http://zoubida-team.com/buttons.jpg
    Code :
         public WeightSelector(){
              button1=new PressedButton("images/buttonun.png", "images/buttonpre.png");
              button2=new PressedButton("images/buttonun.png", "images/buttonpre.png");
              button3=new PressedButton("images/buttonun.png", "images/buttonpre.png");
              setLayout(new GridBagLayout());
              GridBagConstraints c = new GridBagConstraints();
               c.gridx = GridBagConstraints.RELATIVE;
               c.gridy = 0;
              c.fill = GridBagConstraints.NONE;
              add(button1,c);
              add(button2,c);
              add(button3,c);
         }And the PressedButton Class
    public class PressedButton extends JButton{
         String normal, pressed;
         boolean action=false;
         public PressedButton(String normal1, String pressed1){
              this.setSize(new Dimension(26,11));
              setMargin( new Insets(0, 0, 0, 0) );
              setBorder( null );
              normal=normal1;
              pressed=pressed1;
              setIcon(new ImageIcon(normal));
              this.setRolloverEnabled(true);
              this.setRolloverIcon(new ImageIcon(pressed));
         }}

    I found the problem : the first button is selected by default and Swing draws a rectangle arround it (to mention it's selected). Is there a way to avoid or configure this comportement ?
    Thanks

  • Problem disabling JButton in ActionListener after setText of JLabel

    What i want is when i click View to see the label say "Viewing ..." and in the same time the btnView (View Button) to be disabled.
    I want the same with the other 2 buttons.
    My problem is in ActionListener of the button. It only half works.
    I cannot disable (setEnabled(false)) the button after it is pressed and showing the JLabel.
    The code i cannot run is the 3 buttons setEnabled (2 in comments).
    Any light?
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class BTester extends JFrame implements ActionListener { 
         JButton btnView, btnBook, btnSearch;
         BTester(){     //Constructor
              JPanel p = new JPanel();
         JButton btnView = new JButton( "View" );
         btnView.setActionCommand( "View" );
         btnView.addActionListener(this);
         JButton btnBook = new JButton( "Book" );
         btnBook.setActionCommand( "Book" );
         btnBook.addActionListener(this);
         JButton btnSearch = new JButton( "Search" );
         btnSearch.setActionCommand( "Search" );
         btnSearch.addActionListener(this);
         p.add(btnView);
         p.add(btnBook);
         p.add(btnSearch);
         getContentPane().add(show, "North"); //put text up
    getContentPane().add(p, "South"); //put panel of buttons down
    setSize(400,200);
    setVisible(true);
    setTitle("INITIAL BUTTON TESTER");
    show.setFont(new Font("Tahoma",Font.BOLD,20));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
         //actionPerformed implemented (overrided)
         public void actionPerformed( ActionEvent ae) {
    String actionName = ae.getActionCommand().trim();
    if( actionName.equals( "View" ) ) {
    show.setText("Viewing ...");
         btnView.setEnabled(false); // The line of problem
    //btnBook.setEnabled(true);
    //btnSearch.setEnabled(true);
    else if( actionName.equals( "Book" ) ) {
    show.setText("Booking ...");
    //btnView.setEnabled(true);
    //btnBook.setEnabled(false);
    //btnSearch.setEnabled(true);
    else if( actionName.equals( "Search" ) ) {
    show.setText("Searching ...");
    //btnView.setEnabled(true);
    //btnBook.setEnabled(true);
    //btnSearch.setEnabled(false);
    } //end actionPerformed
         private JLabel show = new JLabel( "Press a button ... " );
    public static void main(String[] args) { 
         new BTester();
    } // End BTester class

    1. Use code formatting when posting code.
    2. You have a Null Pointer Exception since in your constructor you create the JButtons and assign them to a local variable instead of the class member.
    Change this
    JButton btnView = new JButton( "View" );to:
    btnView = new JButton( "View" );

  • Problem with JButtons Text field not updating

    Im working on a program (which has its full code included below incase its part of the problem) which wants to change a Jbutton's name during a program. The way I'm trying to make it change is by having a string, "test", be called "before update". then have the jbuttons text equal test. then, in an actionlistener, it changes string test to equal "after update". This doesn't update the Jbuttons text.
    I don't get any errors when I press the button, but the buttons name is not updating. Whats causing the buttons name not to be updated?
    Thanks for help in advance.
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; 
    public class TicTac extends JFrame {
    public int Teamplaying = 0;
    public int CrossA, CrossB, CrossC, CrossD, CrossF, CrossG, CrossH, CrossI, CircleA, CircleB, CircleC, CircleD, CircleE, CircleF, CircleG, CircleH, CirclI, TLB, TMB, TRB, MLB, MMB, MRB, LLB, LMB, LRB = 0;
    String test = "Before Update";
        public TicTac() {
             JPanel TicTac = new JPanel();
             TicTac.setLayout(new GridLayout(3,4));
                  TicTac.add(new JButton(test));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  TicTac.add(new JLabel("a"));
                  setContentPane(TicTac);
             pack();
             setTitle("Add Numbers Together TicTac");
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             setLocationRelativeTo(null);
        class TopLeftBox implements ActionListener {
             public void actionPerformed(ActionEvent e) {
             String test = "After update";
             if (Teamplaying == 0) {
                  CrossA = CrossA + 1;
                  CrossD = CrossD + 1;
                  CrossG = CrossG + 1;
             else {
                  CircleA = CircleA + 1;
                  CircleB = CircleB + 1;
         public static void main(String[]args) {
        TicTac Toe = new TicTac();
        Toe.setVisible(true);
    }

    1) Strings are immutable meaning you can't change them.
    2) Even if you could, the two test strings are completely different variables.
    3) To change JButton text, you should call its setText method.
    4) For a JButton to perform an action on button press, it needs to have an actionlistener added to it via the addActionListener(...) method.
    5) Please read, study, and review the Sun Swing tutorials. You will benefit greatly from having a solid foundation in Swing basics before you try coding in Swing.
    Good luck.
    Edit: a small example code (SSCCE, if you will):
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Frame extends JFrame
        public Frame()
            JPanel panel = new JPanel();
            JButton button = new JButton("Before Update");
            button.addActionListener(new ButtonListener()); // add actionlistener here
            panel.add(button);
            setContentPane(panel);
            pack();
            setTitle("Frame");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
        private class ButtonListener implements ActionListener
            public void actionPerformed(ActionEvent e)
                // get the button that called this action
                JButton button = (JButton)e.getSource();
                // update the button's text
                button.setText("After update");
        public static void main(String[] args)
            SwingUtilities.invokeLater(new Runnable()
                @Override
                public void run()
                    new Frame().setVisible(true);               
    }Edited by: Encephalopathic on Apr 28, 2008 9:26 PM

  • AbstractAction -- JButton

    class QueryAction extends AbstractAction
    // Constructor
    QueryAction(String name)
    super(name);
    String iconFileName = "Images/" + name + ".gif";
    if(new File(iconFileName).exists())
    putValue(SMALL_ICON, new ImageIcon(iconFileName));
    public void aFunc(){
    QueryAction anAction = new QueryAction("Compile Query");
    JButton button = new JButton(anAction);
    toolBar.add(button);
    in other function, I'm trying to disable the button with that particular description string (i.e. "Compile Query"):
    Component[] comp = toolBar.getComponents();
    for (int i=0; i<comp.length; i++){
    JButton aButton = (JButton)comp;
    Action act = aButton.getAction();
    if(act.getValue(Action.NAME).equals("Compile Query"));
    aButton.setEnable(false);
    break;
    But I got NullPointerException for the statement :
    act.getValue(Action.NAME)
    Anybody has an idea how to solve this...?? thx

    Seems to me that your toolBar contains a button which has no action assigned to it.
    Try this: keep a reference to your QueryAction in your class and disable it instead of the button (this feature is one of the reason to use actions afterall):
    public class XYPanel {
      private QueryAction queryAction = new QueryAction();
      private class QueryAction {
        public QueryAction() {
          super("Compile Query");
        // how about using a class resource instead of a file
        // - better for using jars in your app
        putValue(SMALL_ICON, new ImageIcon(getClass().getResource("query.gif")));  
      public void aFunc() {
        toolBar.add(queryAction);
      public void anotherFunc()
        queryAction.setEnabled(false);

  • Problem in JButton while changing look and feel to CDE/Motif

    In my application i'm using JButtons placed in JToolbar. I've setIcons for the JButtons. The top of the icons i.e the JButtons get hided while i changing the look & feel to CDE/Motif. But again if i change the look & feel to Metal, no problem is there. Expecting a favourable response from someone well versed / faced such problems earlier problems.
    Cheers
    sunil s

    Sounds suspiciously like you are setting absolute sizes for your buttons or elsewhere in application. Can you post code snippets showing how you layout your application.
    Cheers
    DB

  • An interesting problem with jbutton

    when you jButton.setbackground()
    jButton is not really change it's background,
    not the same as button,
    I can't find a way to change jbutton 's background color then,,
    who know how? :)

    no Sum-shusSue ,
    this away;-
    b1.setBackground(new java.awt.Color(0,0,255)); //blue
    or thataway;-
    b2.setBackground(Color.blue); // or blue
    when b1/b2 is button ,there's no problem
    but you can use in jButton,it don't work
    jButton.setOpaque before setbackground() then is ok

  • A problem about Jbutton

    hi, everyone, i got a probelm. i ve created a JButton Pad[5][5], and i want to whenever i press the Jubtton i created , different image will be displayed, i have declare a array to store my image, and i have set image[0]=("my image name", image[1]=("my image name"), i want to the result is that i press one jbutton, what i have put into my image arrary will be displayed in turn , for that i can press and get the image i prefered, what i have done is
    public void actionPerformed(ActionEvent e)
         for(int i=0;i<5;i++)
         for(int j=0;j<5;j++)
         if(e.getSource()==Pad[i][j])
         Pad[i][j].setIcon(........) i do not know how to write code here, please help , i m fresher, so if some probelms are silly, please point it to me, very thanks

    what i want is that after i declare Jbutton Pad[5][5], i want to press any Jbutton on my Pad[5][5], the image will be displayed in turn. image i have got already, and i think setIcon should work, but it just can display one image on my button ,not in turn,

  • Problem with JButton and NVDA

    I am using the following code and would like to hear NVDA and JAWS announce my button when I roll over it. The JFrame name and title bar components are announced, but that is all. Can someone help?
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    //import javax.accessibility.*;
    public class JFrameExample
    extends JPanel {
    public JFrameExample() {
    JButton aButton = new JButton("Button");
    aButton.getAccessibleContext().setAccessibleName("Button");
    String desc = "This is a simple button";
    aButton.getAccessibleContext().setAccessibleDescription(desc);
    add(aButton);
    public static void main(String[] args) {
    JFrame f = new JFrame("Accessible Button");
    f.getContentPane().add(new JFrameExample(), BorderLayout.CENTER);
    f.pack();
    f.setVisible(true);
    Environment - Windows Vista 64
    JDK 1.6.0_21 32 bit , jre6 32 bit - Java Access Bridge dlls installed in both places
    NetBeans 6.9.1 , Eclipse Helios 32 bit
    NVDA 2010.1, JAWS 11

    hmmm my code is very big, but make something like that
    create a instance of launcher
    and create the graphicals components
    i call the method,
    private JButton m_logoutButton;
    constructor.....{
    m_logoutButton = new JButton();
    setButtonEnabled(true);
    setButtonEnabled(boolean enabled){
    m_logoutButton.setEnabled(enabled);
    public void logout(){
    setButtonEnabled(false);

  • Problems resizing JButtons

    Hi. I have a GUI with a JPanel to which I have added 43 JButtons. I use a for loop to add the buttons but I cannot set the size of individual buttons. I want two of the buttons to be bigger than the rest. Can anyone please help me

    Hi. I have a GUI with a JPanel to which I have added
    43 JButtons. I use a for loop to add the buttons but I
    cannot set the size of individual buttons. I want two
    of the buttons to be bigger than the rest. Can anyone
    please help meAs some else has mentioned, much depends on your
    layout manager. You be able to do the job with
    GridBagLayout, but if that's too much like hard
    work, you can always turn the panel's layout manager off
    and manually place & size each of your buttons.
    setLayout( null );
    and for each button:
    button.setBounds( x, y, width, height );
    If you do this, the buttons won't move or expand
    when the panel is resized. I reckon that's not
    a bad thing for buttons anyway.

  • Problem painting JButton

    Hi all, first post here so I hope you can help.
    I have a couple of applications in which the JButtons are painted using an inner class. e.g
    JButton butt= new JButton() {
            public void paintComponent(Graphics g){
                    ImageIcon map = new ImageIcon( "some image" );
                   g.drawImage(map.getImage(), 0, 0, 50, 50, null);
                   super.paintComponent(g);
    };However, since I updated to version 1.5 this doesn't seem to work anymore, e.g the image is not displayed. Any JPanels are still painted fine - just not the JButtons.
    Any Ideas will be greatly appreciated.
    Cheers

    Hi all, first post here so I hope you can help.
    I have a couple of applications in which the JButtons
    are painted using an inner class. e.g
    JButton butt= new JButton() {
    public void paintComponent(Graphics g){
    ImageIcon map = new ImageIcon( "some
    ImageIcon( "some image" );
    g.drawImage(map.getImage(), 0, 0, 50,
    ge(), 0, 0, 50, 50, null);
    super.paintComponent(g);
    };However, since I updated to version 1.5 this doesn't
    seem to work anymore, e.g the image is not displayed.
    Any JPanels are still painted fine - just not the
    JButtons.
    Any Ideas will be greatly appreciated.
    CheersIt seems like calling super.paintComponent after your code would paint over whatever you did. Maybe you should call the super method first.

  • Problem with co-existence JFormattedTextField and JButton

    When I use three JFormattedTextField (exactly - my own classes extending JFormattedTextField) and several JTextField I get a problem - the JButton (that changes it's enabling in dependence of field's content) work only after third click. When I use JTextField instead of JFormattedTextField, all works fine.

    my own classes extending JFormattedTextFieldWell, first you test it using the standard classes to see if it works. If it does then you know the problem is with your custom code.
    If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)", that demonstrates the incorrect behaviour.
    http://homepage1.nifty.com/algafield/sscce.html
    Don't forget to use the "Code Formatting Tags", so the posted code retains its original formatting.
    http://forum.java.sun.com/help.jspa?sec=formatting

  • Accessibility of JComboBox problem

    Hi everyone!
    I've got two applications. One of them make changes in the components of the other one through the Accessibility API. (Both are running on the same VM)
    I make changes without problem in JButton, JTextComponent...
    But I've got a JComboBox with 4 options, and the "reader/writer" application has to select the third option, and I can't get a way to do this with Accessibility.
    I can see the JComboBox and its internal structure (a Viewport, a BasicPopUp, a JPanel, ScrollButtons, a ComboList, and the four options).
    The only available action is in the JComboBox and is the togglePopUp that toggles it without problem.
    But I can't get a way to select one of the options of the toggled up pop up.
    Does anybody has an example code for doing this?
    Thank you a lot!
    AC

    Works fine for me.
    If you need further help then you will need to provide [url http://www.physci.org/codes/sscce.jsp]Simple Demo Code that demonstrates the problem.

  • JButtons not triggering

    I'm having a problem with JButtons failing to trigger when the mouse is moved while the button is still being pressed.
    I'm aware that this is designed behavior if you press down on the mouse button, move the mouse outside the JButton boundaries, and then let go of the mouse button; but my JButtons aren't triggering ActionPerformed when the mouse remains within the button boundaries when it's moved.
    In my application, I attach buttons to actions in one uniform fashion, but it seems to trigger ActionPerformed for some JButtons and not for others under these circumstances. (on a direct click without moving the mouse, ActionPerformed always gets called)
    Anybody have any ideas? I also have the same problem with checkboxes -> the box gets checked, but ActionPerformed doesn't get called.
    thanks,
    Stephanie

    I'm having a problem with JButtons failing to trigger when the mouse is moved while the button is still being pressed.
    I'm aware that this is designed behavior if you press down on the mouse button, move the mouse outside the JButton boundaries, and then let go of the mouse button; but my JButtons aren't triggering ActionPerformed when the mouse remains within the button boundaries when it's moved.
    In my application, I attach buttons to actions in one uniform fashion, but it seems to trigger ActionPerformed for some JButtons and not for others under these circumstances. (on a direct click without moving the mouse, ActionPerformed always gets called)
    Anybody have any ideas? I also have the same problem with checkboxes -> the box gets checked, but ActionPerformed doesn't get called.
    thanks,
    Stephanie

  • JButton Not Working Always

    I am having a problem with JButtons and JCheckBoxes in java. I am finding myself having to click the JButtons more than once in some situations to get to the next stage of my GUI. With the JCheckBoxes it sometimes takes a couple of clicks to change the state (unticked -> ticked). This seems to be occurring more than often. Can anyone help?

    If there are clicking problems, it's coming from the computer you're on, not java. There isn't much you can do about that

Maybe you are looking for