Reputation: 926
Currently I have had success implementing a grid bag layout with a jpanel and have added 7 display panels and a few "filler" panels to get a layout I wanted.
The only problem now is that these panels can change size and when they become extremely large, they affect the panels above/below or left/right of them due to the fact that the panels above/below, left/right share the same column/row index and when panel 3 (see picture) extends past its default size, the panel above/below are affected.
When there are only a few elements for the panels to display
A more regular setup
A lot (not very often) of systems
Here is the method that updates the panel. There is a lot of repetitive code with the grid constraints assignments, I should probably make a function that accepts those parameters to make it more readable.
public void updateView() {
int gridXPos, gridYPos, gridWidth, gridHeight;
int maxGridWidth = 30;
Rack r;
SuctionGroup sg;
JLabel label;
JPanel panel;
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
_Panel_MainPanel.setLayout(gbl);
_Panel_MainPanel.removeAll();
// Store panel info at top
//===========================================================
// Positioning
gridXPos = 0;
gridYPos = 0;
gridWidth = maxGridWidth;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelTop(img, storeName);
_Panel_MainPanel.add(panel, c);
// Pressure/temp
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 15;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 0;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelPressureTemps();
_Panel_MainPanel.add(panel, c);
// SEI blank
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 5;
gridHeight = 5;
// Constraints
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
label = new JLabel(" ");
//label.setBorder(border);
panel.add(label);
// performance
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos += gridHeight;
gridWidth = 10;
gridHeight = 10;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelPerformance();
_Panel_MainPanel.add(panel, c);
//===========================================================
// Filler area
gridYPos += gridHeight;
gridHeight = 10;
c.gridx = 0;
c.gridy = gridYPos;
c.gridwidth = 20;
c.gridheight = gridHeight;
c.ipady = 50;
JPanel p1 = new JPanel();
p1.setPreferredSize(new Dimension(panel.getWidth(), 225));
p1.setBackground(Color.black);
//p1.setBorder(border);
panel.add(p1, c);
//===========================================================
// Condenser
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 20;
gridHeight = 5;
c.ipady = 0;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelCondenser();
_Panel_MainPanel.add(panel, c);
// Fan images - blanks
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 2;
gridHeight = 5;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
label = new JLabel("");
//label.setBorder(border);
panel.add(label);
_Panel_MainPanel.add(label, c);
// Load and efficiency
//===========================================================
// Positioning
gridXPos += gridWidth;
//gridYPos = gridHeight;
gridWidth = 8;
gridHeight = 10;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
//c.ipady = 100;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelLE();
_Panel_MainPanel.add(panel, c);
//===========================================================
// Filler area
gridYPos += gridHeight;
gridHeight = 10;
c.gridx = 0;
c.gridy = gridYPos;
c.gridwidth = 20;
c.gridheight = gridHeight;
c.ipady = 150;
JPanel p = new JPanel();
p.setPreferredSize(new Dimension(panel.getWidth(), 225));
p.setBackground(Color.black);
//p.setBorder(border);
panel.add(p, c);
//===========================================================
// Compressor status
//===========================================================
// Positioning
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = 15;
gridHeight = 7;
// Constraints
//c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1; // No space between bottom and below row?
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
c.ipady = 0;
//c.ipady = 0;
// End of Constraints
//===========================================================
panel = panelCompressor();
_Panel_MainPanel.add(panel, c);
//==============================================================
// make labels white
setLabels(_Panel_MainPanel, Colours.White.getCol());
// do it before last panel
// Bottom Panel
//===========================================================
// Constraints
c.fill = GridBagConstraints.BOTH;
//c.weightx = 1;
c.weighty = 0; // No space between bottom and below row?
gridXPos = 0;
gridYPos += gridHeight;
gridWidth = maxGridWidth;
gridHeight = 5; // 5 per row for performance
c.gridx = gridXPos;
c.gridy = gridYPos;
c.gridwidth = gridWidth;
c.gridheight = 1;
//c.ipady = 100;
//c.ipady = 0;
// We dont setup next position because we are adding suction groups still
//gridYPos += gridHeight;
// End of Constraints
//===========================================================
panel = panelBottom(this.numRacks);
_Panel_MainPanel.add(panel, c);
_Panel_MainPanel.revalidate();
_Panel_MainPanel.repaint();
}
Question: How can I set constraints on the panels, so their sizing doesn't affect the size of panels in the same col/row indexes.
Upvotes: 1
Views: 3252
Reputation: 324118
Currently I have had success implementing a grid bag layout with a jpanel and have added 7 display panels and a few "filler" panels to get a layout I wanted.
You are never forced to use a single layout manager. So I would suggest you start with a main panel that uses a BorderLayout. Then your code would be something like:
add(header, BorderLayout.PAGE_START);
add(gridPanel, BorderLayout.CENTER);
add(footer, BorderLayout.PAGE_END);
Doesn't solve your problem, but now you are only dealing with 5 panels in the grid panel.
How can I set constraints on the panels, so their sizing doesn't affect the size of panels in the same col/row indexes.
Don't think you can. The point of a GridBagLayout is to use the information of each component in a row/column to determine the size of the cell. I tried setting the maximum size of the component but GridBagLayout does not seem to respect that.
So a possible solution is to create your own custom layout manager. It should be that difficult. You could start with a BorderLayout as a template since it allows 5 components to positions in specific locations. In your case is looks like you also have 5 components in different locations.
Upvotes: 1