Mohankumar
Mohankumar

Reputation: 43

How to display two canvas in a JFrame java

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)?

Upvotes: 1

Views: 1930

Answers (1)

milez
milez

Reputation: 2201

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.

Upvotes: 1

Related Questions