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:
Whether it is possible to add two canvases to a single JFrame.
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)?
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.
Related
I am using JFrame which contains three sections. 1st section is Pane, which contains the 6 menu button (left side). 2nd section is also Pane, which displays the logo of the company on the top. The third section is DesktopPane in which I am using (calling) JInterenalFrame in the DesktopPane.
How to always show the JInterenalFrame content (form data) into the middle of DesktopPane?
Third section is DesktopPane in which I am using(calling) JInterenalFrame in the DesktopPane.
A JDesktopPane is used to display multiple JInternalFrames. A JInternalFrame can be dragged around the desktop pane.
From your picture it looks like you just have a single JPanel in that area. Therefore you should not be using JDesktopPanel and JInternalFrame.
Instead you just use a regular JPanel with a CardLayout. This you can replace each panel based on the selection from your menu on the left.
See How to Use CardLayout for more information.
Show the data into middle of the form
The easiest way to do this is to use a JPanel with a GridBagLayout.
So you need to wrap your current panel in a panel with the GridBagLayout.
So the basic code is:
JPanel welcomePanel. = new JPanel( new GridBagLayout() );
welcomePanel.add(currentPanel, new GridBagConstraints());
Now your "currentPanel" will be centered in the "welcomePanel", which has been added to your panel using the CardLayout.
I'd like to add JLabels dynamically in a JPanel vertically like the image that I've attached. After loading all images, I need to select an image, then selected the image should appear in another JPanel. I am reading Images from an ArrayList which contains the paths.
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
This is the code that I've used to add Jlabels:
for(String file: files) {
JLabel JLabelPicture = new JLabel(new ImageIcon(file));
panel_images.add(JLabelPicture);
}
I used Jpanel with GridLayout in a JScrollPane, but the result is not the same that I want.
A GridLayout will allow you to display the components vertically. When you create your panel you just use:
JPanel imagePanel = new JPanel( new GridLayout(0, 1) );
This will resize all the images to the same size.
Another option is to use a vertical BoxLayout. In this case you can use:
Box imagePanel = Box.createVerticalBox();
In this case the images will retain their preferred size.
In both case you add to the panel to a scroll pane which is added to your frame:
frame.add( new JScrollPane( imagePanel ) );
Read the Swing tutorial on Layout Managers for more information and working examples of each layout.
Edit:
after listing the images I need to select one of them,
Well where was that requirement in your original question. The complete requirement should be defined in the question so all the information is in one place for everybody to see.
So I would suggest you should be using a JList to display an Icon. Read the section from the Swing tutorial on How to Use Lists for more information.
I am working in the NetBeans. For that when I am working in the new JFrame form or the design view if I add the panel and then add the label and rest of the contents, it makes no difference if I don't add a panel and add the contents like JLabel, JButton, etc. it makes no difference.
Is there any reason why panel should be added to the frame? I tried to close the application when the panel was inserted and when it was not inserted on the frame, the application closes both the times. (When I press run and try to close the application, both times it closes.)
Then what is the use of putting JPanel on a JFrame?
When you add components to the frame the components are added to the content pane of the frame which by default is a JPanel. Read the section from the Swing tutorial on Using Top Level Containers for more information.
By creating a separate panel and adding components to that panel you give yourself more flexibility when designing your application. For example you may want to use a CardLayout which allows you to swap out different panels on the frame. The tutorial also has a section on using CardLayout.
What is the best way to display let's say rectangle (3x5) with icons 20x20 px.? I want to change the image file of every pic icon later (= it's not just static pictures). I tried to make JFrame full of JPanels, but i was able to display only one panel at a time. I don't want to use GridLayout, because I need just small rectangle inside a frame. Any ideas how to do it? Couldn't find any tutorial or solution. I'm completely new to GUI developement. Thanks
You do want to use a GridLayout. Your problem is that the JFrame you put the icons into uses a BorderLayout by default (and really, you shouldn't change the layout of a top level component).
What this means is that, if you add multiple panels to the frame, without using one of the NORTH, EAST, SOUTH, WEST constraints, only one of the panels will be visible and take up all the space. If you use a GridLayout for that one panel you get, the icons will be stretched, because the panel receives all the space due to the frame's BorderLayout. An alternate layout that doesn't stretch its contents is FlowLayout, but the layout to use depends heavily on your context.
To display the icons, a JLabel is handy. Use an ImageIcon for the label's icon. You can later use setIcon() on the label to choose a new icon.
overall, my approach would be this:
use a JFrame which has a BorderLayout
to the frame, add a JPanel to the frame. The default layout is a FlowLayout, which will prevent the stretching
to the panel, add a JPanel with an appropriate GridLayout
to that panel, add the JLabels, each having an appropriate ImageIcon
I am new on using Swings
my requirement is to align components within the jpanel(panel2)
I have taken 2 JPanels (panel1, panel2) and added to the jframe
panel1.add(panel2);
panel2.setLayout(new flowLayout());
panel2.setBounds(80,120,100,100);
getContentPane() .add(Panel1);
and I have created a "Create" button that will generate text area dynamically in panel2
now my problem is if the created textarea is reaching out of panel2 it has to show an error
message "You reached the boundaries of the jpanel so the textarea cant be created "
Thanks in advance
Set the layout before you add any elements to the panel. No not call setBounds as with layout manager present it likely has no effect at all.
I cannot explain how to layout your elements as from your question seems not possible to figure out that do you want to do. Best, post the drawing with elements as they should look like. GridLayout maybe would be good if you want to align multiple elements as in the table.