SIze of Jpanel inside JSplitPanel - java

I was trying to add JsplitPane into my project.Requirement is i need to add two Jpanel inside right panel of JSplitPane.
so what i had done is first add Jpanel say it panel1 to right panel and set BoxLayout.Y-AXIS and than add two panel inside panel1.
now in that two panel first panel have BoxLayout and i want the width of this panel to be of size of panel1 but i am not able to do it.
anyone have idea how to do it?

I was trying to add JsplitPane into my project.Requirement is i need
to add two Jpanel inside right panel of JSplitPane.
When using JSplitPane, we should remember that it only divides the pane into two components say Left and Right or Top and Bottom. So when we again want to add more than one components in a single side of that JSplitPane, say in our case Right, it is better to use Nesting Split Panes. That means creating Split Panes inside Split Panes.
so what i had done is first add Jpanel say it panel1 to right panel
and set BoxLayout.Y-AXIS and than add two panel inside panel1.
now in that two panel first panel have BoxLayout and i want the width
of this panel to be of size of panel1 but i am not able to do it.
If you use Nesting Split Panes, you may not have to create an extra Parent JPanel what you said as panel1. Actually the Split Pane is used to divide the Pane into two segments. So, by using Nesting Split Panes you are creating another Split Pane in stead of what you were creating as panel1 and then put your two child panels inside the two Panes created by new JSplitPane which is nested. So, you don't have to think about the size issue, too. I hope I could make you clear and it solved your issue.
A simple way to achieve that by using:
Declaration:
private JSplitPane jSplitPane1;
private JSplitPane jSplitPane2;
private JPanel jPanel1;
private JPanel jPanel2;
In Constructor:
jSplitPane1 = new JSplitPane();
jSplitPane2 = new JSplitPane();
jSplitPane1.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
jSplitPane1.setRightComponent(jSplitPane2);
jSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
jSplitPane1.setTopComponent(jPanel1);
jSplitPane1.setBottomComponent(jPanel2);
The above described method is the simplest to achieve what you wanted. However, without nesting the Split Pane, it is possible to use Multi Split Panes which may not be so handy. Still you can have a look at this old article at Oracle:
https://community.oracle.com/docs/DOC-983539

Related

Make buttons unresizable

So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.

Placing components on Jpanel

Can i combine Java layouts in same JPanel. I'm stuck with with placing my components on JPanel. It shoudl be like this: JLabel, JButton, JButton , JLabel and new line and same. I used BorderLayout but it wont go to the next row, keep adding components to same row and I need a new row. Ideal sit combined with cardlayout or some other good solution.
EDIT: Solved with GridLayout (0,4) It will do the job till i learn to use GridBaglayout. Thank you for trying to help me.
Yes you can combine layouts.
Using a JPanel you are able to embed other JPanels:
JPanel back = new JPanel(new BorderLayout());
JPanel rows = new JPabel(new GridLayout(3,3));
back.add(rows, BorderLayout.CENTER);
Without seeing your code though it's difficult to know exactly what you are trying to achieve!
Yes you can combine java layouts.
A common pattern I use is BorderLayout first on a frame. The central component expands out, while the other components shrink in. Inside these panels I might have a Flowlayout to show buttons evenly spaced horizontally on top.
Another common approach for forms is using a Gridbaglayout, then adding all the form elements at gridX and gridY positions. I then later can stretch and teak these cells using other constraints in the Gridbaglayout repetoire.
Can you add a screenshot so that we can see what you want to do?

JLayeredPane with gridlayout?

Is there a way for me to add buttons to the right side of my JLayeredPane? The layered pane contains a JPanel which represents a chess board and on top of this board I have JLabels representing chess pieces. I want basically another panel attached to the right side of the board which contains player information and buttons allowing for rematches/etc. What would be the best way to go about adding this panel?
Here is a snippet of my code. The part which sets up the board panel inside the layeredpane:
private void setupBoard() {
paneX = new JLayeredPane();
getContentPane().add(paneX);
paneX.setPreferredSize(new Dimension(500,500));
boardX = new JPanel();
paneX.add(boardX, JLayeredPane.DEFAULT_LAYER);
boardX.setLayout(new GridLayout(8,8));
boardX.setBounds(0,0,500,500);
chessBoard.setPreferredSize(new Dimension(500,500));
}
Then, I go on to add the jlabels to each component on the panel. I want to add another big panel attached to the right side of the board like I mentioned earlier.
Can't quite see why you would use a JLayeredPane for this, but that's just me.
Set the Layout for the main container to BorderLayout. Add the boardX to the BorderLayout.CENTER position of the main container.
Add the player information panel to the BorderLayout.EAST position of the main container.
Setting the bounds of the boardX is not really going to have any effect, as the parent container will want to use the panels preferred/minimum/maximum size (based on whatever layout manager you might use) to determine the best size to make it, which based on your code, would probably be 500x500 anyway...

Why am I not getting a functioning JScrollPane?

I have a JFrame window, and I'd like to add a scrollable JTable towards the middle of it. I have a method, called collectionTableScrollPane() that generates the JScrollPane (and I know this is guaranteed to work).
I then proceed to add it to my mainPanel panel. However, I'd like there to be some forced 30px padding on the left and right of the JScrollPane. Logically, I would create a holding JPanel with a centred FlowLayout, and add Box.createHorizontalStrut(30) either side of the JScrollPane.
JPanel tableHolderPanel = new JPanel(new FlowLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30));
tableHolderPanel.add(collectionTableScrollPane());
tableHolderPanel.add(Box.createHorizontalStrut(30));
However, I'm getting a strange result, where the JScrollPane in the middle of the window (denoted by the arrows) sort of becomes ineffectual.
Does anyone know what the problem is?
Note that the JTable contains four rows, of which only two are visible.
I had some issues in the past when i used a JScrollPane inside a panel with a FlowLayout. The behaviour could be tricky, when the content grow, the horizontal scrollbar may appear or the FlowLayout should add a new line.
In your case, i will replace the FlowLayout by a BorderLayout :
JPanel tableHolderPanel = new JPanel(new BorderLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.WEST);
tableHolderPanel.add(collectionTableScrollPane(), BorderLayout.CENTER);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.EAST);
As far as I'm aware, Box is suppose to be used with the BoxLayout, this may be causing you some issues. Instead, why not use a EmptyBorder on the tableHolderPane
BoxLayout accepting size that came from JComponents, the same issue with default FlowLayout pre_implemented for JPanel
you have to returns PreferredSize by overrode JPanel nested JScrollPane,
use another LayoutManager, e.g. GridBagLayout or todays MigLayout
use NestedLayout, by using BorderLayout where you put two JLabels (e.i. that returns PreferredSize) to the EAST and WEST area
everything depends if you really to want to create the empty area and if shoud be resiziable or not

Swing behavior on splitPane

JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
scrollPane , scrollPane2);
I want to do very trivial action and I it doesn't work.
Whenever I add label to either of scrollPanes - they simply don't display anything. I can display tables within panes but these are added through the constructor. Pisses a lot.
Also if splitPane only allows two components - what if I want to separate window into four parts? SplitPane of SplitPane?
Thanks for help,
You might compare your implementation to this example that adds a JLabel instance to a JPanel on each side of the JSplitPane.
Although JSplitPane only admits two components, you can add another JSplitPane to either pane in order to subdivide either side in either direction, ad libitum.
Don't forget to call updateUI() after you've added your components. JSplitPane is really only supposed to be used for splitting into two components. If you require more, consider using a TableView.

Categories