public class Formats {
public static NumberFormat numberformatter = new NumberFormat("#,###0.00");
public static WritableFont defaultfont = new WritableFont(WritableFont.TAHOMA, 10);
public static WritableFont headerfont = new WritableFont(WritableFont.TAHOMA, 10, WritableFont.BOLD);
private static WritableCellFormat number;
private static WritableCellFormat label;
private static WritableCellFormat PlainLabel;
private static WritableCellFormat header_dark;
private static WritableCellFormat header_light;
private Formats() {
}
/**
* @return the defaultfont
*/
public WritableFont getDefaultfont() {
return defaultfont;
}
/**
* @param defaultfont the defaultfont to set
*/
public void setDefaultfont(WritableFont defaultfont) {
this.defaultfont = defaultfont;
}
/**
* @return the headerfont
*/
public WritableFont getHeaderfont() {
return headerfont;
}
/**
* @param headerfont the headerfont to set
*/
public void setHeaderfont(WritableFont headerfont) {
this.headerfont = headerfont;
}
public static synchronized WritableCellFormat getNumber() throws WriteException {
if (number == null) {
number = new WritableCellFormat(defaultfont, numberformatter);
number.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return number;
}
public static synchronized WritableCellFormat getLabel() throws WriteException {
if (label == null) {
label = new WritableCellFormat(defaultfont);
label.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return label;
}
public static synchronized WritableCellFormat getPlainLabel() throws WriteException {
if (PlainLabel == null) {
PlainLabel = new WritableCellFormat(defaultfont);
}
return PlainLabel;
}
public static synchronized WritableCellFormat getHeader1() throws WriteException {
if (header_dark == null) {
header_dark = new WritableCellFormat(headerfont, numberformatter);
header_dark.setBackground(Colour.AQUA);
header_dark.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return header_dark;
}
public static synchronized WritableCellFormat getHeader2() throws WriteException {
if (header_light == null) {
header_light = new WritableCellFormat(headerfont, numberformatter);
header_light.setBackground(Colour.LIGHT_TURQUOISE);
header_light.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return header_light;
}
Ongelma on, että muotoilut eivät enää pysy, kun olio luodaan uudelleen. Apua kaivataan ASAP!
public static NumberFormat numberformatter = new NumberFormat("#,###0.00");
public static WritableFont defaultfont = new WritableFont(WritableFont.TAHOMA, 10);
public static WritableFont headerfont = new WritableFont(WritableFont.TAHOMA, 10, WritableFont.BOLD);
private static WritableCellFormat number;
private static WritableCellFormat label;
private static WritableCellFormat PlainLabel;
private static WritableCellFormat header_dark;
private static WritableCellFormat header_light;
private Formats() {
}
/**
* @return the defaultfont
*/
public WritableFont getDefaultfont() {
return defaultfont;
}
/**
* @param defaultfont the defaultfont to set
*/
public void setDefaultfont(WritableFont defaultfont) {
this.defaultfont = defaultfont;
}
/**
* @return the headerfont
*/
public WritableFont getHeaderfont() {
return headerfont;
}
/**
* @param headerfont the headerfont to set
*/
public void setHeaderfont(WritableFont headerfont) {
this.headerfont = headerfont;
}
public static synchronized WritableCellFormat getNumber() throws WriteException {
if (number == null) {
number = new WritableCellFormat(defaultfont, numberformatter);
number.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return number;
}
public static synchronized WritableCellFormat getLabel() throws WriteException {
if (label == null) {
label = new WritableCellFormat(defaultfont);
label.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return label;
}
public static synchronized WritableCellFormat getPlainLabel() throws WriteException {
if (PlainLabel == null) {
PlainLabel = new WritableCellFormat(defaultfont);
}
return PlainLabel;
}
public static synchronized WritableCellFormat getHeader1() throws WriteException {
if (header_dark == null) {
header_dark = new WritableCellFormat(headerfont, numberformatter);
header_dark.setBackground(Colour.AQUA);
header_dark.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return header_dark;
}
public static synchronized WritableCellFormat getHeader2() throws WriteException {
if (header_light == null) {
header_light = new WritableCellFormat(headerfont, numberformatter);
header_light.setBackground(Colour.LIGHT_TURQUOISE);
header_light.setBorder(Border.ALL, BorderLineStyle.THIN);
}
return header_light;
}
Ongelma on, että muotoilut eivät enää pysy, kun olio luodaan uudelleen. Apua kaivataan ASAP!
Vastaukset 6
Vanhimmat
pysyy hengissä niin kauan kun yksikin Formats-luokan olio on hengissä. Itse asiassa ne luodaan kun eka Formats- luodaan ja ne pysyy hengissä niin kauan, kun ohjelma on hengissä.
Koitas tehdä jonnekin yksi tuollainen Formats-olio ihan vaan istumaan. Teet sen sellaiseen luokkaan, joka pysyy käynnissä koko prosessisin ajan.
Kokeilin tulostaa yhden olion siellä, missä solu muodostetaan ja lisätään sheetiin (eli asetetaan solulle muotoilut ja sisältöarvo ja isketään se addcell-metodilla writablesheetiin). Sama muistiosoite tulostui kummallakin kerralla, mutta tokalla muotoilut olivat edelleen poissa. Tilanne nollautuu vasta, kun virtuaalikone sammuu.
Siis olio pysyy olemassa mutta mitä ihmettä sille tapahtuu?
Semmoinen tuli vielä mieleen, että saattaisko vika liittyä excel-näkymän renderöimiseen?
JExcelin faq löytyy maininta että cell formatterit eivät saa olla staattisia. Eli kokeileppa tuolla faqssa esitettyä tapaa tehdä toi solujen formatointi.
http://jexcelapi.sourceforge.net/resources/faq/ -> getting the error message 'too many different cell formats'
Saan virheilmoituksen non-static method cannot be referenced from a static context..
Eli joku staatttinen metodi yrittää kutsua olion metodia. Korjauksesta on vaikea sanoa mitään näkemättä koko koodia.