Search code examples
javabrowserjeditorpane

JEditorPane does not work properly, if i implement it, then everything goes wrong


I am in chalange with my friend to make a better program, he decided to make a pain tool, while i decided to make a web browser. I am currently trying to implement the fields. if i add an address bar and a button everything works as told, but when i put a JEditorPane then the display does not show what it is told to show.

CODE with no JEditorPane(everything works):

import java.awt.*;
import javax.swing.*;
public class browserPannel
{
    public static void main(String[] arg)
    {
        JFrame browser = new JFrame("A Nun In A Weelchair");
        browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        browser.setLocationRelativeTo(null);
        browser.setSize(1000,700);
        browser.setVisible(true);
        browser.setResizable(false);

        JTextField url = new JTextField();
        url.setSize(890,30);
        url.setVisible(true);
        url.setLocation(15,15);

        JPanel holder = new JPanel();
        holder.setBackground(Color.lightGray);
        holder.setSize(1000,700);

        JButton send = new JButton("Send");
        send.setSize(75,30);
        send.setVisible(true);
        send.setLocation(906,15);

        //JEditorPane htmlc = new JEditorPane();
        //htmlc.setBackground(Color.red);
        //htmlc.setEditable(true);
        //htmlc.setContentType("text/html");
        //htmlc.setSize(500,500);
        //htmlc.setVisible(true);
        //htmlc.setLocation(15,50);




        holder.add(url);
        holder.add(send);
        //holder.add(htmlc);
        browser.getContentPane().add(holder);
    }
}

IMAGE With no JEditorPanehttp, everything works as told: https://i.sstatic.net/hONs3.png

CODE With JEditorPane (everything does not work as told):

import java.awt.*;
import javax.swing.*;
public class browserPannel
{
    public static void main(String[] arg)
    {
        JFrame browser = new JFrame("A Nun In A Weelchair");
        browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        browser.setLocationRelativeTo(null);
        browser.setSize(1000,700);
        browser.setVisible(true);
        browser.setResizable(false);

        JTextField url = new JTextField();
        url.setSize(890,30);
        url.setVisible(true);
        url.setLocation(15,15);

        JPanel holder = new JPanel();
        holder.setBackground(Color.lightGray);
        holder.setSize(1000,700);

        JButton send = new JButton("Send");
        send.setSize(75,30);
        send.setVisible(true);
        send.setLocation(906,15);

        JEditorPane htmlc = new JEditorPane();
        htmlc.setBackground(Color.red);
        htmlc.setEditable(true);
        htmlc.setContentType("text/html");
        htmlc.setSize(500,500);
        htmlc.setVisible(true);
        htmlc.setLocation(15,50);




        holder.add(url);
        holder.add(send);
        holder.add(htmlc);
        browser.getContentPane().add(holder);
    }
}

IMAGE With JEditorPane, everything does not work as told: https://i.sstatic.net/3ayeP.png

I Tried removing some of the .set's and i hav tried looking onine, i have also tried only running the JEditorPpane on its own, but i cant seem to set variables for it(such as location, size, etc.)


Solution

  • You are getting problems because the FlowLayout manager, which is the default layout manager for JPanel is making its own decisions about how the holder panel should be managed and it's contents laid out.

    • Make use of appropriate layouts. See Laying Out Components Within a Container for more details
    • Start your UI within the context of the Event Dispatching Thread. See Initial Threads for more details
    • Layout, pack and then make your frame visible.
    • With content which is likely to exceeded the visible requirements of your window, you should make use of JScrollPanes. See How to Use Scroll Panes for more details

    Editor Window

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    
    public class BrowserPannel {
    
        public static void main(String[] arg) {
            JFrame browser = new JFrame("A Nun In A Weelchair");
            browser.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            browser.setLocationRelativeTo(null);
    
            JTextField url = new JTextField(20);
    
            JPanel header = new JPanel();
            header.setBackground(Color.lightGray);
    
            JButton send = new JButton("Send");
    
            JEditorPane htmlc = new JEditorPane();
            htmlc.setBackground(Color.red);
            htmlc.setEditable(true);
            htmlc.setContentType("text/html");
    
            header.add(url);
            header.add(send);
            browser.getContentPane().add(header, BorderLayout.NORTH);
            browser.getContentPane().add(new JScrollPane(htmlc));
    
            browser.pack();
            browser.setVisible(true);
    
        }
    }