Hi people, i am doing a project on 2D java that require me to create button and add code to the button,i has search for code online and has use the actionlistener method to act code to the button.When i run my program, it seem that the actionPerformed function has not been called up and so when i click on the button, nothing happen. I hope that you all can help me find out error in my coding.Thank You.
CODE : /* * IconGlassPanel.java * * Created on August 22, 2006, 3:28 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package Pas2DGUI; import CobraNet.*; import DbConnection.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Graphics; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Image; import java.awt.Panel; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.Vector; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JToolBar; /** * * @author staff */ public class IconGlassPanel extends JComponent implements MouseListener,ItemListener,ActionListener{ DAOFactory mySqlFactory = DAOFactory.getMySqlDAOFactory(); ZoneDAO zDao = mySqlFactory.getZoneDAO(); boolean iconSelected = false; Cursor cursor =null; Zone []zA = zDao.getAllZone(); int totZone = zA.length; ZoneIcons []iZone = new ZoneIcons[totZone]; // ButtonActionListener al = new ButtonActionListener(); private Image bgImg; ArrayList iconList = new ArrayList(); private ImageIcon icon; private JLabel bgImgLabel; private Image nImg=null; private Image img; private Graphics gGlass; private Cursor customC; private int selectedIcon = -1; /** Creates a new instance of BgmPanel */ public IconGlassPanel() { this.setOpaque(false); GridBagLayout gbl = new GridBagLayout(); BorderLayout bl = new BorderLayout(); GridBagConstraints c = new GridBagConstraints(); gGlass = this.getGraphics(); initZoneIcon(); //gbl. setLayout(bl); JToolBar tb = new JToolBar("Paging Station"); tb.setFloatable(true); tb.setRollover(true); tb.setBorderPainted(true); tb.setLocation(10,10); tb.setLayout(gbl); c.weightx = 0.0; JButton b1 = new JButton("B1"); tb.add(b1,c); b1.addActionListener(new ButtonActionListener()); JButton b2 = new JButton("B2"); tb.add(b2,c); b2.addActionListener(new ButtonActionListener()); JButton b3 = new JButton("B3"); tb.add(b3,c); b3.addActionListener(new ButtonActionListener()); c.gridwidth = GridBagConstraints.REMAINDER; JButton b4 = new JButton("B4"); tb.add(b4,c); b4.addActionListener(new ButtonActionListener()); c.gridwidth = GridBagConstraints.REMAINDER; JButton emergency = new JButton("Emergency"); tb.add(emergency,c); emergency.addActionListener(new ButtonActionListener()); JButton talk = new JButton("Talk!"); tb.add(talk,c); talk.addActionListener(new ButtonActionListener()); //setLayout(new GridBagLayout()); bgImg = Toolkit.getDefaultToolkit().getImage("C:\\TEMP\\Airport.jpg"); icon = new ImageIcon(bgImg,"ChangiAirport"); bgImgLabel = new JLabel(icon); System.out.println("Inside IconGlassPanel paint! totZone="+totZone); nImg = Toolkit.getDefaultToolkit().getImage("C:\\TEMP\\12.jpg"); add(tb,BorderLayout.SOUTH); img= Toolkit.getDefaultToolkit().getImage("C:\\TEMP\\Winter.jpg"); addMouseListener(this); customC = Toolkit.getDefaultToolkit().createCustomCursor(img,new Point(10,10),"ZonePtr"); this.setDoubleBuffered(true); } public void paint(Graphics g){ super.paint(g); /* System.out.println("Inside IconGlassPanel paint! totZone="+totZone); Image nImg = Toolkit.getDefaultToolkit().getImage("C:\\EngChuan\\Winter.jpg"); for (int i = 0;i<totZone;i++){ Point p = iZone[i].getScreenLoc(); this.getGraphics().drawImage(nImg,p.x-10,p.y-10,20,20,this); } */ } protected void paintComponent(Graphics g){ super.repaint(); // System.out.println("Inside paintcomponent! totZone="+totZone); for (int i = 0;i<totZone;i++){ if (i != selectedIcon){ Point p = iZone[i].getPoint(); if (iZone[i].isSelectedByPS()){ g.drawImage(img,p.x-10,p.y-10,50,50,this); }else { g.drawImage(nImg,p.x-10,p.y-10,20,20,this); g.drawString(iZone[i].getZoneName(),p.x-10,p.y-10 ); } } } } public void actionPerformed(ActionEvent e) { System.out.println("Inside Action Performed"); //get event's source if(!(e.getSource()instanceof JButton)) return; //for safety JButton b = (JButton)e.getSource(); String buttonName = b.getText();//check which button has been pushed if(buttonName == null) return; //for safety if(buttonName.equals("B1")) { //nImg = Toolkit.getDefaultToolkit().getImage("C:\\TEMP\\4.gif"); System.out.println("B1"); } else if(buttonName.equals("B2")){ System.out.println("B2"); } else if(buttonName.equals("B3")){ System.out.println("B3"); } else if(buttonName.equals("B4")){ System.out.println("B4"); } else if(buttonName.equals("Emergency")){ System.out.println("Emergency"); } else if(buttonName.equals("Talk!")){ System.out.println("Talk!"); } } public void mouseClicked(MouseEvent mouseEvent) { //getClickedIcon(mouseEvent.getPoint()); } public void mousePressed(MouseEvent mouseEvent) { int selIcon = getClickedIcon(mouseEvent.getPoint()); if ( selIcon != -1){ cursor = this.getCursor(); this.setCursor(customC); selectedIcon = selIcon; //iZone[selectedIcon].setSelectedByPS(true); } } public void mouseReleased(MouseEvent mouseEvent) { System.out.println("IconGlassPaneClick"); if (selectedIcon !=-1){ setCursor(cursor); iZone[selectedIcon].setPoint(mouseEvent.getPoint()); zDao.updZoneLoc((Zone)iZone[selectedIcon]); selectedIcon = -1; } } void addImage(Graphics g,Point point) { g.drawImage(img,(int)point.getX()-10,(int)point.getY()-10,20,20,null,this); } public void mouseEntered(MouseEvent mouseEvent) { } public void mouseExited(MouseEvent mouseEvent) { } private void initZoneIcon() { for (int i =0;i<totZone;i++){ iZone[i] = new ZoneIcons(); iZone[i].setZoneID(zA[i].getZoneID()); iZone[i].setZoneGroup(zA[i].getZoneGroup()); iZone[i].setPoint(zA[i].getPoint()); iZone[i].setZoneName(zA[i].getZoneName()); iZone[i].setSelected(false); iZone[i].setIconType(0); } } private int getClickedIcon(Point p){ for (int i=0;i<totZone;i++){ if (p.distance(iZone[i].getPoint())<15.00){ System.out.println("Zone "+iZone[i].getZoneName()+"Dist:"+p.distance(iZone[i].getPoint())); iZone[i].setSelected(true); return i; } } return -1; } /** Creates a new instance of IconGlassPanel */ public void itemStateChanged(ItemEvent itemEvent) { } } [Message sent by forum member 'lost' (lost)] http://forums.java.net/jive/thread.jspa?messageID=152037 =========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".