Solved

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

Posted on 2003-12-06
20
673 Views
Last Modified: 2013-11-18
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
Comment
Question by:gnurl
  • 11
  • 7
  • 2
20 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9889842
Use a BorderLayout and add your editor pane in center, and your button south.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9890116
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
 
LVL 7

Author Comment

by:gnurl
ID: 9892308
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
 
LVL 7

Author Comment

by:gnurl
ID: 9892517
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
 
LVL 15

Expert Comment

by:jimmack
ID: 9892678
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
 
LVL 7

Author Comment

by:gnurl
ID: 9892972
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
 
LVL 92

Expert Comment

by:objects
ID: 9893024
Try using a JLabel to display your image, and add your html pane to the label annd make it opaque.
0
 
LVL 7

Author Comment

by:gnurl
ID: 9893049
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
 
LVL 92

Expert Comment

by:objects
ID: 9893067
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
 
LVL 7

Author Comment

by:gnurl
ID: 9893128
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 92

Expert Comment

by:objects
ID: 9893144
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
 
LVL 7

Author Comment

by:gnurl
ID: 9893206
Sorry it's quite late here I even do not understand how to integrate this in my code ....
I will go on tomorrow...
0
 
LVL 7

Author Comment

by:gnurl
ID: 9932175
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
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 9932198
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
 
LVL 7

Author Comment

by:gnurl
ID: 9933803
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
 
LVL 7

Author Comment

by:gnurl
ID: 9934141
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
 
LVL 92

Expert Comment

by:objects
ID: 9935494
> 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
 
LVL 7

Author Comment

by:gnurl
ID: 9937189
I will try it.
0
 
LVL 7

Author Comment

by:gnurl
ID: 9937195
The border is owned by the scrollpane, so

scroll.setBorder(null);

removed the border.

Thanks!
0
 
LVL 92

Expert Comment

by:objects
ID: 9938584
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video teaches viewers about errors in exception handling.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now