Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

!! Actionevent has mind of own

Danie Van Eeden
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i hope this doesn't make the post unbearingly long, but my code is at the bottom.
Imt trying to ad a toolbar with buttons that execute external programs.
simple hey?
ok my action event on button for stock maintainance enters the event twic and i dont know why it would do that. itr just does.
really any help would be appreciated, i need to finish this thing as soon as possible, as it runs different parts of a much bigger project.
regards and thanks in advance
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Toolkit.*;
import java.util.*;

public class amenu extends JFrame implements ActionListener

privateJPanel p = new JPanel();
JToolBar myToolbar = new JToolBar();
JButton btnCustomers = new JButton("Customers");
JButton btnContracts = new JButton("Constracts");
JButton btnMaintenance = new JButton("Maintenance");
JButton btnStockMaintenance = new JButton("Stock Control");
JButton btnPOS = new JButton("Point of Sale");
Dimension d = new Dimension();

public amenu()

setTitle("Autopage Kuilsriver");
setSize(400, 70 );

d = Toolkit.getDefaultToolkit().getScreenSize();

this.setBounds(d.width - this.getSize().width,0,300,300);

setBackground( Color.gray );
p.setLayout(new BorderLayout());
// Create a new toolbar
p.add(myToolbar, BorderLayout.NORTH);


public void AddButtons()
// Add some buttons to the toolbar
btnCustomers = addToolbarButton( myToolbar, true, "Customers",
"Customers", "Customers" );
btnContracts = addToolbarButton( myToolbar, true, "Contracts",
"Contracts", "Contracts" );
btnMaintenance = addToolbarButton( myToolbar, true, "Maintenance",
"Maintenance", "Maintenance" );
btnStockMaintenance = addToolbarButton( myToolbar, true, "StockControl",
"StockControl", "StockControl" );
btnPOS = addToolbarButton( myToolbar, true, "POS",
"POS", "POS" );

public JButton addToolbarButton( JToolBar toolBar, boolean bUseImage, String sButtonText,
String sButton, String sToolHelp )
JButton b;
// Create a new button
if( bUseImage )
b = new JButton( new ImageIcon( sButton + ".gif" ) );
b = (JButton)toolBar.add( new JButton() );
// Add the button to the toolbar
toolBar.add( b );
// Add optional button text
if( sButtonText != null )
b.setText( sButtonText );
// Only a graphic, so make the button smaller
b.setMargin( new Insets( 0, 0, 0, 0 ) );
// Add optional tooltip help
if( sToolHelp != null )
b.setToolTipText( sToolHelp );
// Make sure this button sends a message when the user clicks it
b.setActionCommand( "Toolbar:" + sButton );
b.addActionListener( this );
return b;

public static void main(String[] args)
amenu toolbarMenu = new amenu();

public void smile()

public void RunExternalFile(String path)
try {
Runtime rt = Runtime.getRuntime();

Process prcs = rt.exec(path);
InputStreamReader isr = new InputStreamReader( prcs.getInputStream() );

//BufferedReader br = new BufferedReader(isr);
//String line;
//while ((line = br.readLine()) != null)
// System.out.println(line);
} catch(IOException ioe) {}


public void actionPerformed(ActionEvent e)
if (e.getSource() == btnStockMaintenance)
RunExternalFile("C:\\My Documents\\dve83\\J++\\Autopage\\Stock\\stock.exe");



Ernest Friedman-Hill
author and iconoclast
Posts: 24213
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because at the bottom of addToolbarButton(), you call b.addActionListener(this), and at the bottom of addButtons(), you call btnStockMaintenance.addActionListener(this), so the listener is added to this button twice, and therefore receives the event twice.
Danie Van Eeden
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, and thanks a lot.
i never noticed that and really appreciate the help.
again thanks
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!