1

I recently have a requirement to display a word file within a JFrame. With this link I was able to achieve what I want (Open MS documents into JFrame). What i need is to display a word file and a pdf file side by side within a JFrame.

In the link mentioned above, the word file was displayed in a JFrame via a Canvas from SWT.

I would like to know:

  1. Whether it is possible to add two canvases to a single JFrame.
  2. If not, is it possible to display a word document or a PDF file in a JPanel (since I know that adding two panels to a frame is possible)?
Community
  • 1
  • 1
Mohankumar
  • 43
  • 1
  • 11
  • 3
    This is a monumentally trivial problem that could be expressed without any reference to what is being shown in the frame. For example, figure out how to show 2 labels (`JLabel`), or 2 panels (`JPanel`) and the solution for the SWT canvas will be obvious (do the same thing, with the different component). Once SWT is removed from the question, that also means Eclipse is irrelevant. As to the answer, there are many, *many* options, as shown in [this answer](http://stackoverflow.com/a/9554657/418556). Choose one that will work for this use-case, then try something. Voting to close. – Andrew Thompson Sep 02 '15 at 06:46
  • 2
    *"..is it possible to display a word document or a PDF file in a `JPanel` (since I know that adding two panels to a frame is possible)"* .. Anything that can be added to a `JFrame` can be added to a `JPanel`! Try it.. – Andrew Thompson Sep 02 '15 at 06:52

1 Answers1

1

In the example you linked the canvas is added directly to the content pane of the JFrame. What you need to do is to insert a JPanel with a Layout to the JFrame first, and after that add one or many Canvas objects to the layout. A trivial example with the default layout FlowLayout is below, feel free to modify it to use a different layout manager or add a JScrollPane or JSplitPane depending on the layout you want.

JPanel panel = new JPanel(); //Default layout manager is FlowLayout
//You could change the layout here with panel.setLayout(new ..Layout);
frame.getContentPane().add(panel);
panel.add(canvas1);
panel.add(canvas2);

Here is a useful link to layout managers. Look for example into BorderLayout if you wish to add menus etc. to your frame.

milez
  • 2,201
  • 12
  • 31
  • @AndrewThompson Agreed, it should indicate that scrolls and splits are added to the layouts – milez Sep 02 '15 at 09:59