Results 1 to 18 of 18
  1. #1
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default paintComponent() Conditionnel documentListener

    I asked her to all the forums and I hope to find an answer here because I'm really stuck. So my problem is that I want a entant the data is inserted into a JTextArea I analysis (test with "if") then has each case I want to draw in a JPanel corresponding sound but I see no solution how I'm going with its paintComponent (). To see if there is an insertion I implemented one of the JTextArea class DocumentListener and voila:

    Java Code:
     protected class MyDocumentListener  extends JPanel implements    javax.swing.event.DocumentListener {
    
     public String gettypeevent () {
        DocumentEvent documentEvent = null;
        DocumentEvent.EventType type = documentEvent.getType();
        return type.toString();
    }
        @Override
    public void changedUpdate(javax.swing.event.DocumentEvent e) {
            // text has been altered in the textarea
    
            }
        @Override
    public void insertUpdate(javax.swing.event.DocumentEvent e) 
        {
                 // text has been added to the textarea 
                   System.out.println(gettypeevent() );  
             try { 
               //if not prompt Line
                if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                  Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
          {//if a line after a replace has been inserted
    
              if (Traffic.getLineCount()  == (lastreplace + 2) ) 
              {
    System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
                 //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                      //          Traffic.getLineEndOffset(lastreplace) - 
                       //                 Traffic.getLineStartOffset(lastreplace)));
    
                   lastreplace +=1;
    
              }
    
    
                else//last line when it's not detected by the previous condition it'll be consumed here
              {
                 System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
               //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                    //            Traffic.getLineEndOffset(lastreplace) - 
                    //                    Traffic.getLineStartOffset(lastreplace))); 
              }
          }
            Traffic.setCaretPosition(Traffic.getDocument().getLength());
    
            } catch (BadLocationException ex) {
                Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
            }
    
         }
    
        @Override
    public void removeUpdate(javax.swing.event.DocumentEvent e) {
      // text has been removed from the textarea
    
    }
    
        @Override
        protected void paintComponent (Graphics g)
     {
    
      System.out.println("fffffffffff");
      super.paintComponent(g);
      g.drawString("fffffffffff", 120, 60);
      System.out.println("fffffffffff");
    
      }
    
    
    }
    I tried to use a method called "PortArchitecture (..)" will draw what I want and I use getGraphics () but I was told that it should not be used paintComponent () and now I'm really stuck please help.

  2. #2
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default paintComponent() Conditionnel documentListener

    I asked her to all the forums and I hope to find an answer here because I'm really stuck. So my problem is that I want a entant the data is inserted into a JTextArea I analysis (test with "if") then has each case I want to draw in a JPanel corresponding sound but I see no solution how I'm going with its paintComponent (). To see if there is an insertion I implemented one of the JTextArea class DocumentListener and voila:

    Java Code:
    protected class MyDocumentListener  extends JPanel implements    javax.swing.event.DocumentListener {
    
     public String gettypeevent () {
        DocumentEvent documentEvent = null;
        DocumentEvent.EventType type = documentEvent.getType();
        return type.toString();
    }
        @Override
    public void changedUpdate(javax.swing.event.DocumentEvent e) {
            // text has been altered in the textarea
    
            }
        @Override
    public void insertUpdate(javax.swing.event.DocumentEvent e) 
        {
                 // text has been added to the textarea 
                   System.out.println(gettypeevent() );  
             try { 
               //if not prompt Line
                if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                  Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
          {//if a line after a replace has been inserted
    
              if (Traffic.getLineCount()  == (lastreplace + 2) ) 
              {
    System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
                 //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                      //          Traffic.getLineEndOffset(lastreplace) - 
                       //                 Traffic.getLineStartOffset(lastreplace)));
    
                   lastreplace +=1;
    
              }
    
    
                else//last line when it's not detected by the previous condition it'll be consumed here
              {
                 System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
               //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                    //            Traffic.getLineEndOffset(lastreplace) - 
                    //                    Traffic.getLineStartOffset(lastreplace))); 
              }
          }
            Traffic.setCaretPosition(Traffic.getDocument().getLength());
    
            } catch (BadLocationException ex) {
                Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
            }
    
         }
    
        @Override
    public void removeUpdate(javax.swing.event.DocumentEvent e) {
      // text has been removed from the textarea
    
    }
    
        @Override
        protected void paintComponent (Graphics g)
     {
    
      System.out.println("fffffffffff");
      super.paintComponent(g);
      g.drawString("fffffffffff", 120, 60);
      System.out.println("fffffffffff");
    
      }
    
    
    }
    I tried to use a method called "PortArchitecture (..)" will draw what I want and I use getGraphics () but I was told that it should not be used paintComponent () and now I'm really stuck please help.

  3. #3
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default paintComponent() Conditionnel documentListener

    I asked her to all the forums and I hope to find an answer here because I'm really stuck. So my problem is that I want a entant the data is inserted into a JTextArea I analysis (test with "if") then has each case I want to draw in a JPanel corresponding sound but I see no solution how I'm going with its paintComponent (). To see if there is an insertion I implemented one of the JTextArea class DocumentListener and voila:

    Java Code:
    protected class MyDocumentListener  extends JPanel implements    javax.swing.event.DocumentListener {
    
     public String gettypeevent () {
        DocumentEvent documentEvent = null;
        DocumentEvent.EventType type = documentEvent.getType();
        return type.toString();
    }
        @Override
    public void changedUpdate(javax.swing.event.DocumentEvent e) {
            // text has been altered in the textarea
    
            }
        @Override
    public void insertUpdate(javax.swing.event.DocumentEvent e) 
        {
                 // text has been added to the textarea 
                   System.out.println(gettypeevent() );  
             try { 
               //if not prompt Line
                if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                  Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
          {//if a line after a replace has been inserted
    
              if (Traffic.getLineCount()  == (lastreplace + 2) ) 
              {
    System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
                 //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                      //          Traffic.getLineEndOffset(lastreplace) - 
                       //                 Traffic.getLineStartOffset(lastreplace)));
    
                   lastreplace +=1;
    
              }
    
    
                else//last line when it's not detected by the previous condition it'll be consumed here
              {
                 System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                Traffic.getLineEndOffset(lastreplace) - 
                                        Traffic.getLineStartOffset(lastreplace)));
               //  PortArchitecture (Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                    //            Traffic.getLineEndOffset(lastreplace) - 
                    //                    Traffic.getLineStartOffset(lastreplace))); 
              }
          }
            Traffic.setCaretPosition(Traffic.getDocument().getLength());
    
            } catch (BadLocationException ex) {
                Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
            }
    
         }
    
        @Override
    public void removeUpdate(javax.swing.event.DocumentEvent e) {
      // text has been removed from the textarea
    
    }
    
        @Override
        protected void paintComponent (Graphics g)
     {
    
      System.out.println("fffffffffff");
      super.paintComponent(g);
      g.drawString("fffffffffff", 120, 60);
      System.out.println("fffffffffff");
    
      }
    
    
    }
    I tried to use a method called "PortArchitecture (..)" will draw what I want and I use getGraphics () but I was told that it should not be used paintComponent () and now I'm really stuck please help.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: paintComponent() Conditionnel documentListener

    Still no need to crosspost! Stick to one thread, please.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: paintComponent() Conditionnel documentListener

    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    ok sorry but i asked this several times now and still no answer :/

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: paintComponent() Conditionnel documentListener

    Excuse me?! This is not a code mill, but a forum with volunteers. So you might have to wait for a few hours(or days) until somebody is willing to help you. If you need immediate help go to rent a coder and hire somebody. Crossposting and whining that you got no answer will not really attract volunteers.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #8
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    don't you think I know that and I've been waiting for days now but u could if u can answer and all problems are gone and I'll delete the others I just wanted to increase my chance that's all.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: paintComponent() Conditionnel documentListener

    I suspect part of your problem is people don't have any idea what it is you are trying to do, and what exactly is the problem.

    That code is not exactly easy to follow, for instance.
    Since we are all volunteers many of us will simply not bother to try and figure it out.

    Put simply, what do you want to see happening, and what are you seeing at the moment.
    Include any exceptions or errors in full (stacktrace included) and highlight where they are thrown.
    Include any output seen, and point out why that output is not correct.

    And please format your code correctly so it's easy to follow.

    Also describe what any non-standard classes do.
    Traffic in your case.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    thanks toll.
    so here it's :
    Traffic is a JTextArea where I insert text coming from a serial Port and I did implement a DocumentListener :

    Java Code:
    Traffic.getDocument().addDocumentListener(new MyDocumentListener ());
    the JTextArea "Traffic" show me the Traffic and the programs runs without any problems.
    Now what I want to do is to act upon what have been inserted(only the event of insert) line by line(that's what the test are for),I'll give an example :

    ==> when I recieve "ON-HOOK" I put in the JPanel an image of a telephone on hook if I recieve "OFF-HOOK" I put in the JPanel an image of a telephone off hook and the list goes...

    what I didn't undrestand is how to do that , how to call paintComponent when I want and make it draw what I want since I can do only graphic operation in the paintComponent().
    And this is the class for the DocumentListener :

    Java Code:
         protected class MyDocumentListener  extends JPanel implements javax.swing.event.DocumentListener
           {
            @Override
              public void changedUpdate(javax.swing.event.DocumentEvent e) {
                // text has been altered in the textarea
                 
                }
    
            @Override
       public void insertUpdate(javax.swing.event.DocumentEvent e) 
            {
                    // text has been added to the textarea 
                try { 
                  
                    if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                      Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
                    {
                  
                      if (Traffic.getLineCount()  == (lastreplace + 2) ) 
                      {
                         System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),Traffic.getLineEndOffset(lastreplace) -   
                                                                                                                                Traffic.getLineStartOffset(lastreplace)));
                       lastreplace +=1;
    
                       }
                    
               
                     else
                     {
                       System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                    Traffic.getLineEndOffset(lastreplace) - 
                                            Traffic.getLineStartOffset(lastreplace)));
                     }
                  }
                   Traffic.setCaretPosition(Traffic.getDocument().getLength());
                
                } catch (BadLocationException ex) {
                    Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
                }
    
            }
            
            @Override
       public void removeUpdate(javax.swing.event.DocumentEvent e) {
          // text has been removed from the textarea
               
       }
    
        }

    I did use getGraphics() and I did get a result but everyone said "Do not use getGraphics ,use paintComponent()!" so here I'm .
    And thanks.
    Last edited by taptaptill; 01-22-2013 at 11:54 AM.

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

    Default Re: paintComponent() Conditionnel documentListener

    Quote Originally Posted by taptaptill View Post
    I asked her to all the forums
    Please go through the Forum Rules that you agreed to follow when you became a member of this site. No more multiposting please.

    I've merged all three threads here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    ok sorry again a despreat act it won't happen again .

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: paintComponent() Conditionnel documentListener

    Right, so Traffic is an instance variable?
    Naming conventions exist for a reason...that should be 'traffic' with a lowercase 't'. Using a capital 'T' made it look like a class name.

    Where is the JPanel you want to redraw?
    Presumably the paintComponent method in that has access to something that tells it whether to paint a particular image.
    And that "something" (a flag, an enum, don't really care) is going to be set by the code above?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    that's what I'm trying to figure out.
    I tryed this but it didn't work (the paintComponent() is not called):
    Java Code:
    protected class MyDocumentListener  extends JPanel implements javax.swing.event.DocumentListener
        {
         @Override
           public void changedUpdate(javax.swing.event.DocumentEvent e) {
             // text has been altered in the textarea
               
             }
     
         @Override
    public void insertUpdate(javax.swing.event.DocumentEvent e) 
         {
                 // text has been added to the textarea 
             try { 
                
                 if (!Traffic.getText(Traffic.getLineStartOffset(Traffic.getLineCount()-1),
                   Traffic.getLineEndOffset(Traffic.getLineCount()-1)-Traffic.getLineStartOffset(Traffic.getLineCount()-1)).contains(">>"))
                 {
                
                   if (Traffic.getLineCount()  == (lastreplace + 2) ) 
                   {
                      System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),Traffic.getLineEndOffset(lastreplace) -   
                                                                                                                             Traffic.getLineStartOffset(lastreplace)));
                    lastreplace +=1;
                        graphicPanel.repaint();
                    }
                  
             
                  else
                  {
                    System.err.println(Traffic.getText(Traffic.getLineStartOffset(lastreplace),
                                 Traffic.getLineEndOffset(lastreplace) - 
                                         Traffic.getLineStartOffset(lastreplace)));
                  }
               }
                Traffic.setCaretPosition(Traffic.getDocument().getLength());
              
             } catch (BadLocationException ex) {
                 Logger.getLogger(TrafficSerialPort.class.getName()).log(Level.SEVERE, null, ex);
             }
     
         }
          
         @Override
    public void removeUpdate(javax.swing.event.DocumentEvent e) {
       // text has been removed from the textarea
             
    }
    
        @Override
       protected void paintComponent(Graphics g)
         {
            super.paintComponent(g);
            Graphic2D gfx = (Graphic2D) g;
            gfx.drawString("helloooo",100,100);
          }
     
     }
    ==>graphicPanel is the name of the JPanel.


    I don't know where will I put my tests or how I'll change the objects to paint.
    Last edited by taptaptill; 01-22-2013 at 03:14 PM.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: paintComponent() Conditionnel documentListener

    The paintComponent method needs to be on your (extension) of JPanel, since it is that which is doing the painting.
    MyDocumentListener is not a JPanel.
    Don't try and make it one.

    Where is graphic Panel declared?
    What class is it?
    It needs to be one that extends JPanel and has paintComponent code that draws your telephone based on some flag or other.

    Your listener then sets the flag and (as now) calls repaint().

    At the moment all you seem to have is the repaint() call, and a misplaced paintComponent() method. You need the flag, you need to set the flag, and you need to put that paintComponent in the correct place.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  16. #16
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    I'm using Netbeans so when I created the JPanel I did it graphically.so the graphicPanel is declared in the initComponent() :
    Java Code:
    graphicPanel = new javax.swing.JPanel();

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: paintComponent() Conditionnel documentListener

    Rule 1 for Swing...do not use a GUI builder if you do not know Swing like the back of your hand.
    You should be doing this by hand.

    In any case, you need to subclass JPanel in order to override paintCOmponent() so that you can then get it to do what you want.
    That is the only way to change how a component paints.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  18. #18
    taptaptill is offline Member
    Join Date
    Dec 2012
    Posts
    32
    Rep Power
    0

    Default Re: paintComponent() Conditionnel documentListener

    ok thanks that's what I was afraid from from the begining so I'll do the graphics again handy and try to work it out.Thanks

Similar Threads

  1. Deadlock with DocumentListener
    By dsharma in forum AWT / Swing
    Replies: 6
    Last Post: 04-30-2012, 03:30 PM
  2. DocumentListener, KeyListener or a third option?
    By Muskar in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 11:58 PM
  3. Replies: 3
    Last Post: 05-28-2010, 04:18 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
  •