Results 1 to 7 of 7
- 09-18-2010, 11:04 AM #1
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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); } } }
- 09-18-2010, 11:16 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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
- 09-18-2010, 11:23 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
This is the ToolBarButton class:
As I understand you've proposed to do the following: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); } }
However, the problem is that I would like to add ActionListener outside of the class BrowserToolBar: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); } } }
Is it possible?PHP Code:JToolBar toolBarUpdateDocs = new BrowserToolBar(); toolBarUpdateDocs.setFloatable(false); // ADD ACTION LISTENER TO the button of "toolBarUpdateDocs"Last edited by LianaN; 09-18-2010 at 12:14 PM.
- 09-18-2010, 11:35 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 09-18-2010, 11:48 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 40
- Rep Power
- 0
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.
- 09-18-2010, 12:15 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 09-18-2010, 01:37 PM #7
Similar Threads
-
ActionListener+KeyListener
By mandelbrot in forum AWT / SwingReplies: 5Last Post: 09-10-2010, 12:25 AM -
JToolbar: How to unselect first item
By mjones in forum AWT / SwingReplies: 4Last Post: 02-19-2010, 02:56 AM -
How to access the ActionListener
By jboy in forum New To JavaReplies: 3Last Post: 10-15-2009, 06:04 PM -
JToolBar movable but not floatable
By Fosters in forum AWT / SwingReplies: 0Last Post: 08-22-2008, 03:04 PM -
ActionListener interface
By tsantana in forum New To JavaReplies: 2Last Post: 03-30-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks