JDK-6194767 : JComponent should have an isPrinting() method
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 5.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2004-11-12
  • Updated: 2017-05-16
  • Resolved: 2004-12-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b16Fixed
Related Reports
Relates :  
Relates :  
Description
5.0 added printing support to JTable. While the table printing support deals with the details of hiding selection and focus during printing, developers want an easy way to customize other aspects of their printouts.

For example, one developer commented that he had a table with alternating row colors. In a printed output he didn't want these colors. He needed some way for his renderers to know that printing was going on.

We need to expose an isPrinting() flag for this purpose.
###@###.### 2004-11-12 18:45:47 GMT

Comments
EVALUATION I filed this, I agree. The approach will be to add an isPrinting() flag to JComponent to be set and cleared whenever print(Graphics) is called. Additionally, JTable will be changed to look at this flag instead of the one it currently maintains separately. This will affect existing code that uses the print(Graphics) method but it is expected that developers will want selection and focus turned off during printing, regardless of the method they take. See the work-around section for details on how developers can customize their printing until this flag is released. ###@###.### 2004-11-12 18:45:47 GMT You don't need a new API to know you are printing. All you need to do in your app is write if (graphics instanceof PrinterGraphics) .. This works now (1.5). Where that wouldn't work is if you want to know this outside of the context of the rendering. But testing for PrinterGraphics is more precise in that if you have to repaint the screen because of an expose event, isPrinting() may mislead you into repainting the screen in the way you intended to print. If you test for the graphics you know for sure if you are printing (or not). ###@###.### 2004-11-12 20:44:09 GMT ====== We have SwingUtilities2.isPrinting(Graphics g) already. It is package private but could be public as well. ###@###.### 2004-11-13 00:50:29 GMT ====== Thanks ###@###.### - that's another great way for the developer to know that they're printing. What I'd like to offer with the new isPrinting() in particular, is a way for renderers (which don't get a graphics when they're configured) to know if the associated component is printing. And by overriding it in JTable they can customize whether or not we show the selection/focus in the printouts. Also, JComponent already keeps a flag to say whether or not it is printing (used to determine its use of double buffering, etc.) and I'd simply like to expose it. NOTE that there will not be any problem with getting mixed up in expose events. This flag is set by the print(Graphics) method which is called only when rendering for printing. It will be clearly documented that it should not be confused with other higher level printing APIs like JTable.print(). ###@###.### 2004-11-16 15:44:47 GMT Just decided: JTable.print() will fire "printing" property change events as it changes the value of isPrinting(). ###@###.### 2004-11-30 19:37:42 GMT
12-11-2004

WORK AROUND Until this flag is made public, developers can simply override JTable's print(Graphics) method to set and clear their own flag: public void print(Graphics g) { isPrinting = true; try { super.print(g); } finally { isPrinting = false; } } ###@###.### 2004-11-12 18:45:47 GMT
12-11-2004