How to position a browser within another Jframe interface?

Asked

Viewed 114 times

0

I am developing a project in Java to simulate a proxy server with authentication that presents a single interface, in which several internal frames are arranged inside it. How to position a browser within this interface JFrame?

Look at the design model:

Modelo do projeto

And the code:

\\Código do Browser
package view;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.html.*;

public class SwingHTMLBrowser extends JFrame implements ActionListener, HyperlinkListener {
    public JTextField addressBar;
    public JEditorPane pane;

    SwingHTMLBrowser() {

    super("Swing HTML Browser");

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addressBar = new JTextField();
        addressBar.addActionListener(this);
        pane = new JEditorPane();
        pane.setEditable(false);
        pane.addHyperlinkListener(this);
        add(addressBar, BorderLayout.NORTH);
        add(new JScrollPane(pane));
        setSize(new Dimension(400, 400));
    }

    public void actionPerformed(ActionEvent evt) {
        String url = addressBar.getText();
        try {
            pane.setPage(url);
        } catch (IOException t) {
            t.printStackTrace();
        }
    }

    @Override
    public void hyperlinkUpdate(HyperlinkEvent evt) {
        if (evt.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
            return;
        }
        JEditorPane srcPane = (JEditorPane)evt.getSource();
        if (evt instanceof HTMLFrameHyperlinkEvent) {
            HTMLDocument doc = (HTMLDocument)pane.getDocument();
            doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)evt);
        } else {
            String url = evt.getURL().toString();
            addressBar.setText(url);
            try {
                pane.setPage(url);
            } catch (Throwable t) {
                t.printStackTrace();
                }
            }
       }
    public static void main(String args[]) {
        SwingHTMLBrowser browser = new SwingHTMLBrowser();
        browser.setVisible(true);

    }

}
  • Welcome to Stackoverflow Vagner. And what is the result with this code you have today?

  • What do you want to do? Is it like it is in the picture? The code does not represent what is in the picture.

  • The code represents the browser. If tested it opens a Jframe screen. It is this screen that I intend to get inside the interface above.

  • Just create a Jpanel with all that browser interface and play in a jframe,

  • This is the problem, I cannot load this interface inside Jframe, when running it opens outside. If you test you can notice. There my knowledge in java is recent, so this degree of difficulty.

  • I don’t understand your doubt yet. Do you want to create an interface similar to the correct figure? If it is this is simple.

  • That’s right, this is the goal. It’s an Object-Oriented Programming project.

  • If you can help me, I can put the name "stackoverflow" as the main intermediary of the project and I will be very grateful.

Show 3 more comments

1 answer

2


I managed to do so:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

public class SwingHTMLBrowser extends JFrame {

    private JTextArea userStatus, serverStatus;
    private JPanel actions;


    SwingHTMLBrowser() {



        super("Swing HTML Browser");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setPreferredSize(new Dimension(600, 400));

        userStatus =  new JTextArea(5,5);
        JScrollPane userStatusPane = new JScrollPane(userStatus);
        userStatusPane.setBorder(BorderFactory.createTitledBorder("user Status"));
        userStatusPane.setPreferredSize(new Dimension(this.getPreferredSize().width/6, this.getPreferredSize().height));

        serverStatus = new JTextArea(5,5);
        JScrollPane serverStatusPane = new JScrollPane(serverStatus);
        serverStatusPane.setBorder(BorderFactory.createTitledBorder("server Status"));
        serverStatusPane.setPreferredSize(new Dimension(this.getPreferredSize().width/6, this.getPreferredSize().height));

        actions = new JPanel();
        actions.add(new JLabel("actions"));
        actions.setPreferredSize(new Dimension(this.getPreferredSize().width, 100));
        actions.setBorder(BorderFactory.createLineBorder(Color.red));

        add(actions, BorderLayout.NORTH);

        JPanel secondaryPane = new JPanel();
        secondaryPane.setLayout(new BoxLayout(secondaryPane, BoxLayout.X_AXIS));
        secondaryPane.add(userStatusPane);
        secondaryPane.add(serverStatusPane);
        secondaryPane.add(new Browser());

        add(secondaryPane, BorderLayout.CENTER);

        pack();
    }

    public static void main(String args[]) {

        SwingUtilities.invokeLater(() -> new SwingHTMLBrowser().setVisible(true));
    }
}

class Browser extends JPanel {

    private JTextField addressBar;
    private JEditorPane pane;

    public Browser() {
        setLayout(new BorderLayout());
        addressBar = new JTextField();
        pane = new JEditorPane();
        pane.setEditable(false);

        addressBar.addActionListener(e -> {
            String url = addressBar.getText();
            try {
                pane.setPage(url);
            } catch (IOException t) {
                t.printStackTrace();
            }

        });

        pane.addHyperlinkListener(evt -> {

            if (evt.getEventType() != HyperlinkEvent.EventType.ACTIVATED) {
                return;
            }

            JEditorPane srcPane = (JEditorPane) evt.getSource();

            if (evt instanceof HTMLFrameHyperlinkEvent) {
                HTMLDocument doc = (HTMLDocument) pane.getDocument();
                doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent) evt);
            } else {
                String url = evt.getURL().toString();
                addressBar.setText(url);
                try {
                    pane.setPage(url);
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        });
        add(addressBar, BorderLayout.NORTH);
        add(new JScrollPane(pane));
    }
}

Basically what I did was take advantage of the default layout manager of JFrame(Borderlayout) and use a secondary panel to distribute the 3 components vertically, also taking advantage of the standard layout of JPanel(FlowLayout) that organizes the components vertically, as shown in the figure.

Another modification is to create a class Browser, with a view to independent implementation.

Another important point to note is that all graphic applications involving the swing API should be dispatched to the EDT.

The result comes out:

inserir a descrição da imagem aqui

  • You’re the man. Thank you.

  • It worked, God bless you.

  • @Vagnersantos if the answer helped you, Oce can mark it as accepted by clicking on v next to her.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.