I want to have multiple vertical panels in my frame which will start with a button in them. When I push these buttons they will add more buttons to these panels. When new buttons are added, the panels should expand and push all the other panels below them. I added an example image of what I want above.
I am not new with layout managers, are there any layout manager that I can use for this? If not what can I do?
It depends on the layout manager of the child panel and the layout manager of the parent panel. Both layout manager need to be able to grow. Based on the picture you could use a vertical BoxLayout for both parent and child panels.
When you add a component to a visible panel you then need to revalidate() the panel to invoke the layout manager so the basic code would be:
JButton button = new JButton(...);
panel.add( button );
panel.revalidate();
panel.repaint();
Read the section from the Swing tutorial on Layout Managers for more information and working examples to get you started.
Related
I would like to change my current layout to a new one, more advanced, but I have some issues with it. The current layout is as this:
All the displayed elements are in a panel (bottom panel, there is a top one which just includes JTable but is not relevant in this case) the checkboxes are in a JScrollPaneBox, which border layout is
BorderLayout.CENTER, then the buttons are in a Box and added to the panel with border layout BorderLayout.EAST, and the search field is added to the panel with borderlayout BorderLayout.SOUTH
But this layout is especially 'ugly' when the app is on fullscreen.
I would like to have a layout like this: https://wireframe.cc/Kb05km
How can I add the two labels and add a space between checkboxes and search field? Also how can I limit the maximum width of checkboxes' ScrollPaneBox and the search field?
Thanks!
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 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'm building a Swing application in Java using NetBeans and I have a problem with layout. My main frame contains a JScrollPane which contains a JPanel called contentPanel which in turn contains a JPanel called listPanel. The listPanel is empty when the program starts, but when the user interacts with the program an unpredictable number of smaller JPanels are added to it. I've used the NetBeans GUI-builder to snap the top edge of listPanel to the top of contentPanel, and the same with the bottom edges.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane. The verticalScrollBarPolicy of my scrollpane is set to AS_NEEDED and its viewportView is set to contentPanel. What I think I need to do is to make contentPanel grow when more items are added to listPanel.
The problem I have is that when more components are added to listPanel the vertical scrollbar doesen't appear on my scrollpane.
The scrollbar will appear when the preferred size of the component added to the scrollpane is greater than the size of the scrollpane. When you add components dynamically you need to tell the scrollpane something has changed. So you basic code should be:
panel.add( subPanel );
panel.revalidate();
Or, because you are adding a panel to the sub panel, you may need to revalidate the scrollpane (I don't remember):
panel.add( subPanel );
scrollPane.revalidate();
The key is the revalidate() which tell the layout manager to recalculate its size.
Use a different LayoutManager. One that will allow for vertical growth like BoxLayout. Also remember that you can use multiple layouts and nest them inside of each other for different effects.
I added child panel to parent panel by using method 'parent.addTab(child)' and added one JLabel in the child panel but setBounds method is not working in child panel. This JLabel is getting showed at one fix location. setBounds is working fine in parent panel. What to do?
You need to define an appropriate layout manager for your child panel. For example, if you chose to use BorderLayout you could arrange for the label to be shown in the center of the panel as follows:
JTabbedPane parent = new JTabbedPane();
JPanel child = new JPanel(new BorderLayout());
// Create label with centrally aligned text (default is left).
JLabel label = new JLabel("Hello, World", JLabel.CENTER_ALIGNMENT);
// Add label to center of the child panel.
child.add(label, BorderLayout.CENTER);
// Add child panel as a tab within parent JTabbedPane.
// The child panel will expand to fit the size of the tab.
parent.addTab("My Tab", child);
For more a flexible layout consider using GridBagLayout.
setBounds only work for containers that have their layout set to null. (Or for moving frames around on the desktop.)
JPanel's default layout manager is FlowLayout which lays out components in order horizontally, and dropping to a new row when the current is full (like text in a page).
Using a null layout and setBounds isn't a recommended way to lay out GUIs - it's very fragile - too much depends on the size of the container / frame and the size, resolution and font settings of the desktop can easily break the layout.
Read through the Using Layout Managers section of the Swing tutorial to figure out what layout managers can help you accomplish what you want.