Search in sources :

Example 1 with SelectElement

use of com.teamdev.jxbrowser.dom.SelectElement in project JxBrowser-Examples by TeamDev-IP.

the class DomSelectOption 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("DOM Select Option");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().on(FrameLoadFinished.class, event -> browser.mainFrame().flatMap(Frame::document).flatMap(Document::documentElement).flatMap(element -> element.findElementById("select-tag")).ifPresent(selectElement -> {
        Object[] options = ((SelectElement) selectElement).options().toArray();
        ((OptionElement) options[2]).select();
        System.out.println(selectElement.innerHtml());
    }));
    browser.mainFrame().ifPresent(mainFrame -> {
        mainFrame.loadHtml("<html><body><select id='select-tag'>\n" + "  <option value=\"volvo\">Volvo</option>\n" + "  <option value=\"saab\">Saab</option>\n" + "  <option value=\"opel\">Opel</option>\n" + "  <option value=\"audi\">Audi</option>\n" + "</select></body></html>");
    });
}
Also used : Document(com.teamdev.jxbrowser.dom.Document) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Frame(com.teamdev.jxbrowser.frame.Frame) SelectElement(com.teamdev.jxbrowser.dom.SelectElement) SwingUtilities(javax.swing.SwingUtilities) FrameLoadFinished(com.teamdev.jxbrowser.navigation.event.FrameLoadFinished) Browser(com.teamdev.jxbrowser.browser.Browser) OptionElement(com.teamdev.jxbrowser.dom.OptionElement) HARDWARE_ACCELERATED(com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) Engine(com.teamdev.jxbrowser.engine.Engine) WindowConstants(javax.swing.WindowConstants) SelectElement(com.teamdev.jxbrowser.dom.SelectElement) Frame(com.teamdev.jxbrowser.frame.Frame) JFrame(javax.swing.JFrame) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)1 Document (com.teamdev.jxbrowser.dom.Document)1 OptionElement (com.teamdev.jxbrowser.dom.OptionElement)1 SelectElement (com.teamdev.jxbrowser.dom.SelectElement)1 Engine (com.teamdev.jxbrowser.engine.Engine)1 HARDWARE_ACCELERATED (com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED)1 Frame (com.teamdev.jxbrowser.frame.Frame)1 FrameLoadFinished (com.teamdev.jxbrowser.navigation.event.FrameLoadFinished)1 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)1 BorderLayout (java.awt.BorderLayout)1 JFrame (javax.swing.JFrame)1 SwingUtilities (javax.swing.SwingUtilities)1 WindowConstants (javax.swing.WindowConstants)1