use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class PrintSettings method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
browser.set(PrintCallback.class, (params, tell) -> tell.print());
// #docfragment "Callback"
browser.set(PrintHtmlCallback.class, (params, tell) -> {
// #docfragment "Configure settings"
SystemPrinter<HtmlSettings> printer = params.printers().defaultPrinter().orElseThrow(IllegalStateException::new);
PrintJob<HtmlSettings> printJob = printer.printJob();
printJob.settings().paperSize(ISO_A4).colorModel(COLOR).enablePrintingBackgrounds().disablePrintingHeaderFooter().orientation(PORTRAIT).apply();
// #enddocfragment "Configure settings"
// #docfragment "Subscribe to PrintCompleted"
printJob.on(PrintCompleted.class, event -> {
if (event.isSuccess()) {
System.out.println("Printing is completed successfully.");
} else {
System.out.println("Printing has failed.");
}
});
// #enddocfragment "Subscribe to PrintCompleted"
// #docfragment "Proceed"
tell.proceed(printer);
// #enddocfragment "Proceed"
});
// #enddocfragment "Callback"
browser.navigation().loadUrlAndWait("https://google.com");
browser.mainFrame().ifPresent(Frame::print);
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class RedirectLoggingToFile method main.
public static void main(String[] args) {
Path loggingDir;
try {
loggingDir = Files.createTempDirectory("jxbrowser-logs");
} catch (IOException e) {
throw new RuntimeException("Failed to create a temporary directory", e);
}
Path loggingFile = loggingDir.resolve("jxbrowser.log");
System.setProperty("jxbrowser.logging.file", loggingFile.toAbsolutePath().toString());
System.out.println("Log file path: " + loggingFile.toAbsolutePath());
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Redirect Logging To File");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().loadUrl("https://www.google.com");
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class SelectClientCertificate method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Select Client SSL Certificate");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.set(SelectClientCertificateCallback.class, (params, tell) -> {
if (params.certificates().size() != 0) {
List<Certificate> certificates = params.certificates();
List<X509Certificate> x509Certificates = new ArrayList<>();
for (Certificate certificate : certificates) {
try {
x509Certificates.add(X509Certificates.of(new ByteArrayInputStream(certificate.derEncodedValue())));
} catch (CertificateException e) {
e.printStackTrace();
}
}
Object[] selectionValues = x509Certificates.toArray();
Object selectedValue = JOptionPane.showInputDialog(view, String.format(DIALOG_MESSAGE, params.hostPort().host() + ":" + params.hostPort().port()), DIALOG_TITLE, PLAIN_MESSAGE, null, selectionValues, selectionValues[0]);
if (selectedValue != null) {
// Tell the engine which SSL certificate has been selected.
try {
X509Certificate certificate = (X509Certificate) selectedValue;
ClientCertificate clientCertificate = ClientCertificate.of(certificate, SslPrivateKey.of(certificate.getEncoded()));
tell.select(clientCertificate);
return;
} catch (CertificateEncodingException e) {
e.printStackTrace();
}
}
}
tell.cancel();
});
browser.navigation().loadUrl("https://client.badssl.com/");
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class SelectionAsHtml method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JButton button = new JButton("Get Selected HTML");
button.addActionListener(e -> browser.mainFrame().ifPresent(frame -> SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(view, frame.selectionAsHtml(), "Selected HTML", JOptionPane.PLAIN_MESSAGE))));
JFrame frame = new JFrame("Get Selected HTML");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(button, BorderLayout.NORTH);
frame.add(view, BorderLayout.CENTER);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().loadUrl("https://www.teamdev.com/jxbrowser#features");
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class SpellCheckEvents method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Spell Check Events");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.on(SpellCheckCompleted.class, event -> event.results().forEach(checkResult -> {
System.out.println("Error start index: " + checkResult.location());
System.out.println("Error length: " + checkResult.length());
}));
browser.mainFrame().ifPresent(mainFrame -> {
mainFrame.loadHtml("<html><body>" + "<textarea autofocus rows='20' cols='30'>Smple text with mitake.</textarea>" + "</body></html>");
});
}
Aggregations