Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

puzzling problem involving JTextPane  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am working on a simple word processor. i decided to test it some and found a problem. it has a JMenuItem called htmlSourceMenuItem that is initially disabled. if i open a plain text file it remains disabled. if i then open an html file it becomes enabled. but if i then open a plain text file it remains enabled. i added an else clause to no effect. i then added the line that is commented out, but that didn't fix the problem either. it is important this works right, but i am stumped. here is the code.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
some more information. it does the same thing with rtf files. i tried clicking the html source menu item. when viewing a plain text file it just read the file back in one line at a time. however when i did the same with an rtf file it translated it into html source
perhaps i should do like some companies do and call it a feature instead of a bug

i tried a couple other fixes to no effect

it gets disabled ok when i click the New button

also when i click the htmlSourcMenuItem

to reiterate:
i run the program and htmlSourceMenuItem is disabled
i open an rtf file and it is still disabled
i then open an html file and it becomes enabled
i then open an rtf file and it remains enabled
i then click on it and the rtf file gets translated into html source
 
Bernhard Goetz
Ranch Hand
Posts: 57
1
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this really seems odd.

I would've guessed that the second rtf throws an exception and since you don't really handle these you wouldn't notice that something went wrong. Try to put at least a System.out.println(ex) into the catch clauses.
On the other hand, the first opening of the rtf seemed to work, so there probably aren't any exceptions thrown.

In your position I would try to line debug it. Are you using Eclipse or some other IDE?

Regards,
Bernhard
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, i don't use an IDE. i use notepad++(a great text editor for various source codes)

my bad! it is not translating to html. it is showing the rtf markup(or whatever). i was so shocked i didn't look at it close.

i don't know how well this will post. the rtf file is just straight text except the lines that start note: are in bold red

Drawing
You can change the width of what you draw by going to the Draw menu and changing the width.
Note: The status of the Constrain checkbox effects the final result.

Flood Fill
Draw an outline. Click on Floodfill. Choose the area to fill.
Note: The outline color and the fill color must be the same.

Constrain
If Constrain is checked, the final image is the size of the original image. If it is unchecked, the final image is the size of the drawing area.

Fade
Note: Faded images will only save correctly as .png

Transparency
To make a transparent background, the background must be all one color. If it is not, use the drawing tools to make it so.
Note: You can save these images as .png or .gif

Superimpose
First you need a background image. Use open, paste, or draw one. Click on Superimpose and choose an image file to overlay. Use the mouse to position the overlay, then click and you are done.
Note: The status of the Constrain checkbox effects the final result.

Save As
Note: When saving an image that contains transparency, you should not save it as jpg or jpeg.


here is the result after i click htmlSourceMenuItem

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}{\f1\fswiss\fprq2\fcharset0 Arial;}}
{\colortbl ;\red255\green0\blue0;}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\b\f0\fs24 Drawing\par
\b0 You can change the width of what you draw by going to the Draw menu and changing the width.\par
\cf1 Note: The status of the Constrain checkbox effects the final result.\par
\cf0\par
\b Flood Fill\par
\b0 Draw an outline. Click on Floodfill. Choose the area to fill.\par
\cf1\f1 Note: The outline color and the fill color must be the same.\par
\cf0\f0\par
\b Constrain\b0\par
If Constrain is checked, the final image is the size of the original image. If it is unchecked, the final image is the size of the drawing area.\par
\b\par
Fade\b0\par
\cf1\f1 Note: Faded images will only save correctly as .png\cf0\f0\par
\par
\b Transparency\par
\b0 To make a transparent background, the background must be all one color. If it is not, use the drawing tools to make it so.\par
\cf1\f1 Note: You can save these images as .png or .gif\cf0\f0\par
\par
\b Superimpose\par
\b0 First you need a background image. Use open, paste, or draw one. Click on Superimpose and choose an image file to overlay. Use the mouse to position the overlay, then click and you are done.\par
\cf1 Note: The status of the Constrain checkbox effects the final result.\par
\cf0\b\par
Save As\par
\cf1\b0\f1 Note: When saving an image that contains transparency, you should not save it as jpg or jpeg.\par
\cf0\f0\par
\par
}

i was joking when i said maybe i should just call it a feature instead of a bug, but considering the results, maybe i should just let it be enabled all the time and call it a feature.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is exactly what i am going to do. it actually IS a feature. if it is a plain text file it just re-displays it. if it is an html file it displays the html source. if it is an rtf file it displays something like the above. i will just change the menu from View/HTML Source to View/Source. i get an extra feature in exchange for less code.

i am still curious why the code i originally posted didn't work as expected, but i am not going to worry about it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!