Why does BorderLayout overwrite components when new ones are added? - java

BorderLayout does something strange. If I add two panels to a Container with the same constraint (BorderLayout.CENTER for instance), then the first one goes away, even if the second one is deleted or made invisible
It seems as though it would make sense for it to "stack" each element on top of the previous ones.
Is this correct and by design? If so, is there some documentation on it?
Has anyone else been frustrated by it? Have you a solution, such as a custom LayoutManager?
Sample code:
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);
panel2.setVisible(false); // Seems like it should allow us to see panel1.
frame.setVisible(true);
This creates and displays a 500x500 blank box.

BorderLayout was simply not designed to do what you want. Separation of responsibility. If you want that behavior you should compose: combine the BorderLayout with a CardLayout. Though for the actual stack behavior, you'll have to code something yourself (or find someone who already has.)

Is this correct and by design?
Yes.
You need to understand the basics of how layout managers work. One of the jobs of the layout manager is to set the "location" and "size" of the components added to the panel. In the case of a BorderLayout it only tracks 5 components so only the last component added to the CENTER is known by the layout manager.
Layout management is not done when components are added to the panel. It is done when the frame is packed, or made visible (or the revalidate() method is invoked) . In this case the blue panel is not part of the components managed by the BorderLayout so its size remains (0, 0), which means there is nothing to paint.
Try changing your code to:
JPanel panel1 = new JPanel();
panel1.setSize(200, 200);
and you will see the blue panel painted at the specified size.
Now try commenting out:
//panel2.setVisible(false);
and you will see both panels. This is because as components are added to the panel they are assigned a ZOrder. Basically the last component added is painted first, which is why the blue panel is painted on top of the red panel. Check out the setComponentZOrder() method of the Container class for more information.
The CardLayout is probably the layout manager you should be using, but you can check out the Overlap Layout as well.

Related

Java setOpaque method not working to add panels over the top of others

I am attempting to place two panels over the top of another two panels, so the positions match exactly. This will allow me to then paint over the top of the previous(bottom) panels. The reason for doing this, is that I have implemented a "drag a rectangle" zoom function, so on the panel the user will drag the mouse to draw a rectangle, and then the panel will become filled with the contents of that rectangle. However, I have now decided I would like include a visual representation of the rectangle as the user drags it.
Since I (assume) I cannot draw the rectangle on the bottom panel, since it already has a complicated paint on it anyway, I figured I would be able to draw it onto another panel that I would place on top. (If there is any easier way please shout!)
I am happy I have been able to get the panels into the correct position, using a BorderLayout/GridLayout combo, without the need for absolute positing, however the setOpaque method is not working on any of the three panels I have added. Does anyone know why this is?
Many thanks in advance, code below:
public void initiate() {
JPanel panel1 = new JPanel();
mandPanel mandDisplay = new mandPanel();
juliaPanel juliaDisplay = new juliaPanel();
JPanel detailsPanel = new JPanel();
JPanel bottomDetails = new JPanel();
JPanel panelForDisplays = new JPanel();
JPanel listenerPanelHolder = new JPanel();
JPanel listenerPanel = new JPanel();
JPanel blankPanel = new JPanel();
panelForDisplays.setLayout(new GridLayout(1,2));
detailsPanel.setLayout(new FlowLayout());
bottomDetails.setLayout(new FlowLayout());
panel1.setLayout(new BorderLayout());
panelForDisplays.add(mandDisplay);
panelForDisplays.add(juliaDisplay);
listenerPanelHolder.setOpaque(false);
listenerPanel.setOpaque(false);
blankPanel.setOpaque(false);
listenerPanelHolder.setLayout(new GridLayout(1,2));
listenerPanelHolder.add(listenerPanel);
listenerPanelHolder.add(blankPanel);
panel1.add(detailsPanel,BorderLayout.NORTH);
panel1.add(panelForDisplays,BorderLayout.CENTER);
panel1.add(listenerPanel,BorderLayout.CENTER);
panel1.add(bottomDetails, BorderLayout.SOUTH);
setContentPane(panel1);
setSize(width*2, height);
setResizable(false);
this.addWindowListener(new onClose(savedJulias));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
PS: A lot of code is omitted to avoid mess, but if anyone thinks they could solve with more code please say! Thanks :)
First off, adding two components at the BorderLayout.CENTER position replaces the first component with the 2nd, and so the 2nd doesn't cover the 1st, it displaces it. As per the BorderLayout API:
Each region may contain no more than one component,...
But having said that, as I understand your problem in greater detail, I think that your best solution is not to use JLayeredPane or to overlay two JPanels. The best solution is in fact to draw directly on the image-displaying JPanel. This is not hard to do, and shouldn't over complicate your code. For example.
You can use JLayeredPane to place one component to the top of the other. You can check this

Absolute Layout Panel within JScrollPane

I'am using panel with absolute layout (don't ask why) and I need to add elements on it programmatically. I done that part, but now I want to surround panel with JScrollPane so that when user add more items, scroll bar does its job. But surrounding panel with scroll bar doesn't work. What can I do here.
JFrame frame = new JFrame();
frame.setSize(582, 451);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 546, 391);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
for(int i=0;i<labelList.size();i++){
if(a==4){
a=0;
b++;
}
labelList.get(i).setBounds(10+120*a+10*a, 10+130*b+10*b, 120, 130);
labelList.get(i).setOpaque(true);
labelList.get(i).setBackground(Color.WHITE);
labelList.get(i).setText(Integer.toString(i));
panel.add(labelList.get(i));
a++;
}
You're not going to like my answer, but I feel that we should be compelled to give you the best answer, which is not always what you want to hear. And that is this: use layout managers to do your component layouts. Yes while it might seem to newbies that null layouts are the easiest to use, as you gain more experience you will find that exactly the opposite is true. Null layout use makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
As for ease of use, I invite you to create a complex GUI with your null layout, and then later try to add a component in the middle of this GUI. This will require you to manually re-calculate all the positions and sizes of components added to the left or below the new component, which is prone to many errors. The layout managers will all do this automatically for you.
Specifically use of valid layout managers will update your JPanel's preferredSize automatically increase as more components are added and as the JPanel is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. Yes, a work around is for you to manually calculate and set your JPanel's preferredSize, but this is dangerous and not recommended for the same reasons noted above.

How to put two java swing JPanels next to each other and have a table in it?

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

How can I split a panel into subpanels?

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

How can I position a JButton under a JTable?

How can I position a JButton under a JTable? What kind of layouts? How? I have a JTable table what is scrollable, and the table is in a frame.
There are many layouts to fulfill this need.
The simplest is using BorderLayout:
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JScrollPane(jtable), BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
If you want this button to be not resized just add JPanel to contentPane. This JPanel should contain your button centered using almost any layout.
If you would like to use more sophisticated layout - the best in my opinion is MigLayout
To correct LoveToCode's well-meaning but misleading advice, you would never want to give a JTable itself a layout. The solution to your problem is not to set the JTable's layout but to set the layout for the JPanel that holds both the JTable's JScrollPane and the JButton. Likely a BorderLayout would work best with the JScrollPane being placed BorderLayout.CENTER and the JPanel that holds the JButton BorderLayout.SOUTH.
Note, if you're adding these components to the JFrame, then know that its contentPane already uses BorderLayout (the tutorials will tell you this -- please read them). So just add these guys to the contentPane as described above.
If hard coding of swing and awt is not mandatory, try using the WindowsBuilder Pro, a Free tool now from google, install its plugin in to eclipse, then Use
GroupLayout - Introduced by NetBeans team in 2005 integrated in WindowsBuilder Pro, is one of the most convenient way create a good gui in less time in Java.
It seems that you can set the layout of a jtable the same way in which you can set the layout of a jframe or a jpane:
table.setLayout(new grideLayout(4, 3)
would give it a grid layout with 4 rows and 3 columns.
the scrollable feature allows you to 'scroll' through your table with the moving bar. It may be a default feature, or it may be that you must use it, try seeing what methods your table gives you.
Lastly, your table should be in a frame, so that you can view it on your window, make you class extend JFrame and it will automatically be a frame upon which you can simply add a table!

Categories

Resources