Results 1 to 7 of 7
  1. #1
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default ActionListener for JToolBar components

    Hi!

    I would like to add ActionListener to buttons stored in JToolBar. However, if I'm using the code bellow, then I could add only MouseListener ("addActionListener" doesn't work). So, is it possible to somehow add ActionListener? Thanks a lot!

    PHP Code:
            JToolBar toolBarUpdateDocs = new BrowserToolBar();
            toolBarUpdateDocs.setFloatable(false);   
    toolBarUpdateDocs.getComponent(i).addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            System.out.println("Ok");
                        }
                      });
    ...
    
     private class BrowserToolBar extends JToolBar {
            private BrowserToolBar() {
                    String[] imageFiles =
                      { "add.png", "del.png", "search.png" };
                    String[] toolbarLabels =
                      { "Add", "Delete", "Find" };
                    Insets margins = new Insets(0, 0, 0, 0);
                    for(int i=0; i<toolbarLabels.length; i++) {
                      ToolBarButton but = new ToolBarButton("src/icons/" + imageFiles[i]);
                      but.setToolTipText(toolbarLabels[i]);
                      but.setMargin(margins);
                      add(but);
                }
            }
        }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    I don't know what a ToolBarButton is but you should add an ActionListener to such a button; better make those buttons member variables (or keep them in another way) so you can add those listeners to them.

    kind regards,

    Jos

  3. #3
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    This is the ToolBarButton class:

    PHP Code:
        private class ToolBarButton extends JButton {
              private final Insets margins = new Insets(0, 0, 0, 0);
    
              private ToolBarButton(Icon icon) {
                super(icon);
                setMargin(margins);
                setVerticalTextPosition(BOTTOM);
                setHorizontalTextPosition(CENTER);
              }
    
              private ToolBarButton(String imageFile) {
                this(new ImageIcon(imageFile));
              }
    
              private ToolBarButton(String imageFile, String text) {
                this(new ImageIcon(imageFile));
                setText(text);
              }
        }
    As I understand you've proposed to do the following:
    PHP Code:
        private class BrowserToolBar extends JToolBar {
            private BrowserToolBar() {
                    String[] imageFiles =
                      { "add.png", "del.png", "search.png" };
                    String[] toolbarLabels =
                      { "Add", "Delete", "Find" };
                    Insets margins = new Insets(0, 0, 0, 0);
                    for(int i=0; i<toolbarLabels.length; i++) {
                      ToolBarButton but = new ToolBarButton("src/icons/" + imageFiles[i]);
                      but.setToolTipText(toolbarLabels[i]);
                      but.setMargin(margins);
    // ADD ACTION LISTENER TO "BUT"
                      add(but);
                }
            }
        }
    However, the problem is that I would like to add ActionListener outside of the class BrowserToolBar:
    PHP Code:
    JToolBar toolBarUpdateDocs = new BrowserToolBar();
            toolBarUpdateDocs.setFloatable(false); 
    // ADD ACTION LISTENER TO the button of "toolBarUpdateDocs"
    Is it possible?
    Last edited by LianaN; 09-18-2010 at 12:14 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LianaN View Post
    However, the problem is that I would like to add ActionListener outside of the class BrowserToolBar:
    Is it possible?
    Sure, your BrowserToolBar should be able to make those buttons available to the outside world so that outside world can add an ActionListener to the button(s).

    kind regards,

    Jos

  5. #5
    LianaN is offline Member
    Join Date
    Aug 2010
    Posts
    40
    Rep Power
    0

    Default

    Ok, thanks. I've solved this problem in the following way:

    PHP Code:
        private class BrowserToolBar extends JToolBar {
            private BrowserToolBar(String[] imageFiles, String[] toolbarLabels) {
                    Insets margins = new Insets(0, 0, 0, 0);
                    for(int i=0; i<toolbarLabels.length; i++) {
                      ImageIcon ic = new ImageIcon("src/icons/" + imageFiles[i]);
                      JButton but = new JButton(ic);
                      but.setToolTipText(toolbarLabels[i]);
                      but.setMargin(margins);
                      add(but);
                }
            }
    
            private JButton getButton(int i) {
                return (JButton) this.getComponent(i);
            }
        }
    
    ...
    
    toolBarUpdateUsers.getButton(0).addActionListener...
    Last edited by LianaN; 09-18-2010 at 11:50 AM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by LianaN View Post
    Ok, thanks. I've solved this problem in the following way: [ snip ]
    Well done; but now you have to know the index of the button you want to retrieve (it doesn't matter much here).

    kind regards,

    Jos

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

Similar Threads

  1. ActionListener+KeyListener
    By mandelbrot in forum AWT / Swing
    Replies: 5
    Last Post: 09-10-2010, 12:25 AM
  2. JToolbar: How to unselect first item
    By mjones in forum AWT / Swing
    Replies: 4
    Last Post: 02-19-2010, 02:56 AM
  3. How to access the ActionListener
    By jboy in forum New To Java
    Replies: 3
    Last Post: 10-15-2009, 06:04 PM
  4. JToolBar movable but not floatable
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-22-2008, 03:04 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •