Displaying a grid as 2 parts in java - java

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.

Related

Formatting JButtons in JSplitPane

I'm having trouble getting my JButtons to appear where I want. For some reason all my buttons are appearing in the middle of the Panel when I want it to appear flushed with the top of the Panel, centered. I've tried manipulating different layout positioning such as BorderLayout.NORTH and GridBagConstraints.NORTH however I do not know if I utilizing it on the right components.
Currently my code has A Frame holding a JLayeredPane holding a JSplitPane with two JPanels in it so it has gotten quite confusing.
Do you have any suggestions on how I can get the buttons to appear on the top of the Panel?
I currently have the panel using a GridBagLayout
Read the section from the Swing tutorial on How to Use GridBagLayout for a working example and information about the constraints used by the layout manager.
For some reason all my buttons are appearing in the middle of the Panel
Specifically read the section on the weightx/y constraints, which states that the components will clump together in the center unless you use non-zero values.

Java GridLayout how to dynamically change grid layout

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
}

How to get JButton below the JTextFields

I have created a GridLayout for my GUI to show 6 rows and 4 columns. I am trying to get the two buttons I have "Calculate" and "Exit" to show in the 6th row. I only have two pane.add of four for row 5 (which is the weight label and textfield). I'm just trying to get the calculate and exit buttons to be on their own row. I tried to do pane.add(); to fill the gap and couldn't get it to work. What am I missing so that I can get the buttons on their own row?
basically I have
pane.setLayout(new GridLayout(6, 4));
pane.add(score1L);
pane.add(score1TF);
pane.add(weight1L);
pane.add(weight1TF);
pane.add(score2L);
pane.add(score2TF);
pane.add(weight2L);
pane.add(weight2TF);
pane.add(score3L);
pane.add(score3TF);
pane.add(weight3L);
pane.add(weight3TF);
pane.add(score4L);
pane.add(score4TF);
pane.add(weight4L);
pane.add(weight4TF);
pane.add(weightAvgL);
pane.add(weightAvgTF);
pane.add(calculateB);
pane.add(exitB);
Possible options:
Add empty JLabels to fill in any gaps.
Use mixed layouts -- nest JPanels, each using its own layout and add components to them. For example, the overall JPanel could use BorderLayout. The textfields/labels could be in a GridLayout or GridBagLayout using JPanel added BorderLayout.CENTER to the main JPanel. The JButtons could be held in their own GridLayout using JPanel that is then added BorderLayout.PAGE_END to the main JPanel.
Use a layout manager that allows for more complex layouts such as a GridBagLayout, or better perhaps, a 3rd party layout manager such as MigLayout.
Perhaps the best of all --use a JTable to hold your data grid, add it to a JScrollPane, and then place your buttons below the JScrollPane.

Drawing borders on a java swing component

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.

Java Swing elements sizing and positioning issue

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.

Categories