(JAVA) Could not apply AutoFit feature

Hello,

I'm using Cells for Java ver 1.7.5

Could not apply autofit for columns using the following code:
Worksheet worksheet = workbook.getWorksheets().addSheet(DATA_SHEET_NAME);
worksheet.setZoom(85); // set worksheet zoom
/* add rows to worksheet and so on ... */
worksheet.autoFitColumns(0,columnsAmount);
/* save worksheet */
as the result all columns are not extended to the appropriate size.
How can I fix this?

WBR

Tyrex

Hi Tyrex,

Please try this code it works fine.

Workbook wkb = new Workbook();
// add sheet
Worksheet wks = wkb.getWorksheets().addSheet("add new");
// add data
wks.getCells().getCell(0,0).setValue("aa");
wks.getCells().getCell(0,1).setValue("aaaaaaaaaaaaaaaaaa");
wks.setZoom(85);
wks.autoFitColumns(0, 100);

// Save the workbook
wkb.save( "E:\\temp\\testAutoFitter_out.xls" );

If the problem still exists, kindly post us here your created file(s)
with your sample code. So, that we may check and resolve the issue soon.

Thank you.

Hi Karen

I tried your code and received the following exception on the line wks.autoFitColumns(0,100):

java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:121)
at java.awt.Label.(Label.java:133)
at java.awt.Label.(Label.java:100)
at com.aspose.cells.at.(Unknown Source)
at com.aspose.cells.h.a(Unknown Source)
at com.aspose.cells.h.(Unknown Source)
at com.aspose.cells.Worksheet.j(Unknown Source)
at com.aspose.cells.Worksheet.autoFitColumns(Unknown Source)

So I couldn't post my created files :(

After that I've updated my version to 1.8.0.0 but the same exception appears.

Could you help?

Thanks

Tyrex

Hi Tyrex,

Here is the definition of java.awt.HeadlessException in JDK API SPEC:
"Thrown when code that is dependent on a keyboard, display,
or mouse is called in an environment that does not support a keyboard, display, or mouse. "

In order to get font display size, Aspose.Cells calls some java.awt class to get graphics environment setting.
If the Java environment does not support such an action, java.awt.HeadlessException is thrown out.
You can use the following command line to run the application:
java -Djava.awt.headless=true

The following links have some info about headless environment. Just open the pages and search "headless".
http://java.sun.com/developer/technicalArticles/J2SE/Desktop/headless/#headlessexception
http://java.sun.com/products/java-media/2D/reference/faqs/index.html

If the problem still exists, please post your running environment info, such as the xServer and the JRE version.
So, that we may check and resolve the issue.

Thank you.


I’m having the same problem. I did include -Djava.awt.headless=true and I still get:

java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159)
at java.awt.Label.(Label.java:133)
at java.awt.Label.(Label.java:100)
at com.aspose.cells.bJ.(Unknown Source)
at com.aspose.cells.n.a(Unknown Source)
at com.aspose.cells.n.(Unknown Source)
at com.aspose.cells.Worksheet.j(Unknown Source)
at com.aspose.cells.Worksheet.autoFitRows(Unknown Source)

followed by “my” stacktrace.

This is being run from a command-line and doesn’t include any explicit graphics. I’m using /usr/java/jdk1.5.0_06. It’s on Linux. Is there any other runtime info you need?

Hi gvgriffin,

In order to get font display size, AutoFit methods call some java.awt class to get graphics environment setting.

Please check if the following code can be run in your environment:

Label lab = new Label();

Can you please tell me your linux version? Is there X windows in your Linux? Is X windows running when you run the code?

Thank you.

When I entered uname -a I got this info:
Linux spyamapp 2.6.9-34.ELlargesmp #1 SMP Fri Feb 24 17:06:55 EST 2006 x86_64 x86_64 x86_64 GNU/Linux

I don’t know if that machine is running X, but it gets a HeadlessException when I try to run.

I tried on my machine, which gives me this uname info:
Linux gryphon 2.6.9-11.ELsmp #1 SMP Fri May 20 18:26:27 EDT 2005 i686 i686 i386 GNU/Linux

I connect to this machine using Reflection X on my PC and do my development using lots of windows and MyEclipse. It gets the HeadlessException when I start it in from the command line in a terminal window but does not when I start it under MyEclipse.

Hi gvgriffin,

It seems that the error is not a java error but an environment error. If you are RLOGIN into some other machine, please try to set the DISPLAY back to your machine and then run your code. Suppose your host address is 192.168.2.2, you can set the display:

Under C shell: setenv DISPLAY 192.168.2.2:0.0
Under Korn: export DISPLAY=192.168.2.2:0.0

If it doesn’t work, please change “192.168.2.2” to “127.0.0.1” or that machine’s address and try again.

Thank you.

The DISPLAY environment variable is set correctly when I run from the command line on my machine. Also, our application machines DO NOT use a graphics environment. The DISPLAY variable is explicitly NOT set in the production environment.

Hi gvgriffin,

Since simple code like “Label lab = new Label();” still occurs error in your environment, it seems that the error is an environment error but not our product error. I am working around to solve this error. Hopefully it can be solved soon.

Please help me confirm the following information:

  • You are using a PC and the OS is Linux with graphics environment
  • The application machine’s OS is Linux without graphics environment
  • You connect to the application machine using Reflection X, then open a command line window in the terminal, set the DISPLAY environment variable back to your machine and run java application with “Label lab = new Label();” in the same window, but HeadlessException is thrown out
  • You connect to the application machine using Reflection X, then start MyEclipse in the terminal and run java application with “Label lab = new Label();” without error
  • JRE and MyEclipse is installed in the application machine

Thank you

The java.awt.Label documentation for Java 1.5 indicates that it throws a HeadlessException in the headless environment. That environment is set automatically if it can’t find a display or if the user explicitly sets java.awt.headless.

If the autofit code requires instances of Label, it cannot work in a headless environment. Do you have any workaround that would let me autofit my file in a headless environment?

MyEclipse is only installed in the development Linux machine. When I run under MyEclipse, I am not running in a headless environment. In other cases, I need to run under a headless environment.

The PC only acts as an X Server as far as this problem is concerned. I only mentioned it to validate to you that the Linux box is set up to run as an X Client. I have the same thing happen using the monitor directly connected to the Linux box.

Hi gvgriffin,

Please try this fix. javax.swing.JLabel has taken the place of java.awt.Label in AutoFit method. Hopefully it will work.

Thank you.

I didn’t get the stacktrace so I assume it works. (I didn’t rip out the explicit calculation code, but I’ve used a similar trick before.)