Results 1 to 18 of 18
- 01-22-2013, 10:13 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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:
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.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"); } }
- 01-22-2013, 10:14 AM #2
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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:
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.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"); } }
- 01-22-2013, 10:15 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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:
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.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"); } }
- 01-22-2013, 10:16 AM #4
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
- 01-22-2013, 10:17 AM #5
Re: paintComponent() Conditionnel documentListener
Stop crossposting! http://www.java-forums.org/java-2d/6...tlistener.html
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-22-2013, 10:21 AM #6
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: paintComponent() Conditionnel documentListener
ok sorry but i asked this several times now and still no answer :/
- 01-22-2013, 10:26 AM #7
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
- 01-22-2013, 10:53 AM #8
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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.
- 01-22-2013, 11:20 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-22-2013, 11:45 AM #10
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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 :
the JTextArea "Traffic" show me the Traffic and the programs runs without any problems.Java Code:Traffic.getDocument().addDocumentListener(new MyDocumentListener ());
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.
- 01-22-2013, 12:33 PM #11
Re: paintComponent() Conditionnel documentListener
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-22-2013, 12:42 PM #12
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Re: paintComponent() Conditionnel documentListener
ok sorry again a despreat act it won't happen again .
- 01-22-2013, 01:48 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-22-2013, 02:51 PM #14
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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):
==>graphicPanel is the name of the JPanel.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); } }
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.
- 01-22-2013, 03:24 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-22-2013, 03:30 PM #16
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
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();
- 01-22-2013, 04:49 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-22-2013, 04:52 PM #18
Member
- Join Date
- Dec 2012
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
Deadlock with DocumentListener
By dsharma in forum AWT / SwingReplies: 6Last Post: 04-30-2012, 03:30 PM -
DocumentListener, KeyListener or a third option?
By Muskar in forum New To JavaReplies: 6Last Post: 11-20-2010, 11:58 PM -
Use documentlistener to get source of which text field is changed
By Andolph23 in forum AWT / SwingReplies: 3Last Post: 05-28-2010, 04:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks