0

Estou testando criar um webapp em .jar para desktop mesmo, usando a ferramenta de amostras Webview do NETBEANS - JavaFX. O código é o apresentado abaixo. Estou querendo apenas uma forma de retirar o campo de endereço e o botão "GO", deixando a janela apenas com o conteúdo da página do blog, por exemplo.

package webviewbrowser;

import java.util.List;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewBrowser extends Application {

    @Override public void start(Stage primaryStage) throws Exception {
        Pane root = new WebViewPane();
        primaryStage.setScene(new Scene(root, 1024, 768));
        primaryStage.show();
    }


     public static void main(String[] args) {
        launch(args);
    }


    public class WebViewPane extends Pane {

        public WebViewPane() {
            VBox.setVgrow(this, Priority.ALWAYS);
            setMaxWidth(Double.MAX_VALUE);
            setMaxHeight(Double.MAX_VALUE);

            WebView view = new WebView();
            view.setMinSize(500, 400);
            view.setPrefSize(500, 400);
            final WebEngine eng = view.getEngine();
            eng.load("http://www.blogmarciocunha.com.br");
            final TextField locationField = new TextField("http://www.blogmarciocunha.com.br");
            locationField.setMaxHeight(Double.MAX_VALUE);
            Button goButton = new Button("Go");
            goButton.setDefaultButton(true);
            EventHandler<ActionEvent> goAction = new EventHandler<ActionEvent>() {
                @Override public void handle(ActionEvent event) {
                    eng.load(locationField.getText().startsWith("http://") ? locationField.getText() :
                            "http://" + locationField.getText());
                }
            };
            goButton.setOnAction(goAction);
            locationField.setOnAction(goAction);
            eng.locationProperty().addListener(new ChangeListener<String>() {
                @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                    locationField.setText(newValue);
                }
            });
            GridPane grid = new GridPane();
            grid.setVgap(5);
            grid.setHgap(5);
            GridPane.setConstraints(locationField, 0, 0, 1, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.SOMETIMES);
            GridPane.setConstraints(goButton,1,0);
            GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
            grid.getColumnConstraints().addAll(
                    new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true),
                    new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true)
            );
            grid.getChildren().addAll(locationField, goButton, view);
            getChildren().add(grid);
        }

        @Override protected void layoutChildren() {
            List<Node> managed = getManagedChildren();
            double width = getWidth();
            double height = getHeight();
            double top = getInsets().getTop();
            double right = getInsets().getRight();
            double left = getInsets().getLeft();
            double bottom = getInsets().getBottom();
            for (int i = 0; i < managed.size(); i++) {
                Node child = managed.get(i);
                layoutInArea(child, left, top,
                               width - left - right, height - top - bottom,
                               0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
            }
        }
    }
}

1 Resposta 1

0

Basta remover as linhas que adicionam os elementos ao WebViewPane. Como não utilizará mais esses controles, não faz sentido manter os trechos em que é criada as instâncias e definido os eventos, então isso também pode ser removido.

Se quer somente uma cena com o conteúdo da página web, isso basta:

// Omitindo imports.

public class StackOverflow extends Application {

   public static void main(String... args){
      launch(args);
   }

   @Override
   public void start(Stage primaryStage){

      final WebView webView = new WebView();
      final WebEngine webEngine = webView.getEngine();
      webEngine.load("http://www.blogmarciocunha.com.br");

      BorderPane borderPane = new BorderPane();
      borderPane.setCenter(webView);

      primaryStage.setScene(new Scene(borderPane, 1024, 768));
      primaryStage.setMaximized(true);
      primaryStage.setVisible(true);
   }  
}

Não lembro se BorderPane possui barra de scroll... se o código acima não permitir que role a página, crie um ScrollPane para inserir o webView:

// ...

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(webView);

BorderPane borderPane = new BorderPane();
borderPane.setCenter(scrollPane);

primaryStage.setScene(new Scene(borderPane, 1024, 768));

// ...
2
  • Como ficaria o código com o scrollpane? 21/06/2016 às 9:39
  • Igual ao primeiro, porém embaixo de webEngine.load(...); voce substitui pelo código que coloquei embaixo. 21/06/2016 às 12:01

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .