I am trying to create a character sheet program that arranges 3 panels based on how wide the frame is. If the frame is not very wide, I want the panels to be arranged vertically, but if the user chooses to make the frame wider, I would like to arrange the panels horizontally.
I also have a scrollPanel that the 3 panels are arranged on, so the scrollPanel is being added to a scrollPane.
I have seen posts that say that an eventlistener would work, however most of these are for buttons, and I need the Frame size to pass a certain threshold before the layout changes. I seen other posts that recommend html which I don't see as necessary right now.
So in this picture the 3 panels are arranged vertically, and are able to be scrolled through.
scrollPanel.add(leftPanel); //this is the yellow panel that is being added
scrollPanel.add(centerPanel); //this one is farther down
scrollPanel.add(rightPanel); //even farther down
scrollPanel.setBackground(Color.CYAN);
scrollPanel.setLayout(new GridLayout(0, 1)); //so here I would like to have an if statement or event swap the 0 and the 1.
Summary:
Can I change a GridLayout dynamically based on the size of a Frame?
How do I monitor for a Frame size change? event? if statement?
Can I do the same with a ScrollPane with setting the horizontal and vertical scroll bars to never appear based on if the panels are vertical or horizontal?
You can change the GridLayout as follows:
gr.setRows(newR);
gr.setColumns(newC);
myFrame.validate();
where gr is the layout you created somewhere and newR/newC are the new numbers of rows/columns.
Or you can say
gr=new GridLayout(newR, newC);
myFrame.setLayout(gr);
Then
if(myFrame.getWidth()>thershold) {
.. do the above
}
Related
I have a grid that is 100 square boxes. For the purposes of the program this is the same grid however for visual purposes I need to have a label through the middle of it.
I know I can declare 2 separate grids and add 2 separate panels but this would be rather annoying rework of everything just for visual purposes . I am hoping there is a way to display the first 50 boxes in 1 panel on top and add a second panel to display the other 50 boxes.
I should be more specific this is done in a GUI swing frame, not an actual print to console.
So as a summary
Current grid:
[][][][][]
[][][][][]
Desired grid:
[][][][][]
LABEL
[][][][][]
Use a JFrame with a BorderLayout.
In the BorderLayout.PAGE_START you add a panel with one grid
In the BorderLayout.CENTER you add the label
In the BorderLayout.PAGE_END you add the second panel.
Read the section from the Swing tutorial on How to Use a BorderLayout for more information and working examples.
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.
I have a UI requirement in java swing wherein I need to achieve the below:
The 2 buttons on the top are placed in a JPanel. I need to draw a line through the center of that panel, upto the beginning of the 2 buttons. The panel below is a container of panels arranged in a card layout. As and when the button is clicked, the card is switched showing another panel.
So in all respects this looks like a JTabbedPane, with one difference, the tabs are buttons arranged in the center of the tabbed pane. I need this difference for the UI I am building.
As of now, the buttons and card layout panel, looks like the below
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
Put the Border around the outer panel. That is use a panel with a BorderLayout. This panel can have a LineBorder. Then you add your button panel to the NORTH and the panel with the CardLayout to the CENTER.
The line won't be drawn through the buttons but the buttons and panel will appear like they represent on unit.
I'm trying to make a ToDoManager in java. For now I have about what I want it to be for a basic version. But I'm having a problem with the size of a panel.
I have a main JFrame. This contains a JPanel, say jPanel1.
jPanel1 has 2 buttons (add and remove) and another JPanel (say jPanel2).
jPanel2 contains a JScrollPane, which contains a modified version of JTable.
The thing I want is to tell the JTable to stretch out, so i can view everything in the JTable, and then tell the JScrollPane and jPanel2 to "Pack", or resize, so the JTable is completely vissable (if not possible the JScrollPane should do its work and draw the scrollbars).
This is what I have got at the moment:
So maybe you can see 2 problems:
1) The horizontal scroll bar does not appear. (But I did set the scroll bar: HORIZONTAL_AS_NEEDED)
2) I did not set any preferred size for the main JFrame, nor for the jPanel1, but it packs always as the same size. So I would like to stretch the jPanel2 to the full JTable, and if that would exceed the screen size, draw the scroll bars.
Using another layout manager, it's a lot easier to comprehend the usage of the JPanels and this concludes the problem.
I'm creating a Java swing app and I'm having a real hard time getting elements positioned nicely.
How would I go about making the input boxes and combo boxes to say 30px high? Also, how would I go about making the right edge of all the text line up vertically and the left edge of all the input boxes line up vertically?
I have a main JPanel which is BoxLayout.Y_AXIS, and then I have 6 JPanels on the main JPanel. These 6 JPanels are set to BorderLayout and as you can see I have used WEST for the JLabels and EAST for the input fields.
Here's a snippet of how I'm creating the 1st panel which is on top of the main panel.
private JPanel getProtocolPanel() {
protocolNumber.setBorder(BorderFactory.createLineBorder(Color.GREEN));
protocolNumberInput.setBorder(BorderFactory.createLineBorder(Color.CYAN));
protocolNumber.setVerticalAlignment(SwingConstants.TOP);
protocolPanel = new JPanel();
protocolPanel.setLayout(new BorderLayout());
protocolPanel.setBorder(BorderFactory.createLineBorder(Color.RED));
protocolPanel.add(protocolNumber, BorderLayout.WEST);
protocolPanel.add(protocolNumberInput, BorderLayout.EAST);
return protocolPanel;
}
Let me know if there is a better way to do this type of layout.
Thanks.
You are either going to have to nest layouts inside each other, or use a more complex layout (Spring, GridBag, Mig), or both to get the desired effect.