I have a class which extends JPanel and implements ActionListener.
I am having 2 menuItem. When I implemented the abstract method actionPerformed(ActionEvent e) and used repaint if the event source are the menuItem paint method is not called. What can be the possible reasons?

This is what I am doing inside the ActionListener implemantation

FileInputStream fstream = null;
            try {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(frame);
                File file1 = null;
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file1 = fc.getSelectedFile();
                }
                fstream = new FileInputStream(file1);
                br = new BufferedReader(new InputStreamReader(new DataInputStream(fstream)));
                String str;
                while ((str = br.readLine()) != null) {
                     this.repaint();
                    String[] strarr = str.split(",");
                    
                    if (strarr[0].trim().equals("$GPGGA")) {
                        //System.out.println(str);
                        String latstringint = strarr[2].trim().substring(0, 2);
                        double latstringdec = Double.parseDouble(strarr[2].trim().substring(2)) / 60;
                        if (strarr[3].trim().equals("N")) {
                            Lat = Integer.parseInt(latstringint) + latstringdec;
                        } else {
                            Lat = -(Integer.parseInt(latstringint) + latstringdec);
                        }
                        String lonstringint = strarr[4].trim().substring(0, 3);
                        double lonstringdec = Double.parseDouble(strarr[4].trim().substring(3)) / 60;
                        if (strarr[5].trim().equals("E")) {
                            Lon = Integer.parseInt(lonstringint) + lonstringdec;
                        } else {
                            Lon = -(Integer.parseInt(lonstringint) + lonstringdec);
                        }
                        System.out.println(Lat+"\t"+Lon);
                        //pos[0]= Lat;
                        //pos[1]= Lon;
                        //path.add(pos);
                                                 
                        this.repaint();
                        Thread.sleep(250);
                        
                    }
                    if (strarr[0].trim().equals("$GPVTG")) {
                        heading = Double.parseDouble(strarr[1]);
                        sog = Double.parseDouble(strarr[7]);
                        this.repaint();
                    }
                }
            } catch (Exception ex) {
                Logger.getLogger(MovingMap.class.getName()).log(Level.SEVERE, null, ex);
            }

Recommended Answers

All 4 Replies

You called "this".repaint(). Try doing

if (event.getSource() == menuItem) menuItem.repaint();

Google the Java Event Dispatch Thread and read about Swing threads.
Your repaint() calls won't do anything until your event method completes, even if it sleeps in the meantime..

Google the Java Event Dispatch Thread and read about Swing threads.
Your repaint() calls won't do anything until your event method completes, even if it sleeps in the meantime..

Thanks for your reply. I tried to solve it out by creating a new class java.util.Timer which will add the data and call the repaint periodically but that is also not working.
What are your suggestions on how to overcome this problem

Use javax.swing.Timer, it's better adapted to work with the swing EDT.
As for the latest version of your code - I've no idea wht may be wrong with it (for an obvious reason :-) )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.