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.
Related
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 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.
I want to display a JFrame ( made with the Netbeans GUI Editor ) that has an enclosed panel ( the panel encovers the entire JFrame ). The panel is twice as wide as the frame, so I want it so that when a button is pressed inside of the panel, the panel's visible area slides over ( over about 2 seconds) to the hidden area of the JPanel and the previously visible section of the JPanel becomes invisible. I couldn't find any function how to set the currently visible section of a JPanel, so the function and/or a different solution to this would be helpful.
I suggest that you put the JPanel in a JScrollPane, one that if you wish does not show its scrollbars. Then you could easily use the scrollpane's model and a Swing Timer to create an animation that shows the JPanel sliding.
The solutions is CardLayout based http://java-sl.com/tip_slider.html
You can add 2 (or more) panels into container and rotate them.
I am trying to build my own "Battleship" game and have problems with swing.
I now read endless docs on oracle tutorials on LayoutManagers, but not any of them works as I understand them. They only add a few buttons, but never two individual panels.
JPanel Background = new JPanel();
Background.setLayout(new BoxLayout(Background, BoxLayout.X_AXIS));
panelPlayer = new JPanel();
panelPlayer.setBorder(BorderFactory.createLineBorder(Color.black));
panelPlayer.setSize(700, 600);
// PC Field
panelPc = new JPanel();
panelPc.setBorder(BorderFactory.createLineBorder(Color.black));
panelPc.setSize(700, 600);
//adding to frame
getContentPane().add(Background);
Background.add(panelPlayer);
Background.add(panelPc);
After that I have a loop thats adds 16x16 buttons in a JButton[] once for every panel.
How to get the two panels to show a table layout?
I used GridLayout before, the grid works, but it always takes up the whole space of the frame, not of the Container or Panel or else. The panels are overlapping then.
GridBagLayout just puts the buttons in a row and beyond the screen.
Don't fix the size of the panel while using any layout. It works only when you use null layout
You can achieve your goal with GridBagLayout. While adding buttons specify gridx, gridy correctly, it will add buttons like table
just keep nesting the layouts.
in your case make a big one with two sides -
then in each side place another panel with your grid.
You can solve this by nesting panels. Each panel has its own layout manager, so it is a matter of breaking up your UI into pieces and choosing the layout manager for each piece.
If you want two panels side-by-side, then the panel that contains them should have a FlowLayout manager with horizontal orientation. Create a panel with FlowLayout and add the panels to it.
If each of the the side-by-side panels needs the grid of buttons, then set the panel layout to GridLayout and put the buttons in the panel. This fits what I remember of Battleship; in a grid layout, all the grid elements remain the same size no matter how the window is resized.
That should get you started. If, as I expect, you will want another panel with some game controls on it, look into BorderLayout; it has a section on each edge of a rectangle and another in the middle. Put the panel containing the two grids in the center of a panel using BorderLayout, and then your game controls can go in a panel to the north, south, east, or west of that.
Good luck. Let us know if you have a specific problem (in another question).
I already have a panel made (its a row of buttons), and have it located at the bottom of a frame (SOUTH), but I would like to add two rows (panels/ subpanels) beneath it (a text input line and output line if it matters). Right now the only thing I know to do is declare and add more panels, which would be fine, but when I specify .SOUTH they go over top of the previous panel.
EDIT: The solution I used
As suggested by Ted Hopp, I added my original panel (now called subPanel1), as well as the two new panels which were going on top of the original (subPanel2 & subPanel3), to a fourth "container panel" (bottomCotainerPanel). Since I only had three subPanels, this allowed me to specify where in the containerPanel each subPanel would go (using NORTH, CENTER, SOUTH, might have to do something slightly different if you had more than 3...), and then specify where the contianerPanel would go in the frame (SOUTH).
subPanel1.setLayout(new GridLayout(1,6)); //Layout of subPanel1
subPanel1.add(clearButton);
subPanel1.add(customerNameLabel);
subPanel1.add(customerNameTextField);
subPanel1.add(showByNameButton);
subPanel1.add(openNewSavingsButton);
subPanel1.add(openNewCheckingButton);
subPanel2.add(sendChatTextField);
subPanel2.add(sendButton);
subPanel2.add(clearButton2);
subPanel3.add(receiveChatTextField);
subPanel3.add(nextButton);
subPanel3.add(previousButton);
bottomContainerPanel.setLayout(new GridLayout(3,1)); //Layout of Container Panel
bottomContainerPanel.add(subPanel1, BorderLayout.NORTH);
bottomContainerPanel.add(subPanel2, BorderLayout.CENTER);
bottomContainerPanel.add(subPanel3, BorderLayout.SOUTH);
tellerWindow.getContentPane().add(bottomContainerPanel, BorderLayout.SOUTH);
You need to add a single container panel as the SOUTH panel of the frame. The container itself should have the layout that you want for everything that goes at the bottom.
If you just want to split panel onto 2 equal parts at south and north use GridLayout. If you want something in the middle you can use BorderLayout.
If you want to give your user ability to change the sub-panels size use JSplitPane.
I had a similar problem trying to put several rows of buttons into a panel borrowed from the ListDemo example. Well, the first thing to do is to read about BorderLayout: How to Use BorderLayout, or at least see the image shown there:
You cannot have multiple bottom rows in a BorderLayout. But you can use a nested layout. What we need is a BoxLayout, see How to Use BoxLayout:
.
We just have to replace the buttons shown on the above image by rows of buttons.
public class MyStuff extends JPanel {
...
public MyStuff() {
super(new BorderLayout());
...
JPanel buttonArea = new JPanel();
buttonArea.setLayout(new BoxLayout(buttonArea, BoxLayout.PAGE_AXIS));
add(buttonArea, BorderLayout.PAGE_END);
...
//if you dislike the default center alignment:
//panelWithButtons1.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonArea.add(...);// add the 1st panel with buttons
buttonArea.add(...);// add the 2nd panel with buttons
buttonArea.add(...);// add the 3rd panel with buttons