0

I posted a question asking about some other techniques to format panels (previous question) and was told to post my code to help get specific help.

My project contains a bunch of different classes so I reduced the problem to one class in order to make it easier to help me out.

First, i will post what I am looking to do. Current look Wanted look (im sorry i suck at paint)

Racks - Contain a number of fans and a number of suction group. For each suction group there will be set of compressors and a set of systems. So if a rack has 3 suction groups, it will have 3 sets of compressors, and 3 sets of systems.

I would like each column to be a rack. So if a rack has 3 suction groups, they are all in the same column (see rack 5 - larger than the rest because it has 2 suction groups but both systems + comps are in the same column as the title + fans).

I also want the suctiongroups (comp + systems) to be directly under the largest fan group. In the second picture i moved them to be around the same starting point (just under the rack 1 fans). Ideally the backgrounds of these panels would extend to the max size so there is no white space.

I also cant get the first row to have the labels at the top of panel, instead they are centered. I want them at the top because there will be an image going inside these boxes.

It is also key that there are spaces in between the panel borders and the text as small on/off icons will be linked to each label.

My code looks different from the pictures I linked but implementation will be similar to my code.

Main

package help;

import javax.swing.JFrame;

/**
 *
 * @author Eric
 */
public class Help {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        JFrame frame = new JFrame();
        BackgroundMain bg = new BackgroundMain();
        bg.updateRacks();
        frame.add(bg);
        frame.revalidate();
        frame.pack();
        frame.repaint();
        frame.setVisible(true);
    }
    
}

BackgroundMain

package help;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import static java.awt.GridBagConstraints.LINE_START;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author Eric
 */
public class BackgroundMain extends javax.swing.JPanel {

    /**
     * Creates new form BackgroundMain
     */
    public BackgroundMain() {
        initComponents();
    }


    public int numRacks;
    public String[] racks; // Class
    public Font font;
    public Border border;

    public void updateRacks(){
        this.racks = new String []{"e", "e2", "e3"};
        this.numRacks = 3;   
        Color color = Color.RED;        
        border = BorderFactory.createLineBorder(color);
        this.updateView();
    }

    public void updateRacks(String[] racks, int numRacks, Font font, Border border) {
        this.racks = racks;
        this.numRacks = numRacks;
        this.font = font;
        this.border = border;
        this.updateView();
    }

    public void updateFont(Font font) {
        this.font = font;
        this.updateView();
    }

    public void updateBorder(Border border) {
        this.border = border;
        this.updateView();
    }

    public void updateFontBorder(Font font, Border border) {
        this.font = font;
        this.border = border;
        this.updateView();
    }

    // remove int i to be the called function
    public void updateView() {

        // Vars used        
        int gridXPos = 0, gridYPos = 0, gridWidth = 0;
        String r;        
        JLabel label;
        JPanel panel = new JPanel();
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        _Panel_MainPanel.setLayout(gbl);
        _Panel_MainPanel.removeAll();

        // First initial cell
        label = new JLabel("Outside Air Temp");
        ////label.setFont(font);
        c.fill = GridBagConstraints.BOTH;
        //c.anchor = FIRST_LINE_START;
        c.weightx = 1;
        c.weighty = 0; // No space between bottom and below row?

        c.gridx = gridXPos;
        c.gridy = gridYPos;

        //c.gridwidth = 2;
        //c.gridheight = 5;
        c.ipady = 100;
        panel.add(label);
        panel.setBorder(border);
        _Panel_MainPanel.add(panel, c);

        c.ipady = 0;
        c.gridheight = 1;
        // Add 2 grid spots
        gridXPos += 2;

        // rack names + SEI, they have their own panels, incase we want borders
        for (int i = 0; i < this.numRacks; i++) {

            r = racks[i];
            // Number of suction groups for the rack            

            panel = panelRackName("Rack NAME EXAMPLE");
            c.gridx = gridXPos;
            // We have panel, now format for the panel

            gridWidth = (3 * 1); 
            c.gridwidth = gridWidth;
            gridXPos += gridWidth;

            _Panel_MainPanel.add(panel, c);

        }

        //===========================
        // RACKS STATUS
        //===========================
        gridXPos = 0;
        gridYPos = 15;
        // Rack status
        label = new JLabel("Condenser Fans");
        ////label.setFont(font);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = LINE_START;
        c.weightx = 1;
        //c.weighty = 0.00;

        c.gridx = gridXPos;
        c.gridy = gridYPos;
        c.gridwidth = 2;
        //c.ipady = 200;
        panel = new JPanel();
        panel.add(label);
        panel.setBorder(border);
        _Panel_MainPanel.add(panel, c);

        gridXPos += 2;
        c.ipady = 0;

        // Add condensers
        for (int i = 0; i < this.numRacks; i++) {
            c.weighty = 1;
            r = racks[i];
            // Number of suction groups for the rack
            //evenSG = r.getNumSuctionGroups() % 2 == 0;

            //===========================
            // RACK CONDENSER
            //===========================
            panel = panelCondenser(5, 2) ; //r.getNumCondenserFans(), r.getNumSuctionGroups());

            //c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = gridXPos;
            c.gridy = gridYPos;

            // Even number of suction groups
            // This means we allocate 3 rows x num of suction groups
            gridWidth = (3 * 1); // r.getNumSuctionGroups());
            c.gridwidth = gridWidth;

            _Panel_MainPanel.add(panel, c);
            c.gridy = gridYPos + 2;
            // Suction Groups - Compressors then Systems   
            for (int j = 0; j < 1; j++) { // r.numSuctionGroups; j++) {

                //sg = r.getSuctionGroupIndex(j);

                // Compressor
                panel = panelCompressor(5, (j + 1));

                c.gridx = gridXPos;
                //c.fill = GridBagConstraints.HORIZONTAL;

                gridWidth = 1;
                c.gridwidth = gridWidth;
                gridXPos += gridWidth;

                _Panel_MainPanel.add(panel, c);
                // System

                panel = panelSystems(4, (j + 1));
                c.gridx = gridXPos;

                // This means we allocate 3 rows x num of suction groups
                gridWidth = 2;
                c.gridwidth = gridWidth;
                gridXPos += gridWidth;

                _Panel_MainPanel.add(panel, c);

            }
        }

        //===========================
        // Performance
        //===========================
        gridXPos = 0;

        // Rack status
        label = new JLabel("Performance");
        ////label.setFont(font);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = LINE_START;
        c.weightx = 1;
        c.weighty = 0.00;

        c.gridx = gridXPos;
        c.gridy = gridYPos;
        c.gridwidth = 2;
        //c.ipady = 200;
        panel = new JPanel();
        panel.add(label);
        label = new JLabel("Predicted");
        ////label.setFont(font);
        c.gridx = gridXPos;
        c.gridy = gridYPos + 1;
        panel.add(label);
        label = new JLabel("Actual");
        //label.setFont(font);
        c.gridx = gridXPos;
        c.gridy = gridYPos + 2;
        panel.add(label);
        label = new JLabel("Difference");
        c.gridx = gridXPos;
        c.gridy = gridYPos + 3;
        //label.setFont(font);
        panel.add(label);
        panel.setBorder(border);
        _Panel_MainPanel.add(panel, c);
        _Panel_MainPanel.setVisible(true);
        _Panel_MainPanel.revalidate();
        _Panel_MainPanel.repaint();

    }

    public JPanel panelRackName(String rackName) {

        JLabel label;
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        // Return a panel containing two labels
        JPanel panel = new JPanel(gbl);
        //===========================
        // RACK NAMES
        //===========================

        label = new JLabel(rackName);
        //label.setFont(font);
        label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.weightx = 1;
        c.gridy = 0;
        c.ipady = 100;
        //c.gridheight = 5;
        //label.setOpaque(true);
        //label.setBackground(new java.awt.Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)));
        panel.add(label, c);

        // rack SEI
        label = new JLabel("SEI");
        //label.setFont(font);
        label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.gridx = 1;

        c.gridy = 0;
        //c.gridheight = 2;

        //label.setOpaque(true);
        //label.setBackground(new java.awt.Color((int) (Math.random() * 256), (int) (Math.random() * 256), (int) (Math.random() * 256)));
        panel.add(label, c);

        panel.setBorder(border);

        return panel;
    }

    public JPanel panelCondenser(int numCond, int numSg) {

        // Condenser Panel will list the condensers 
        JLabel label;
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        // Return a panel containing condenser labels
        JPanel panel = new JPanel(gbl);
        int numCols = numSg + 1;
        double numPerCol = Math.ceil((double) numCond / numCols);
        //System.out.println("Ceil of " + numCond + "/" + numCols + "=" + numPerCol);

        int numAdded = 0;
        //===========================
        // RACK CONDENSER
        //===========================
        //label = new JLabel("Condensers");
        ////label.setFont(font);
        //label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        c.fill = GridBagConstraints.HORIZONTAL;
        //c.gridx = 0;
        c.weightx = 1;
        //c.gridy = 0;
        //c.gridheight = 1;
        //panel.add(label, c);

        int colIndex = 0;
        int rowIndex = 2;

        for (int i = 1; i <= numCond; i++) {

            if (numAdded < numPerCol) {
                c.gridx = colIndex;
                c.gridy = rowIndex++;
                numAdded++;
            } else {
                numAdded = 1;
                colIndex++;
                rowIndex = 2;
                c.gridx = colIndex;
                c.gridy = rowIndex++;
            }

            label = new JLabel("Fan " + i);
            //label.setFont(font);
            c.fill = GridBagConstraints.HORIZONTAL;
            label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
            panel.add(label, c);
        }

        panel.setBorder(border);
        return panel;
    }

    public JPanel panelCompressor(int numComp, int sgNum) {

        // Condenser Panel will list the condensers 
        JLabel label;
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        // Return a panel containing condenser labels
        JPanel panel = new JPanel(gbl);

        //===========================
        // RACK COMPRESSOR
        //===========================
        //label = new JLabel("SG " + sgNum + " Comp");
        ////label.setFont(font);
        //label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.weightx = 1;
        c.gridy = 0;
        c.gridheight = 1;
        //panel.add(label, c);

        //==========================================================
        //                  Compressors
        //==========================================================
        for (int i = 1; i <= numComp; i++) {
            label = new JLabel("Comp " + i);
            label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
            c.gridx = 0;
            c.gridy = i;
            //label.setFont(font);
            panel.add(label, c);
        }

        panel.setBorder(border);
        return panel;
    }

    public JPanel panelSystems(int numSystems, int sgNum) {

        // Condenser Panel will list the condensers 
        JLabel label;
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        // Return a panel containing condenser labels
        JPanel panel = new JPanel(gbl);
        //==========================================================
        // SYSTEMS
        //==========================================================
        label = new JLabel("SG " + sgNum + " Sys");

        label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.weightx = 1;
        c.gridy = 0;
        c.gridheight = 1;

        //label.setFont(font);
        panel.add(label, c);
        //c.gridheight = 8;

        for (int i = 1; i <= numSystems; i++) {
            label = new JLabel("SYSTEM NAME EXAMPLE");
            label.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
            c.gridx = 0;
            c.gridy = i;
            //label.setFont(font);
            panel.add(label, c);
        }
        panel.setBorder(border);
        return panel;
    }

    public JPanel panelPerformance() {
        // Condenser Panel will list the condensers 
        JLabel label;
        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        // Return a panel containing condenser labels
        JPanel panel = new JPanel(gbl);

        return panel;

    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        _Panel_MainPanel = new javax.swing.JPanel();

        javax.swing.GroupLayout _Panel_MainPanelLayout = new javax.swing.GroupLayout(_Panel_MainPanel);
        _Panel_MainPanel.setLayout(_Panel_MainPanelLayout);
        _Panel_MainPanelLayout.setHorizontalGroup(
            _Panel_MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 683, Short.MAX_VALUE)
        );
        _Panel_MainPanelLayout.setVerticalGroup(
            _Panel_MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(_Panel_MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(_Panel_MainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JPanel _Panel_MainPanel;
    // End of variables declaration                   

}
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Eric G
  • 928
  • 1
  • 9
  • 29
  • 3
    There is no need to only use a single layout manager. Each panel can contain its own layout manager. – camickr Oct 06 '15 at 15:07
  • 1. [GBC is column based...](http://stackoverflow.com/a/32277227/714968). 2. MigLayout can do that, 3. don't place bunch of JComponents to one JPanel, – mKorbel Oct 06 '15 at 17:50

1 Answers1

0

I suggest you use a Table instead of working with single labels and GridBagLayout.

It might be possible to use Colspan, too, see: Jtable Row Span and Column Span

If its just a View (No Buttons, etc.), you could also consider to use a JEditorPane and render your View with HTML, that might be the easiest way.

Community
  • 1
  • 1
hinneLinks
  • 3,673
  • 26
  • 40