Results 1 to 11 of 11
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Exclamation JOptionPane when called from inside the paintComponent() method

    Java Code:
    import javax.swing.*;
    import java.awt.Graphics;
    
    public class JPanel01 extends JPanel {
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            testD();
        }
    
        void testD() {
            javax.swing.JOptionPane.showMessageDialog(null, "Syntax Error : ", "Incorrect user input", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    
        public static void main(String args[]) {
            javax.swing.JFrame frame = new javax.swing.JFrame("Two-Dimensional Graph Sketcher");
            frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel01());
            frame.setSize(1280, 770);
            frame.setVisible(true);
        }
    }

    When the JOptionPane method is called from inside the paintComponent() method it is displayed multiple time.. why does it happens so??

    In my project a method called from inside the paintComponent method displays error message using JOptionPane .. and when the file is run then the above problem is encountered .

    Can anybody explain how can I make the use of JOptionPane efficiently.thnx:cool:

  2. #2
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Red face

    As an alternative to the above solution I adopted this another practice.. I created a class the initializes a frame and then called this frame from another class... The problem however surprisingly remained the very same.

    Java Code:
    import javax.swing.*;
    import java.awt.Color;
    
    public class Errorframe01 {
    
        JFrame error_frame = new JFrame("Invalid User Input");
        JPanel error_panel = new JPanel();
        JTextPane error_textpane = new JTextPane();
    
        public Errorframe01() {
            error_textpane.setText("Syntax Error");
            error_panel.add(error_textpane);
            error_panel.setBackground(Color.WHITE);
            error_frame.add(error_panel);
            error_frame.setSize(400, 200);
            error_frame.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
        }
    }

    Java Code:
    import javax.swing.*;
    import java.awt.Graphics;
    
    public class JPanel01 extends JPanel {
    
        JButton b = new JButton("Display");
    
        JPanel01() {
            add(b);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            testD();
        }
    
        void testD() {
                new Errorframe01().error_frame.setVisible(true);
        }
    
        public static void main(String args[]) {
            javax.swing.JFrame frame = new javax.swing.JFrame();
            frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel01());
            frame.setSize(500, 500);
            frame.setVisible(true);
        }
    }

    plz help me in resolving this problem.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    We've discussed this before, you and I. Never, never call JOptionPane from inside of paint/paintComponent. Period. You cannot control when this method gets called. This method should be small and fast and never slowed down by user interaction. This is not a problem to resolve other than to never get user interaction from inside of this method.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Y. Progammer View Post
    plz help me in resolving this problem.
    The paintComponent( ... ) method is called whenever 'something' (the AWT EDT thread) thinks the component needs to be repainted. That method is called often and often at moments you didn't expect it to be called. So, don't show a JOptionPane in the body of that method. You have to take that logic out of the method and show it when needed.

    kind regards,

    Jos

  5. #5
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    ok. Here I come straight to the problem which i m facing exactly..

    the painComponent() method calls a method that throws exception.. that is handled using try-catch inside the paintComponent().. that's ok i won't call JOptionPane or Frame or other components from inside the Paint method. but when an exception is caught inside the paint i need to inform the user about it using JOptionPane or any other component..

    How can i exactly inform the user about the exception been caught while the exception is handled inside the paint method.?? plz suggest.:confused:

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi everyone.

    I agree with Fubarable and JosAH. This piece of code certainly is strange, however there are times that one would like user input in any situation. I tried to create a simple console for Java, and faced a similar problem. I thought the output the OP's code gives was cool. ;) But anyways:
    Java Code:
    import javax.swing.*;
    import java.awt.Graphics;
    
    public class JPanel01 extends JPanel {
    
        protected boolean canPaint = true;
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (canPaint) {
                // Do some painting if we can..
                // Lets say an error occurred:
                // - If the dialog is shown over the component then the component will 
                //   be redrawn. Now, we don't want this code to loop, so we set a 
                //   flag to control the "loop".
                canPaint = false;
                // - Now we tell the AWT event queue that we want to show the message
                //   whenever it get's a chance, because the Dialog won't be drawn
                //   properly if it's called here.
                java.awt.EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        testD();
                    }
                });
            }
        }
    
        void testD() {
            javax.swing.JOptionPane.showMessageDialog(null, "Syntax Error : ", "Incorrect user input", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
    
        public static void main(String args[]) {
            javax.swing.JFrame frame = new javax.swing.JFrame("Two-Dimensional Graph Sketcher");
            frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
            frame.add(new JPanel01());
            frame.setSize(1280, 770);
            frame.setVisible(true);
        }
    }
    In general, Y. Progammer, this is not good programming practice.

    Good luck
    Tim
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Why do you need to catch an exception in paintComponent? Exceptions should be caught from errors in program logic, and paintComponent should be free of program logic. Please go into that in a bit more detail as I suspect that your program has a faulty design. Perhaps we can help you fix it. You may need to create an SSCCE that shows why the try/catch is necessary.

    Much luck.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Y. Progammer View Post
    ok. Here I come straight to the problem which i m facing exactly..

    the painComponent() method calls a method that throws exception.. that is handled using try-catch inside the paintComponent().. that's ok i won't call JOptionPane or Frame or other components from inside the Paint method. but when an exception is caught inside the paint i need to inform the user about it using JOptionPane or any other component..

    How can i exactly inform the user about the exception been caught while the exception is handled inside the paint method.?? plz suggest.:confused:
    You're dealing with the classic producer/consumer problem: something produces an error (the producer) and something else (the consumer) is waiting for something to be produced. As long as nothing is produced the consumer waits; when something has been produced the producer notifies anything else that is waiting for it.

    kind regards,

    Jos

  9. #9
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    I am actually plotting a graph inside the paintComponent method. and I need to replot it every time the window appears. Now taking the logic outside the paintComponent method would be quite typical perhaps.. beacuase as i mentioned i need to replot it.

    and within the logic an exception is thrown and handled .. and i need to inform the user for the same.suggest.

  10. #10
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Well, a graph typically consists out of primitives: polylines, polygons, ellipses, images and so on. Let a method outside of the paint method do the graph construction outputting a structure of primitives and just draw that structure in your paint method. You can remove all logic from your paint method this way, and only worry about painting. That's what I would do. ;) Use a window event to call the method when the window is shown.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Y. Progammer View Post
    I am actually plotting a graph inside the paintComponent method.
    No, this is a very bad design indeed as you're doing what you've been told never to do: never do program logic inside of paintComponent. Never. Simply don't do it. Instead, plot your graph in your regular program and place the plotted points into a collection. Call repaint() when you want the GUI to display the plot or the portion of the plot, and then have paintComponent use collection of points to draw the graph.

    and I need to replot it every time the window appears. Now taking the logic outside the paintComponent method would be quite typical perhaps.. beacuase as i mentioned i need to replot it.
    Bad solution though. Use a focus listener or a WindowListener (perhaps better), but (again): Let me repeat what I stated above: never do program logic inside of paintComponent. Never.

Similar Threads

  1. what made paintComponent() method to be called twice??
    By Y. Progammer in forum New To Java
    Replies: 5
    Last Post: 02-21-2010, 10:19 PM
  2. how to call service method inside another service
    By kirtichopra2003 in forum Web Frameworks
    Replies: 1
    Last Post: 10-24-2009, 02:19 AM
  3. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 02:07 PM
  4. Replies: 1
    Last Post: 07-26-2008, 05:26 PM
  5. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 02:43 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
  •