Click here to Skip to main content
15,902,114 members
Home / Discussions / Java
   

Java

 
AnswerRe: about java Pin
Richard MacCutchan7-Feb-12 4:55
mveRichard MacCutchan7-Feb-12 4:55 
AnswerRe: about java Pin
TorstenH.7-Feb-12 20:22
TorstenH.7-Feb-12 20:22 
Questionset database value to java script var on Js file Pin
swapnil666-Feb-12 1:48
swapnil666-Feb-12 1:48 
AnswerRe: set database value to java script var on Js file Pin
David Skelly6-Feb-12 22:12
David Skelly6-Feb-12 22:12 
QuestionRe: set database value to java script var on Js file Pin
swapnil667-Feb-12 22:53
swapnil667-Feb-12 22:53 
QuestionHow to manipulate voice information??????? Pin
Member 86168852-Feb-12 10:28
Member 86168852-Feb-12 10:28 
AnswerRe: How to manipulate voice information??????? Pin
gebri2-Feb-12 21:32
gebri2-Feb-12 21:32 
Questionprint a report sourced by html page Pin
ceyhunerdil1-Feb-12 21:40
ceyhunerdil1-Feb-12 21:40 
I'm working on a system and we have a problem on printing. System send an html content to the print class and we could not get a proper print page. I'm writing my codes. Please have a look..(sorry about my bad english)

PrintableEditorPane pane = new PrintableEditorPane(
html.getText());
pane.setContentType("text/html");

EditorPanePrinter pnl = new EditorPanePrinter(pane, new Paper(), new Insets(18, 18, 18, 18));

pnl.print();

---and this is my printing class


package org.esse.utility.image;

import javax.swing.*;
import javax.swing.text.View;
import javax.swing.text.Position;
import javax.print.PrintService;
import java.awt.print.*;
import java.awt.*;
import java.awt.geom.Area;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;

public class EditorPanePrinter extends JPanel implements Pageable, Printable {

JEditorPane sourcePane;
Paper paper;
Insets margins;
ArrayList<pagepanel> pages;
int pageWidth;
int pageHeight;
View rootView;
public static int PAGE_SHIFT = 20;
PageFormat pageFormat;

public EditorPanePrinter(JEditorPane pane, Paper paper, Insets margins) {
initData(pane, paper, margins);
}

public void initData(JEditorPane pane, Paper paper, Insets margins) {
JEditorPane tmpPane = new JEditorPane();
tmpPane.setEditorKit(pane.getEditorKit());
tmpPane.setContentType(pane.getContentType());
tmpPane.setText(pane.getText());

this.sourcePane = tmpPane;

this.paper = paper;
this.margins = margins;
this.pageWidth = (int) paper.getWidth();
this.pageHeight = (int) paper.getHeight();
pageFormat = new PageFormat();
paper.setImageableArea(0, 0, paper.getWidth(), paper.getHeight() );
pageFormat.setPaper(paper);

doPagesLayout();
}

public void doPagesLayout() {
setLayout(null);
removeAll();
this.rootView = sourcePane.getUI().getRootView(sourcePane);

sourcePane.setSize(pageWidth - margins.top - margins.bottom, Integer.MAX_VALUE);
Dimension d = sourcePane.getPreferredSize();
sourcePane.setSize(pageWidth - margins.top - margins.bottom, d.height);

calculatePageInfo(); int count = pages.size();

this.setPreferredSize(new Dimension(pageWidth * 2 + 50, PAGE_SHIFT + count * (pageHeight + PAGE_SHIFT)));
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);

AffineTransform old = ((Graphics2D) g).getTransform();

((Graphics2D) g).setTransform(old);
}

protected void calculatePageInfo() {
pages = new ArrayList<pagepanel>();
int startY = 0;
int endPageY = getEndPageY(startY);
while (startY+pageHeight-margins.top-margins.bottom <
sourcePane.getHeight() * ( pageWidth /
sourcePane.getPreferredSize().getWidth()) / 1.8) {
Shape pageShape = getPageShape(startY, pageWidth - margins.left - margins.right, pageHeight - margins.top - margins.bottom, sourcePane);
pages.add(new PagePanel(startY, endPageY, pageShape));
startY = endPageY;
endPageY = getEndPageY(startY);
}
Shape pageShape = getPageShape(startY, pageWidth - margins.left - margins.right, pageHeight - margins.top - margins.bottom, sourcePane);
pages.add(new PagePanel(startY, endPageY, pageShape));

int count = 0;

for (PagePanel pi : pages) {
add(pi);
pi.setLocation(PAGE_SHIFT, PAGE_SHIFT + count * (pageHeight + PAGE_SHIFT));
count++;
}
}

protected int getEndPageY(int startY) {
int desiredY = startY + pageHeight - margins.top - margins.bottom;
int realY = desiredY;
for (int x = 1; x < pageWidth; x++) {
View v = getLeafViewAtPoint(new Point(x, realY), rootView);
if (v != null) {
Rectangle alloc = PrintApp.getAllocation(v, sourcePane).getBounds();
if (alloc.height > pageHeight - margins.top - margins.bottom) {
continue;
}
if (alloc.y + alloc.height > desiredY) {
realY = Math.min(realY, alloc.y);
}
}
}

return realY;
}

protected View getLeafViewAtPoint(Point p, View root) {
return getLeafViewAtPoint(p, root, sourcePane);
}

public static View getLeafViewAtPoint(Point p, View root, JEditorPane sourcePane) {
int pos = sourcePane.viewToModel(p);
View v = sourcePane.getUI().getRootView(sourcePane);
while (v.getViewCount() > 0) {
int i = v.getViewIndex(pos, Position.Bias.Forward);
v = v.getView(i);
}
Shape alloc = PrintApp.getAllocation(root, sourcePane);
if (alloc.contains(p)) {
return v;
}

return null;
}

public static Shape getPageShape(int pageStartY, int pageWidth, int pageHeight, JEditorPane sourcePane) {
Area result = new Area(new Rectangle(0, 0, pageWidth, pageHeight));
View rootView = sourcePane.getUI().getRootView(sourcePane);
Rectangle last = new Rectangle();
for (int x = 1; x < pageWidth; x++) {
View v = getLeafViewAtPoint(new Point(x, pageStartY), rootView, sourcePane);
if (v != null) {
Rectangle alloc = PrintApp.getAllocation(v, sourcePane).getBounds();
if (alloc.y < pageStartY && alloc.y + alloc.height > pageStartY) {
if (!alloc.equals(last)) {
Rectangle r = new Rectangle(alloc);
r.y -= pageStartY;
result.subtract(new Area(r));
}
}
last = alloc;
}
}

last = new Rectangle();
for (int x = 1; x < pageWidth; x++) {
View v = getLeafViewAtPoint(new Point(x, pageStartY + pageHeight), rootView, sourcePane);
if (v != null) {
Rectangle alloc = PrintApp.getAllocation(v, sourcePane).getBounds();
if (alloc.y < pageStartY + pageHeight && alloc.y + alloc.height > pageStartY + pageHeight) {
if (!alloc.equals(last)) {
Rectangle r = new Rectangle(alloc);
r.y -= pageStartY;
result.subtract(new Area(r));
}
}
last = alloc;
}
}

return result;
}
//pageable methods

public int getNumberOfPages() {

return pages.size();
}

public PageFormat getPageFormat(int pageIndex) throws IndexOutOfBoundsException {
return pageFormat;
}

public Printable getPrintable(int pageIndex) throws IndexOutOfBoundsException {
return this;
}

public int print(Graphics g, PageFormat pageFormat, int pageIndex)
throws PrinterException {
if (pageIndex < pages.size()) {
pageFormat.getPaper().setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
pages.get(pageIndex).isPrinting = true;
pages.get(pageIndex).paint(g);
pages.get(pageIndex).paintAll(g);
pages.get(pageIndex).isPrinting = false;


return PAGE_EXISTS;
}

return NO_SUCH_PAGE;
}

class PagePanel extends JPanel {

int pageStartY;
int pageEndY;
Shape pageShape;
boolean isPrinting = false;
JPanel innerPage = new JPanel() {

public void paintComponent(Graphics g) {
super.paintComponent(g);

AffineTransform old = ((Graphics2D) g).getTransform();
//double scales = pageWidth
/ sourcePane.getPreferredSize().getWidth();

//scales = scales / 1.8;
Shape oldClip = g.getClip();

Area newClip = new Area(oldClip);
if (isPrinting) {
newClip = new Area(pageShape);
} else {
newClip.intersect(new Area(pageShape));
}
g.setClip(newClip);

g.translate(0, -pageStartY);

//((Graphics2D) g).scale(scales, scales);
sourcePane.paint(g);
for (Component c : sourcePane.getComponents()) {
AffineTransform tmp = ((Graphics2D) g).getTransform();
g.translate(c.getX(), c.getY());
((Container) c).getComponent(0).paint(g);
((Graphics2D) g).setTransform(tmp);

}

((Graphics2D) g).setTransform(old);
g.setClip(oldClip);
}
};

public PagePanel() {
this(0, 0, null);
}

public PagePanel(int pageStartY, int pageEndY, Shape pageShape) {
this.pageStartY = pageStartY;
this.pageEndY = pageEndY;
this.pageShape = pageShape;


setSize(pageWidth, pageHeight);
setBackground(Color.white);
setLayout(null);
add(innerPage);
innerPage.setBounds(margins.left, margins.top, pageWidth - margins.left - margins.right, pageHeight - margins.top - margins.bottom);
}

// public void paintComponent(Graphics g) {
// super.paintComponent(g);
// g.setColor(Color.black);
// g.drawRect(0, 0, getWidth() - 2, getHeight() - 2);
// }
}

public void print() {
print((PrintService) null);
}

public void print(PrintService ps) {
try {
PrinterJob pj = PrinterJob.getPrinterJob();
JFrame tmp = null;
if (this.getParent() == null) {
tmp = new JFrame();
tmp.getContentPane().add(new JScrollPane(this));
tmp.pack();
tmp.setVisible(false);
}
pj.setPageable(this);
if (ps != null) {
pj.setPrintService(ps);
}
// sending the document to the printer page
Boolean appliedPrint = pj.printDialog();
if(appliedPrint)

pj.print();

if (tmp != null) {
tmp.dispose();
}
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
}
AnswerRe: print a report sourced by html page Pin
David Skelly1-Feb-12 22:04
David Skelly1-Feb-12 22:04 
QuestionComparing Strings Reverse if-conditional Pin
johtnkucz1-Feb-12 7:37
johtnkucz1-Feb-12 7:37 
AnswerRe: Comparing Strings Reverse if-conditional Pin
Richard MacCutchan1-Feb-12 8:58
mveRichard MacCutchan1-Feb-12 8:58 
GeneralRe: Comparing Strings Reverse if-conditional Pin
johtnkucz1-Feb-12 11:40
johtnkucz1-Feb-12 11:40 
GeneralRe: Comparing Strings Reverse if-conditional Pin
Richard MacCutchan1-Feb-12 12:06
mveRichard MacCutchan1-Feb-12 12:06 
GeneralRe: Comparing Strings Reverse if-conditional Pin
johtnkucz1-Feb-12 12:26
johtnkucz1-Feb-12 12:26 
GeneralRe: Comparing Strings Reverse if-conditional Pin
Richard MacCutchan1-Feb-12 22:19
mveRichard MacCutchan1-Feb-12 22:19 
GeneralRe: Comparing Strings Reverse if-conditional Pin
johtnkucz3-Feb-12 4:08
johtnkucz3-Feb-12 4:08 
GeneralRe: Comparing Strings Reverse if-conditional Pin
Richard MacCutchan3-Feb-12 4:35
mveRichard MacCutchan3-Feb-12 4:35 
AnswerRe: Comparing Strings Reverse if-conditional Pin
Dinu_613-Feb-12 22:24
Dinu_613-Feb-12 22:24 
Questionhow to get a value in to JS file from java servlet Pin
swapnil661-Feb-12 7:14
swapnil661-Feb-12 7:14 
AnswerRe: how to get a value in to JS file from java servlet Pin
chakki2921-Feb-12 20:57
chakki2921-Feb-12 20:57 
QuestionHow to displaly progress bar for downloading attachment Pin
R_K1-Feb-12 4:05
R_K1-Feb-12 4:05 
AnswerRe: How to displaly progress bar for downloading attachment Pin
Nagy Vilmos1-Feb-12 6:00
professionalNagy Vilmos1-Feb-12 6:00 
GeneralRe: How to displaly progress bar for downloading attachment Pin
R_K2-Feb-12 1:54
R_K2-Feb-12 1:54 
GeneralRe: How to displaly progress bar for downloading attachment Pin
TorstenH.2-Feb-12 2:42
TorstenH.2-Feb-12 2:42 
GeneralRe: How to displaly progress bar for downloading attachment Pin
R_K6-Feb-12 2:50
R_K6-Feb-12 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.