O JavaFX para de abrir o URL no WebView - abra no navegador
O navegador WebView incorporado que estou usando precisa de tratamento especial para determinadas URLs, para abri-las no navegador padrão nativo em vez de WebView. A parte de navegação real funciona bem, mas eu preciso parar o WebView de exibir essa página também. Eu posso pensar em várias maneiras de fazer isso, mas nenhuma delas funciona. Aqui está o meu código:
this.wv.getEngine().locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue)
{
Desktop d = Desktop.getDesktop();
try
{
URI address = new URI(observable.getValue());
if ((address.getQuery() + "").indexOf("_openmodal=true") > -1)
{
// wv.getEngine().load(oldValue); // 1
// wv.getEngine().getLoadWorker().cancel(); // 2
// wv.getEngine().executeScript("history.back()"); // 3
d.browse(address);
}
}
catch (IOException | URISyntaxException e)
{
displayError(e);
}
}
});
Um pouco mais de informação sobre o que acontece em cada um dos três casos
1. Carregando o endereço anteriorwv.getEngine().load(oldValue);
Isso mata a JVM. Curiosamente, a página abre bem no navegador nativo.
# A fatal error has been detected by the Java Runtime Environment:
#
# EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x000000005b8fef38, pid=7440, tid=8000
#
# JRE version: 7.0_09-b05
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.5-b02 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# C [jfxwebkit.dll+0x2fef38] Java_com_sun_webpane_platform_BackForwardList_bflItemGetIcon+0x184f58
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Greg Balaga\eclipse\Companyapp\hs_err_pid7440.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
2. Cancelando o trabalhadorwv.getEngine().getLoadWorker().cancel();
Não faz nada, a página é carregada no WebView e no navegador nativo.
3. Usando history.back ()wv.getEngine().executeScript("history.back()");
O mesmo que acima, sem efeito.
4. Reagindo às mudanças do Palco ao invésEu também tentei em vez de olhar olocationProperty
doWebEngine
, ouça em chenges parastateProperty
doWorker
e disparar o mesmo código de abertura senewState == State.SCHEDULED
. Não houve diferença no resultado do método anterior (além de não ser realmente capaz de usar # 1).
O código que estou usando agora ainda trava a JVM:
this.wv.getEngine().locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, final String oldValue, String newValue)
{
Desktop d = Desktop.getDesktop();
try
{
URI address = new URI(newValue);
if ((address.getQuery() + "").indexOf("_openmodal=true") > -1)
{
Platform.runLater(new Runnable() {
@Override
public void run()
{
wv.getEngine().load(oldValue);
}
});
d.browse(address);
}
}
catch (IOException | URISyntaxException e)
{
displayError(e);
}
}
});
Solução alternativaOk, eu consegui fazer isso derrubar a webview e reconstruí-la.
this.wv.getEngine().locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, final String oldValue, String newValue)
{
Desktop d = Desktop.getDesktop();
try
{
URI address = new URI(newValue);
if ((address.getQuery() + "").indexOf("_openmodal=true") > -1)
{
Platform.runLater(new Runnable() {
@Override
public void run()
{
grid_layout.getChildren().remove(wv);
wv = new WebView();
grid_layout.add(wv, 0, 1);
wv.getEngine().load(oldValue);
}
});
d.browse(address);
}
}
catch (IOException | URISyntaxException e)
{
displayError(e);
}
}
});