Results 1 to 6 of 6
  1. #1
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default Problem with ActionListener?

    Hi,

    i am working on my school project with mainly deals with Openstreetmap using java swing,the problem which i encountered when running the below code is that,when i try to run the below class INDEPENDENTLY(i.e in netbeans right click and run the file),the code works like a charm,BUT,when i try to call the same code/class from an actionPerformed of a some button,a blank frame appears.here's the code snippet.

    Java Code:
    import javax.swing.JFrame;
    import org.jdesktop.swingx.JXMapKit;
    import org.jdesktop.swingx.JXMapKit.DefaultProviders;
    import org.jdesktop.swingx.mapviewer.GeoPosition;
    import org.jdesktop.swingx.mapviewer.util.GeoUtil;
    import org.jdesktop.swingx.mapviewer.WaypointPainter;
    import org.jdesktop.swingx.mapviewer.Waypoint;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.util.HashSet;
    import java.util.Set;
    import javax.swing.JPanel;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import java.awt.Dimension;
    import javax.swing.JTextField;
    import javax.swing.BoxLayout;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.TitledBorder;
    import javax.swing.BorderFactory;
    import javax.swing.border.Border;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.sql.*;
    
    
    public class Proximity extends JFrame
    {
    
    
    
        static double lattitude;
        static double longitude;
        static double dontchangeone ;
        static double dontchangetwo;
        static double deslat;
        static double deslong;
        int count=0;
        int R=6371;
        double Distance,adistance,bdistance,cdistance,ddistance;
        double A,B,C,D,E,F,G,H,n=0,bil=0,slno;
        double xx=0,y=0,yy=0;
        String speedgen,billgen;
    
        JLabel speed=new JLabel("Average Speed");
        JTextField textspeed=new JTextField(15);
        JLabel BILL=new JLabel("Bill");
        JTextField bill=new JTextField(15);
        final JXMapKit mapView = new JXMapKit();
         WaypointPainter painter = new WaypointPainter();
    
         static JButton btup;
           
          static Proximity o;
    
        
           public void map(String x,String y,String z)
        {
    
            btup=new JButton("Begin Trace");
    
            slno=1;  //needs to be removed
            count=0;
            GeoPosition gpos;
            GeoPosition ppos;
            GeoPosition vehicle;
           
            mapView.setDefaultProvider(DefaultProviders.OpenStreetMaps);
            mapView.setDataProviderCreditShown(true);
            mapView.setVisible(true);
           
            
           getContentPane().setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
            add(mapView,BorderLayout.CENTER);
            
            JPanel panelbt=new JPanel(null);
        
         
            Border time=BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK,null);
            TitledBorder title=BorderFactory.createTitledBorder(time,"", TitledBorder.LEFT,TitledBorder.TOP);
            panelbt.setBorder(title);
            panelbt.setPreferredSize(new Dimension(600,100));
      
            panelbt.setBounds(450,10,400,100);
              btup.setBounds(450,10,70,40);
              
            panelbt.add(btup);
           
            BILL.setBounds(50, 10, 110, 20);
            panelbt.add(BILL);
            bill.setBounds(170, 10, 110, 20);
            panelbt.add(bill);
            speed.setBounds(50,30,110,20);
           panelbt.add(speed);
            textspeed.setBounds(170,30,110,20);
           panelbt.add(textspeed);
           panelbt.setBackground(Color.white);
           panelbt.setPreferredSize(new Dimension(200,100));
    
             btup.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==btup)
                        {
                                                
                              while(true)
                              {
                         try{
                                
                            A=Math.toRadians(Proximity.lattitude + 0.00007);
                            B=Math.toRadians(Proximity.lattitude - 0.00007);
                            C=Math.toRadians(Proximity.longitude+.00007);
                            D=Math.toRadians(Proximity.longitude-.00007);
                            E=Math.toRadians(Proximity.lattitude);
                            F=Math.toRadians(Proximity.longitude);
                            G=Math.toRadians(Proximity.deslat);
                            H=Math.toRadians(Proximity.deslong);
                            Distance=Math.acos(Math.sin(G)*Math.sin(E) +
                                    Math.cos(G)*Math.cos(E) *
                                        Math.cos(F-H)) * R;
                            adistance=Math.acos(Math.sin(G)*Math.sin(A) +
                                        Math.cos(G)*Math.cos(A) *
                                         Math.cos(C-H)) * R;
                            bdistance=Math.acos(Math.sin(G)*Math.sin(A) +
                                         Math.cos(G)*Math.cos(A) *
                                        Math.cos(D-H)) * R;
                            cdistance=Math.acos(Math.sin(G)*Math.sin(B) +
                                        Math.cos(G)*Math.cos(B) *
                                        Math.cos(C-H)) * R;
                            ddistance=Math.acos(Math.sin(G)*Math.sin(B) +
                                        Math.cos(G)*Math.cos(B) *
                                        Math.cos(D-H)) * R;
                            System.out.println(Distance+"//"+adistance+"//"+bdistance+"//"+cdistance);
                          if(adistance<Distance)
                             {System.out.println("1");
                              Proximity.lattitude = Proximity.lattitude + 0.00007;
                              
                               Proximity.longitude = Proximity.longitude + 0.00007;
                               yy=Distance-adistance;
                               waypoint(Proximity.dontchangeone,Proximity.dontchangetwo,Proximity.deslat,Proximity.deslong,Proximity.lattitude,Proximity.longitude);
                               
    
                                        repaint();
                               
                                
                                
                              
                             }
                           else if(bdistance<Distance)
                              {System.out.println("2");
                             Proximity.lattitude = Proximity.lattitude + 0.00007;
                             
                               Proximity.longitude = Proximity.longitude - 0.00007;
                               yy=Distance-bdistance;
                               waypoint(Proximity.dontchangeone,Proximity.dontchangetwo,Proximity.deslat,Proximity.deslong,Proximity.lattitude,Proximity.longitude);
                               
                               repaint();
                                
                               
                               
                                 }
                            else if (cdistance<Distance)
                                 {System.out.println("3");
                                      
                                   Proximity.lattitude = Proximity.lattitude - 0.00007;
                 
                               Proximity.longitude = Proximity.longitude + 0.00007;
                               yy=Distance-cdistance;
                               waypoint(Proximity.dontchangeone,Proximity.dontchangetwo,Proximity.deslat,Proximity.deslong,Proximity.lattitude,Proximity.longitude);
                                   
                               repaint();
                                
                             
                               
                                 }
                                 //if((Math.abs(Proximity.deslong)-Math.abs(Proximity.longitude - 0.0001))<(Math.abs(Proximity.deslong)-Math.abs(Proximity.longitude )))
                            else
                                 {System.out.println("4");
                                     Proximity.lattitude = Proximity.lattitude - 0.00007;
                              
                               Proximity.longitude = Proximity.longitude - 0.00007;
                               yy=Distance-ddistance;
                               waypoint(Proximity.dontchangeone,Proximity.dontchangetwo,Proximity.deslat,Proximity.deslong,Proximity.lattitude,Proximity.longitude);
                               mapView.getMainMap().setOverlayPainter(painter);
                               
                               
                               repaint();
                               
                              
                                
                                                                                                      
                                 }
                                 
                             //if((Math.abs(Proximity.deslat)-Math.abs(Proximity.lattitude - 0.0001))<(Math.abs(Proximity.deslat)-Math.abs(Proximity.lattitude ))
                                       
                        }
                             catch(Exception h)
                        {System.out.println(h);}}
    
                        
                }  }
            });
    
    
            add(panelbt);
           
            
            String street="MG Road";
            String city="Mangaluru";
            String state="Karnataka";
            //obtained from trace module
            String desstreet=x;
            String descity=y;
            String desstate=z;
           
    
            try
            {
                gpos = GeoUtil.getPositionForAddress(street,city,state);
                
            }catch (Exception e)
            {
    
                e.printStackTrace();
                gpos = new GeoPosition(51.5,0);
    
            }
             dontchangeone = gpos.getLatitude();
             dontchangetwo = gpos.getLongitude();
              mapView.setCenterPosition(gpos);
              mapView.setZoom(1);
              try
            {
    
                ppos = GeoUtil.getPositionForAddress(desstreet,descity,desstate);
              
            }catch (Exception e)
            {
                e.printStackTrace();
          javax.swing.JOptionPane.showMessageDialog(null, "Address Invalid", "Message", javax.swing.JOptionPane.PLAIN_MESSAGE);
        javax.swing.SwingUtilities.getWindowAncestor(Proximity.this).dispose();
    
                ppos = new GeoPosition(51.5,0);
              
    
            }
    
             deslat = ppos.getLatitude();
             deslong= ppos.getLongitude();
               try
            {
                vehicle = GeoUtil.getPositionForAddress(street,city,state);
    
            }catch (Exception e)
            {
              
                e.printStackTrace();
                vehicle = new GeoPosition(51.5,0);
    
            }
             lattitude= vehicle.getLatitude();
             longitude= vehicle.getLongitude();
            lattitude=lattitude +.001;
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            E=Math.toRadians(Proximity.lattitude);
            F=Math.toRadians(Proximity.longitude);
            G=Math.toRadians(Proximity.deslat);
            H=Math.toRadians(Proximity.deslong);
            xx=Math.acos(Math.sin(G)*Math.sin(E) +
                     Math.cos(G)*Math.cos(E) *
                     Math.cos(F-H)) * R;
            
    
    
            waypoint(dontchangeone,dontchangetwo,deslat,deslong,lattitude,longitude);
        }
            public void waypoint(double dontchangeone,double dontchangetwo,double deslat,double deslong,double vehiclelat,double vehiclelong)
        {
             setExtendedState(MAXIMIZED_BOTH);
             setVisible(true);
             Set<Waypoint> waypoints = new HashSet<Waypoint>();
             waypoints.add(new Waypoint(dontchangeone,dontchangetwo));
             waypoints.add(new Waypoint(deslat,deslong));
             waypoints.add(new Waypoint(vehiclelat,vehiclelong));
            
              painter.setWaypoints(waypoints);
             
              mapView.getMainMap().setOverlayPainter(painter);
              repaint();
           
               
              if (Math.abs(deslat-vehiclelat)<.0003 & Math.abs(deslong-vehiclelong)<.0005)
              {
                    y= Math.floor((xx/count)*3600); //converting into km/hr
                  
                     speedgen=y+"km/hr";
                  textspeed.setText(speedgen);
                  javax.swing.JOptionPane.showMessageDialog(null,"Destination reached"+"\n"+" Avreage speed="+speedgen+"\n"+"Bill="+billgen);
                  try
                        {
    
                        String url = "Jdbc:odbc:vehicle";
                        String userid="root";
                        String password="jmc";
                        Connection con= DriverManager.getConnection(url,userid,password);
                        PreparedStatement sta=con.prepareStatement("update history set average_speed='"+y+"',bill='"+bil+"',Status='Delivered'where slno='"+slno+"'");
                        sta.executeUpdate();
                        }
                        catch(Exception ee)
                        {
    
                        }
                  System.exit(0);
              }
     else 
              {
                
                  try
                  {                                    
                     Thread.sleep(1000);
                      count++;
                     bil=Math.floor(yy*100*count);
                     billgen="RS"+bil;
                     bill.setText(billgen);
                    System.out.println(count+billgen+yy);
    
    
                  [b]  [color=red] btup.doClick(); [/color][/b][color=red] [/color]   //move the marker 1step forward 
    
    
                  }
                  catch(Exception gg)
                  {
    
                  }
    
     }
      
               }
           public static void main(String[] args)
             {
               o= new Proximity();
               o.map("MG Road","Mangaluru","Karnataka");
            }
    }
    //calling class

    Java Code:
    class me
    {
    .....
    .....
    
    JButton b=new JButton("start");
    b.addActionListener(new ActionListener()
    {
           public void actionPerformed(actionEvent e)
           {
                if(e.getSource==b)
                 {
                           new Proximity();
                  }
    
            }
    }




    i tryed to change the flow of the program manier times,but dint successed in them,i personally feel that the problem lies in the code that is btup.doClick(); since the entire class is bieng called from another buttons actionlistener,
    please provide any information on how i could change the flow of the program, or any other idea to surpass this problem!



    Thank You,
    Manish87
    Last edited by Manish87; 03-31-2011 at 11:58 AM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default

    That's not really a "snippet". If you want help with code, you'll have to post an SSCCE that demonstrates the behavior you're talking about.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Here's the main class which is used to postion the marker on the map,which is done eventually when an action is performed on the button btup.

    Java Code:
    class Proximity
    {
    
    
    public void map(arg1,arg2,arg3)
    {
    .....
    ....
    ....
    
    
     JButton btup=new JButton("Start");
    
    
     btup.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==btup)
                        {
                                                
                              while(true)
                              {
                         
    
                        // codeing to reposition the marker ,say 1step forward/backward,when the marker reaches a particular place,the frame is closed. 
                            
                        //code to play sound goes here
                              }  
              } });
    
    
    
                    [b]btup.doclick();[/b] // for every click the marker is placed on the map
    
    
    .....
    .....
    ...
    
    }
    case 1:

    the above class works fine when called from the following class
    Java Code:
    class Trace
    {
       public Trace()
       {
          Proximity o=new Proximity();
          o.map(arg1,arg2,arg3)
        }
    
    }

    case 2:

    //following code doesnt work,when i call the proximity class from an actionListener

    Java Code:
    class Trace
    {
    
    
    public void Trace()
    {
    .....
    ....
    ....
    
    
     JButton clickme=new JButton("click me");
    
    
     clickme.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                        if(e.getSource()==clickme)
                        {
                                                
                           Proximity o=new Proximity();
                           o.map(arg1,arg2,arg3)
    
              } });
    
    
    
    .....
    .....
    ...
    
    }


    Output based on (case2):

    when the button "click me" is invoked, a white blank frame gets loaded up "until" the marker reaches the destination,,but yes the sound plays.




    Kind Regards,
    Manish87

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

    Default

    You'll find something useful here:
    Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    Your 'case 2' executes the code on the EDT and probably blocks it, preventing painting.

    db

  5. #5
    Manish87 is offline Member
    Join Date
    Aug 2010
    Posts
    29
    Rep Power
    0

    Default

    Hello,

    Thank you sir,i will definetly look into the link which you posted!. sir,in my case Should i create 2 different thread in here? one for displaying map and another for plotting the markers?

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

    Default

    Go through the tutorial linked, where you will find examples of using a SwingWorker.

    db

Similar Threads

  1. no return- problem with actionlistener
    By lparadiso in forum AWT / Swing
    Replies: 2
    Last Post: 11-21-2010, 02:31 AM
  2. Problem Actionlistener
    By Questionmark in forum New To Java
    Replies: 18
    Last Post: 08-07-2010, 12:29 PM
  3. Problem w/ ActionListener on a button
    By qwertyuiop23 in forum AWT / Swing
    Replies: 2
    Last Post: 11-02-2009, 06:25 AM
  4. ActionListener run automatically problem
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-23-2009, 09:42 PM
  5. ActionListener Applet problem
    By xander5511 in forum Java Applets
    Replies: 1
    Last Post: 02-21-2009, 03:42 AM

Posting Permissions

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