Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What Layout Manager to use here

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What Layout Manager to use here

    Hi,

    I have been working on the following Java GUI class. All it does is display a JScrollPane, and within that pane displays arrays of numbers, each of which has an associated label. The main class is as follows:-


    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.BoxLayout;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.Border;
    import javax.swing.BorderFactory;
     
     
     
    /**
     * Write a description of class MatrixDisplayPanel here.
     *
     * @author Jeremy Watts
     * @version 23-01-2019
     */
    public class MatrixDisplayPanel extends JPanel
    {
        // instance variables
        int rowNumber;
        int columnNumber;
     
     
        /**
         * Constructor for objects of class MatrixDisplayFrame
         */
        public MatrixDisplayPanel(String[][] jLabelString, String matrixLabel)
        {
            // initialise instance variables
            this.rowNumber = jLabelString.length;
            this.columnNumber = jLabelString[jLabelString.length - 1].length;
            create(jLabelString, matrixLabel);
        }
     
        /**
         * An example of a method - replace this comment with your own
         *
         * @param  y  a sample parameter for a method
         * @return    the sum of x and y
         */
        private void create(String[][] jLabelString, String matrixLabel) {
     
            JPanel gridPanel;
            JLabel[][] jLabel;
     
            int rowIndex;
            int columnIndex;
     
            gridPanel = new JPanel(new GridLayout(this.rowNumber, this.columnNumber, 15, 15));
     
            jLabel = new JLabel[this.rowNumber][this.columnNumber];
            Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
            for(rowIndex = 0; rowIndex <= this.rowNumber - 1; rowIndex++)
            {
                for(columnIndex = 0; columnIndex <= this.columnNumber - 1; columnIndex++)
                {
                    jLabel[rowIndex][columnIndex] = new JLabel();
                    jLabel[rowIndex][columnIndex].setText(jLabelString[rowIndex][columnIndex]);
                    jLabel[rowIndex][columnIndex].setFont(new java.awt.Font("Tahoma", 0, 15));
                    jLabel[rowIndex][columnIndex].setBorder(border);
                    gridPanel.add(jLabel[rowIndex][columnIndex]);
                }
            }
     
            gridPanel.setOpaque(true);
            gridPanel.setBackground(Color.GREEN);
     
     
     
            JPanel mainPanel;
            mainPanel = new JPanel();
            mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
            mainPanel.add(gridPanel);
     
     
            mainPanel.setOpaque(true);
            mainPanel.setBackground(Color.RED);
     
            JPanel labelPanel = new JPanel();
            JLabel label = new JLabel(matrixLabel + "    = ");
            label.setFont(new java.awt.Font("Tahoma", 0, 18));
     
            labelPanel.setOpaque(true);
            labelPanel.setBackground(new Color(232, 232, 232));
     
            labelPanel.add(label);
            labelPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
     
            this.add(labelPanel);
            this.add(mainPanel);
     
     
            this.setOpaque(true);
            this.setBackground(Color.BLUE);
     
        }
     
    }




    The following class, called 'ResultsPanel' , then takes instances of the above, and puts them onto a JPanel using BoxLayout as the Layout Manager. It is as follows:-

    import java.awt.Component;
    import javax.swing.JPanel;
    import javax.swing.BoxLayout;
    import javax.swing.Box;
    import java.awt.Dimension;
     
    /**
     * Is the JPanel that holds all currently declared matrices
     *
     * @author Jeremy Watts
     * @version 24-01-2019
     */
    public class ResultsPanel extends JPanel
    {
        // instance variables
        private int bufferHeight = 50;
        private int numberOfResults;
     
     
        /**
         * Constructor for objects of class MatricesPanel
         */
        public ResultsPanel()
        {
            this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            this.numberOfResults = 0;
        }
     
        /**
         * An example of a method - replace this comment with your own
         *
         * @param  y  a sample parameter for a method
         * @return    the sum of x and y
         */
        public void addMatrixDisplayPanel(MatrixDisplayPanel matrixDisplayPanel)
        {
            matrixDisplayPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
     
            this.add(matrixDisplayPanel);
            this.numberOfResults++;
        }
    }



    Finally, an instantiator class is used:-

    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.Box;
    import java.awt.Dimension;
     
    /**
     * Write a description of class Run2 here.
     *
     * @author (your name)
     * @version (a version number or a date)
     */
    public class Run2
    {
        public static void main(String[] args)
        {
     
            String[][] jLabelString = new String[4][4];
            jLabelString[0][0] = "    0.0 + 9.67i";
            jLabelString[0][1] = "2.6598663";
            jLabelString[0][2] = "100234";
            jLabelString[0][3] = "1.09864653";
     
            jLabelString[1][0] = "-2/4 + 8/9i";
            jLabelString[1][1] = "2095.6789457";
            jLabelString[1][2] = "-2845";
            jLabelString[1][3] = "8.906343";
     
            jLabelString[2][0] = "-0.00989";
            jLabelString[2][1]= "9.9067";
            jLabelString[2][2] = "1.56";
            jLabelString[2][3] = "7.894557446";
     
            jLabelString[3][0] = "-0.00989";
            jLabelString[3][1]= "9.9067";
            jLabelString[3][2] = "1.56";
            jLabelString[3][3] = "1.56";
     
     
     
     
            MatrixDisplayPanel mdp1 = new MatrixDisplayPanel(jLabelString, "A");
     
     
            String[][] jLabelString2 = new String[5][5];
            jLabelString2[0][0] = "    0.0 + 9.67i";
            jLabelString2[0][1] = "2.6598663";
            jLabelString2[0][2] = "100234";
            jLabelString2[0][3] = "1.09864653";
            jLabelString2[0][4] = "1.09864653";
     
            jLabelString2[1][0] = "-2/4 + 8/9i";
            jLabelString2[1][1] = "2095.6789457";
            jLabelString2[1][2] = "-2845";
            jLabelString2[1][3] = "8.906343";
            jLabelString2[1][4] = "8.906343";
     
            jLabelString2[2][0] = "-0.00989";
            jLabelString2[2][1]= "9.9067";
            jLabelString2[2][2] = "1.56";
            jLabelString2[2][3] = "7.894557446";
            jLabelString2[2][4] = "7.894557446";
     
            jLabelString2[3][0] = "-0.00989";
            jLabelString2[3][1]= "9.9067";
            jLabelString2[3][2] = "1.56";
            jLabelString2[3][3] = "1.56";
            jLabelString2[3][4] = "1.56";
     
            jLabelString2[4][0] = "-0.00989";
            jLabelString2[4][1]= "9.9067";
            jLabelString2[4][2] = "1.56";
            jLabelString2[4][3] = "1.56";
            jLabelString2[4][4] = "1.56";
     
     
            MatrixDisplayPanel mdp2 = new MatrixDisplayPanel(jLabelString2, "B");
            /*JScrollPane jsp = new JScrollPane(new MatrixDisplayPanel(jLabelString, "A"));
            mdf.add(jsp);*/
     
     
     
     
     
     
            String[][] jLabelString3 = new String[6][6];
            jLabelString3[0][0] = "    0.0 + 9.67i";
            jLabelString3[0][1] = "2.6598663";
            jLabelString3[0][2] = "100234";
            jLabelString3[0][3] = "1.09864653";
            jLabelString3[0][4] = "1.09864653";
            jLabelString3[0][5] = "1.09864653";
     
            jLabelString3[1][0] = "-2/4 + 8/9i";
            jLabelString3[1][1] = "2095.6789457";
            jLabelString3[1][2] = "-2845";
            jLabelString3[1][3] = "8.906343";
            jLabelString3[1][4] = "8.906343";
            jLabelString3[1][5] = "8.906343";
     
            jLabelString3[2][0] = "-0.00989";
            jLabelString3[2][1]= "9.9067";
            jLabelString3[2][2] = "1.56";
            jLabelString3[2][3] = "7.894557446";
            jLabelString3[2][4] = "7.894557446";
            jLabelString3[2][5] = "7.894557446";
     
            jLabelString3[3][0] = "-0.00989";
            jLabelString3[3][1]= "9.9067";
            jLabelString3[3][2] = "1.56";
            jLabelString3[3][3] = "1.56";
            jLabelString3[3][4] = "1.56";
            jLabelString3[3][5] = "1.56";
     
            jLabelString3[4][0] = "-0.00989";
            jLabelString3[4][1]= "9.9067";
            jLabelString3[4][2] = "1.56";
            jLabelString3[4][3] = "1.56";
            jLabelString3[4][4] = "1.56";
            jLabelString3[4][5] = "1.56";
     
            jLabelString3[5][0] = "-0.00989";
            jLabelString3[5][1]= "9.9067";
            jLabelString3[5][2] = "1.56";
            jLabelString3[5][3] = "1.56";
            jLabelString3[5][4] = "1.56";
            jLabelString3[5][5] = "1.56";
     
            MatrixDisplayPanel mdp3 = new MatrixDisplayPanel(jLabelString3, "C");
     
     
     
            ResultsPanel res = new ResultsPanel();
            res.addMatrixDisplayPanel(mdp1);
     
            res.addMatrixDisplayPanel(mdp2);
     
            res.addMatrixDisplayPanel(mdp3);
     
     
            JFrame mdf = new JFrame();
            JScrollPane jsp = new JScrollPane(res);
     
            mdf.add(jsp);
            mdf.pack();
            mdf.setVisible(true);
        }
    }



    If you run the code, then you can see 3 arrays of numbers, each with the labels 'A','B' and 'C'. Basically, what I am trying to do is to get the 'A' and 'B' arrays to line up with the bottom one. I simply want it always to be the case that the labels for any array is far flush to the left and always in line with eachother. (P.S. I have coloured the various panels used to make it easier to discern what's going on)

    My intention is to get the 'labels' all linedScreenshot from 2019-01-25 12-14-47.jpg up flush to the left, all in line. I thought that setting the alignment to LEFT_ALIGNMENT would have this effect, but apparently not.

    Jeremy

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: What Layout Manager to use here

    Hello,

    As unpleasant as it is, I would urge you to bite the bullet and get very familiar with the GridBagLayout. It's the most complex and daunting layout manager, but with great responsibility comes great power.

    The start of your problem is in MatrixDisplayPanel. Here's your create() function but with my modifications to use GridBagLayout instead (near the bottom):
        /**
         * An example of a method - replace this comment with your own
         *
         * @param  y  a sample parameter for a method
         * @return    the sum of x and y
         */
        private void create(String[][] jLabelString, String matrixLabel) {
     
            JPanel gridPanel;
            JLabel[][] jLabel;
     
            int rowIndex;
            int columnIndex;
     
            gridPanel = new JPanel(new GridLayout(this.rowNumber, this.columnNumber, 15, 15));
     
            jLabel = new JLabel[this.rowNumber][this.columnNumber];
            Border border = BorderFactory.createLineBorder(Color.GRAY, 1);
            for(rowIndex = 0; rowIndex <= this.rowNumber - 1; rowIndex++)
            {
                for(columnIndex = 0; columnIndex <= this.columnNumber - 1; columnIndex++)
                {
                    jLabel[rowIndex][columnIndex] = new JLabel();
                    jLabel[rowIndex][columnIndex].setText(jLabelString[rowIndex][columnIndex]);
                    jLabel[rowIndex][columnIndex].setFont(new java.awt.Font("Tahoma", 0, 15));
                    jLabel[rowIndex][columnIndex].setBorder(border);
                    gridPanel.add(jLabel[rowIndex][columnIndex]);
                }
            }
     
            gridPanel.setOpaque(true);
            gridPanel.setBackground(Color.GREEN);
     
     
     
            JPanel mainPanel;
            mainPanel = new JPanel();
            mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20));
            mainPanel.add(gridPanel);
     
     
            mainPanel.setOpaque(true);
            mainPanel.setBackground(Color.RED);
     
            JPanel labelPanel = new JPanel();
            JLabel label = new JLabel(matrixLabel + "    = ");
            label.setFont(new java.awt.Font("Tahoma", 0, 18));
     
            labelPanel.setOpaque(true);
            labelPanel.setBackground(new Color(232, 232, 232));
     
            labelPanel.add(label);
            labelPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
     
    		// Begin new code
     
    		GridBagLayout gbl = new GridBagLayout();
    		GridBagConstraints gbc = new GridBagConstraints();
    		this.setLayout(gbl);
     
    		this.add(labelPanel);
    		gbc.anchor = GridBagConstraints.WEST;
    		gbl.setConstraints(labelPanel, gbc);
     
    		this.add(mainPanel);
    		gbc = new GridBagConstraints();
    		gbc.fill = GridBagConstraints.BOTH;
    		gbc.weightx = 1;
    		gbc.weighty = 1;
    		gbl.setConstraints(mainPanel, gbc);
     
    		// End new code
     
            this.setOpaque(true);
            this.setBackground(Color.BLUE);
     
        }
    So the MatrixDisplayPanel now has GridBagLayout as the layout manager. After adding each component (labelPanel, mainPanel), we call setConstraints() on the GridBagLayout to position the component using the passed-in GridBagConstraints. This will get your A=, B=, and C= labels positioned to the far left like you want. But then you'll see that your label grids are not filling. That's because 'mainPanel' is also not using GridBagLayout to position 'labelPanel' with fills. You should be able to use use this example and do the same thing to 'mainPanel'. Give 'mainPanel' a GridBagLayout and create a GridBagConstraints for it which has fill, weightx, and weighty set up properly. Then the label grids should fill their entire containers regardless of how many columns each one has.

Similar Threads

  1. [SOLVED] Layout Manager and Keyboard Panel
    By DOLZero in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 22nd, 2012, 12:19 PM
  2. Problem with layout manager
    By mDennis10 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 4th, 2011, 07:47 PM
  3. Layout Manager
    By mDennis10 in forum AWT / Java Swing
    Replies: 4
    Last Post: September 3rd, 2011, 10:27 PM
  4. Layout manager
    By kurt-hardy in forum AWT / Java Swing
    Replies: 3
    Last Post: January 19th, 2011, 10:25 AM
  5. float based layout manager?
    By deepthought in forum AWT / Java Swing
    Replies: 1
    Last Post: January 1st, 2011, 10:11 PM