Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 682
  • Last Modified:

How to show formated text from a file including (with underlying background image) scrollbar therefore an a button below to confirm the stated text?

Hi I want to display a help window with Swing, this window should display a formated text with an image in the background and an "OK" Button down under to close the window, furthermore I need a Scrollbar to scrolldown the text.

At the moment I use an JEditorPane to load an HTML formated file which includes the background image in the source (bad because I would like to change the image by a parameter from Java). I couldn't get this working in connection with a LayoutManager to add the button.

The window should have the following layout:
_________________                                            _________________
| fomated text  | ^ |                  or                       | fomated text  | ^ |
|                     | b  |                                            |                     | b  |
|                     | a  |                                           |                     | a  |
|                     | r  |                                            |                     | r  |
| ___________ |    |                                            | ___________ | v  |
|      button      | v  |                                            |           button       |
------------------------                                             ------------------------
I hope you could help me with this problem, maybe with some code too ...

Thanks

gnurl

Here the code as at the moment:

    code:

    import java.awt.Dimension;
    import java.awt.Insets;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.IOException;
    import java.net.URL;

    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;

    public class Help extends JFrame{

    private static String path ="";
    private static int width=600, height=550;

    public void addHTML()
    {
    path="file:theHTMLStyledText.File";


    //prepare window
    setSize(width,height);
    setTitle("help");

    //prepare text
    JEditorPane text = new JEditorPane();
    text.setEditable(false);

    //create scrollbar
    JScrollPane scroll = new JScrollPane(text);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scroll.setPreferredSize(new Dimension(width-15,height-49));
    scroll.setMinimumSize(new Dimension(10,10));
    setContentPane(scroll);

    //set distance to frame to zero
    text.setMargin(new Insets(0,0,0,0));


    try
    {
    text.setPage(new URL(path));
    //text.setOpaque(false);
    }
    catch (IOException e)
    {
    System.out.println("error reading file" + path);
    }

    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });
    }

    /**
    * testdriver for class
    *
    */
    public static void main(String args[])
    {
    Help HWindow = new Help();
    HWindow.addHTML();
    HWindow.show();
    }

    }
0
gnurl
Asked:
gnurl
  • 11
  • 7
  • 2
1 Solution
 
objectsCommented:
Use a BorderLayout and add your editor pane in center, and your button south.
0
 
jimmackCommented:
As objects said ;-)

You might want to consider using the .* version of imports if you're going to add much more.

I wasn't sure how many buttons you wanted, so I put a couple there so you could see how I did it ;-)

You might also want to consider replacing the addWindowListener code to the shorter:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Help extends JFrame
{

    private static String path = "";
    private static int width = 600, height = 550;


    public void addHTML()
    {
        path = "file:theHTMLStyledText.File";

        //prepare window
        setSize(width, height);
        setTitle("help");

        //prepare text
        JEditorPane text = new JEditorPane();
        text.setEditable(false);

        //create scrollbar
        JScrollPane scroll = new JScrollPane(text);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(width - 15, height - 49));
        scroll.setMinimumSize(new Dimension(10, 10));
       
        JButton button1 = new JButton("Button1");
        JButton button2 = new JButton("Button2");
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.add(button1);
        buttonPanel.add(button2);
       
        JPanel allPanels = new JPanel(new BorderLayout());
        allPanels.add(scroll, BorderLayout.CENTER);
        allPanels.add(buttonPanel, BorderLayout.SOUTH);
       
        setContentPane(allPanels);

        //set distance to frame to zero
        text.setMargin(new Insets(0, 0, 0, 0));

        try
        {
            text.setPage(new URL(path));
            //text.setOpaque(false);
        }
        catch (IOException e)
        {
            System.out.println("error reading file" + path);
        }

        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
    }


    /**
     * testdriver for class
     *
     * @param  args  Description of the Parameter
     */
    public static void main(String args[])
    {
        Help HWindow = new Help();
        HWindow.addHTML();
        HWindow.show();
    }

}
0
 
gnurlAuthor Commented:
This is how I got it later in the evening by myself....

But thanks.

I think I have to state more exactly what I want.
I want the window above with an background image, but the image not from the HTML file.
I need the possibility th change the background image by a given parameter from the source, so the text should come from the HTML file, but the background image ?must? come from a LayeredPane or so....


Hope for your advise.

gnurl
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gnurlAuthor Commented:
Hi jimmack

I used your code to try to produce the thing I need...

If you resize the the Frame so that the buttons "could" be seen than the background picture comes in front of the other components ..... so if you click where the buttons should be, they appear (same with the other components), but the JEditorPane is not transparent .... thats what I want.

I do not really know whats going on there...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Help extends JFrame
{

      private static String path = "";
      private static int width = 600, height = 550;


      public void addHTML()
      {
            path = "pathToBackground.Image";
            System.out.println(path);
            
            String path2 = "file:pathToHTML.file";
            System.out.println(path2);
            //prepare window
            setSize(width, height);
            setTitle("help");
      
      ImageIcon backgr = new ImageIcon(path);
      JLabel label = new JLabel(backgr);
      label.setBounds(0,0,width, height);
      
      JDesktopPane jdp = new JDesktopPane();
      JPanel transparentPanel = new JPanel();
      transparentPanel.setOpaque(false);
      transparentPanel.setBounds(0,0,width,height);
      transparentPanel.setPreferredSize(new Dimension(width,height));

            //prepare text
            JEditorPane text = new JEditorPane();
            text.setEditable(false);
      //text.setOpaque(false);

            //create scrollbar
            JScrollPane scroll = new JScrollPane(text);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setPreferredSize(new Dimension(width - 15, height - 49));
            scroll.setMinimumSize(new Dimension(10, 10));
    //scroll.setOpaque(false);  
            JButton button1 = new JButton("Button1");
            JButton button2 = new JButton("Button2");
            JPanel buttonPanel = new JPanel(new FlowLayout());
      //buttonPanel.setOpaque(true);
            buttonPanel.add(button1);
            buttonPanel.add(button2);
       
            JPanel allPanels = new JPanel(new BorderLayout());
            allPanels.add(scroll, BorderLayout.CENTER);
            allPanels.add(buttonPanel, BorderLayout.SOUTH);
      //allPanels.setOpaque(false);             
            setContentPane(allPanels);
      
            //set distance to frame to zero
            text.setMargin(new Insets(0, 0, 0, 0));

            try
            {
                  text.setPage(new URL(path2));
                  //text.setOpaque(false);
            }
            catch (IOException e)
            {
                  System.out.println("error reading file" + path2);
            }

      transparentPanel.add(allPanels, new Integer(1));
      transparentPanel.setOpaque(false);
      jdp.add(label);
      jdp.add(transparentPanel);
      setLayeredPane(jdp);


            addWindowListener(
                  new WindowAdapter()
                  {
                        public void windowClosing(WindowEvent e)
                        {
                              System.exit(0);
                        }
                  });
      }


      /**
       * testdriver for class
       *
       * @param  args  Description of the Parameter
       */
      public static void main(String args[])
      {
            Help HWindow = new Help();
            HWindow.addHTML();
            HWindow.show();
      }

}
 
0
 
jimmackCommented:
I think that I vaguely understand what you're trying to do here.  Wouldn't it be easier to replace the background image URL in the HTML once you've read it in (especially if the only problem you have is the image)?
0
 
gnurlAuthor Commented:
This would be the easies way,

but the user in the program should have the possibility to change between predefined (or selfdefined) themes (or styles) in the program, and so, the background image of the HTML file should fit to the rest of the program just by a click.
If only one help file would exist, the text could be formated (hardcoded) in the class (as in Sun Java Demo shown for Stylepad Wonderland.java), but the user can switch between languages (and create his own helpfile (should be easy -> thats why html)).
So using the background image from the HTML file isn't really what I need.

Thanks
gnurl
0
 
objectsCommented:
Try using a JLabel to display your image, and add your html pane to the label annd make it opaque.
0
 
gnurlAuthor Commented:
Isn't it what I tried in the last code?

I don't get the HTML pane opaque so it seems (besides other problems like background image comes to foreground when resize and so on ....)
0
 
objectsCommented:
Sorry too much code to digest :)
Though you don't appear to be making the scroll panes viewport opaque, and i'm not sure your intention of the layered pane.
I'd get it working (opaqueness) with justy a JPanel first and then replace it with a JEditorPane.
0
 
gnurlAuthor Commented:
To quote Morpheus from Matrix:  "Show me!" :)
 
All code with // I tired in combination with the un//ed code to get ist working ...
The LayeredPane is the way I know to put an image to the background (use layers like layers in Photoshop ..), layer zero background, layer one the other components.
0
 
objectsCommented:
ImageIcon backgr = new ImageIcon(path);
JLabel label = new JLabel(backgr);
label.setLayout(new BorderLayout());
JPanel x = new JPanel();
//x.setOpaque(false);
x.setBackground(Color.red);
label.add(BorderLayout.CENTER, x);
0
 
gnurlAuthor Commented:
Sorry it's quite late here I even do not understand how to integrate this in my code ....
I will go on tomorrow...
0
 
gnurlAuthor Commented:
Hi all,

I tried objects sugestion, but I could not get it working. The backgound do not shine through the JEditorPane.

Any sugestions how to solve this problem?





This is the Code:

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class Help extends JFrame
{

      private static String path = "";
      private static int width = 600, height = 550;


      public void addHTML()
      {
            path = "pathToBackground.Image";
            System.out.println(path);
            
            String path2 = "pathToHTML.File";
            System.out.println(path2);
            //prepare window
            setSize(width, height);
            setTitle("help");

            ImageIcon backgr = new ImageIcon(path);
            JLabel label = new JLabel(backgr);
                  
            label.setLayout(new BorderLayout());      
            //label.setOpaque(true);
                                          
            JPanel thepanel = new JPanel();
            thepanel.setLayout(new FlowLayout());      
      
            //      prepare text
            JEditorPane text = new JEditorPane();
            text.setEditable(false);
            text.setOpaque(false);
            
            //      create scrollbar
            JScrollPane scroll = new JScrollPane(text);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setPreferredSize(new Dimension(width - 150, height - 149));
            scroll.setMinimumSize(new Dimension(10, 10));
            scroll.setOpaque(false);
      
            try
            {
                  text.setPage(new URL(path2));                  
            }
            catch (IOException e)
            {
                  System.out.println("error reading file" + path2);
            }

            addWindowListener(
                  new WindowAdapter()
                  {
                        public void windowClosing(WindowEvent e)
                        {
                              System.exit(0);
                        }
                  });
            
            text.setOpaque(false);      
            thepanel.add(scroll);
            thepanel.setOpaque(false);      
            label.add(thepanel);
            setContentPane(label);
            
      }


      /**
       * testdriver for class
       *
       * @param  args  Description of the Parameter
       */
      public static void main(String args[])
      {
            Help HWindow = new Help();
            HWindow.addHTML();
            HWindow.show();
      }

}
0
 
objectsCommented:
you still ahven't made the viewport opaque.
Did u try with just a panel, to determine if the problem is with the editor pane or elsewhere?
0
 
gnurlAuthor Commented:
I made it this way to determine if I can get the JEditorPane opaque, and smaller code ist better code I think, so why work with DesktopPanels if it works with labels and panels too.

I have never worked with viewports, I translated this with the opaqueness of the ScrollPane.

My next fight will be with this :)
0
 
gnurlAuthor Commented:
I got it! :)

Thanks objects that are 300 points to you if you could help me how to avoid the borders of the EditorPane which left.

This is my code:



import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;


public class Help extends JFrame implements ActionListener
{

      private static String path = "";
      private static int width = 600, height = 550;


      public void addHTML()  
      {
            path = "pathToBackground.Image";
            System.out.println(path);
            
            String path2 = "pathToHTML.File";
            System.out.println(path2);
            //prepare window
            setSize(width, height);
            setTitle("help");

            ImageIcon backgr = new ImageIcon(path);
            JLabel label = new JLabel(backgr);
                  
            label.setLayout(new GridLayout());      
                                          
            JPanel thepanel = new JPanel();
            thepanel.setLayout(new FlowLayout());      
      
            //      prepare text
            JEditorPane text = new JEditorPane();
            text.setEditable(false);
            text.setOpaque(false);
            
            // prepare viewport
            JViewport view = new JViewport();
            view.add(text);
            view.setOpaque(false);
            

            //      create scrollbar
            JScrollPane scroll = new JScrollPane(view);
            scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scroll.setPreferredSize(new Dimension(width-10, height - 80));
            scroll.setMinimumSize(new Dimension(10, 10));
            scroll.setOpaque(false);
            
            scroll.setViewport(view);      
                  
            JButton b = new JButton("OK");
            b.addActionListener(this);
            
            try
            {
                  text.setPage(new URL(path2));      
            }
            catch (IOException e)
            {
                  System.out.println("error reading file" + path2);
            }

            addWindowListener(
                  new WindowAdapter()
                  {
                        public void windowClosing(WindowEvent e)
                        {
                              System.exit(0);
                        }
                  });

            //prepare painiting      
            thepanel.add(scroll);
            thepanel.add(b);
            thepanel.setOpaque(false);      
            label.add(thepanel);
            setContentPane(label);
      }

      public void actionPerformed(ActionEvent e) {
            System.exit(0);
      }


      /**
       * testdriver for class
       *
       * @param  args  Description of the Parameter
       */
      public static void main(String args[])
      {
            Help HWindow = new Help();
            HWindow.addHTML();
            HWindow.show();
      }

}


I wonder why I could not find any useable information in the web about this feature, is it really so easy to produce such a help window, or is nobody interested in such a window?

CU gnurl

0
 
objectsCommented:
> if you could help me how to avoid the borders of the EditorPane which left.

try setting the border of the editor pane, scrollpane, viewport to null (not sure which actually owns the border).
0
 
gnurlAuthor Commented:
I will try it.
0
 
gnurlAuthor Commented:
The border is owned by the scrollpane, so

scroll.setBorder(null);

removed the border.

Thanks!
0
 
objectsCommented:
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 11
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now