Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java swing layout

Im having a hard time identifying what layout to be used. help any suggestions.

enter image description here

JPanel mainpanel = new JPanel();

public void addPanel(JPanel panel){
    mainpanel.add(panel);
}

addPanel(A);
addPanel(B);
addPanel(C);
addPanel(D);
addPanel(E);
....

Panels A,B,C,D... are not of fixed sized.

How can I make this possible?

like image 960
charmae Avatar asked Mar 12 '12 00:03

charmae


People also ask

What is layout in Java Swing?

Layout refers to the arrangement of components within the container. In another way, it could be said that layout is placing the components at a particular position within the container. The task of laying out the controls is done automatically by the Layout Manager.

What are the different layouts in Swing?

The most commonly used layouts are FlowLayout, BorderLayout and BoxLayout. LayoutManagers are a concept from AWT that is also used in Swing. This means they can be used in rendering AWT-based UIs as well as Swing-based UIs.

What is the default layout for Frame in Java Swing?

Java BorderLayout It is the default layout of a frame or window. The BorderLayout provides five constants for each region: public static final int NORTH.


2 Answers

Seems like by using GridBagLayout, you can achieve this. Hopefully you can change the JPanel with MAGENTA colour as per your liking :-)

Here is the output :

GRIDBAG LAYOUT TEST

And here is the code :

 import java.awt.*;
 import javax.swing.*; //swing package

 public class GridBagLayoutTest
 {


    //defining the constructor
    public GridBagLayoutTest() 
    {

        JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
        maFrame.setLocationByPlatform(true); //centering frame

        JPanel headPanel = new JPanel(); //creating the header panel

        Container container = maFrame.getContentPane();
        container.setLayout(new GridBagLayout()); //setting layout of main frame
        GridBagConstraints cns = new GridBagConstraints(); //creating constraint
        cns.gridx = 0;
        cns.gridy = 0;
        //cns.gridwidth = 3;
        //cns.gridheight = 4;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;        
        headPanel.setBackground(Color.BLUE);
        container.add(headPanel, cns);

        JPanel panel = new JPanel();
        panel.setBackground(Color.CYAN);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 0;
        //cns.gridwidth = 7;
        //cns.gridheight = 4;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel, cns);

        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.RED);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.3;
        cns.anchor = GridBagConstraints.LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel1, cns);     

        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.PINK);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel2, cns);                 

        JPanel panel4 = new JPanel();
        panel4.setBackground(Color.ORANGE);
        cns = new GridBagConstraints();
        cns.gridx = 1;
        cns.gridy = 2;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LINE_END;
        cns.fill = GridBagConstraints.BOTH;
        container.add(panel4, cns); 

        JPanel mainPanel = new JPanel();
        mainPanel.setBackground(Color.WHITE);
        mainPanel.setLayout(new GridBagLayout());
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 4;
        cns.gridwidth = 2;
        cns.gridheight = 2;
        cns.weightx = 1.0;
        cns.weighty = 0.3;
        cns.anchor = GridBagConstraints.LAST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        container.add(mainPanel, cns); 

        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.MAGENTA);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 0;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 0.5;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.FIRST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        mainPanel.add(panel3, cns); 

        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.WHITE);
        cns = new GridBagConstraints();
        cns.gridx = 0;
        cns.gridy = 1;
        //cns.gridwidth = 2;
        //cns.gridheight = 2;
        cns.weightx = 1.0;
        cns.weighty = 0.2;
        cns.anchor = GridBagConstraints.LAST_LINE_START;
        cns.fill = GridBagConstraints.BOTH;
        mainPanel.add(bottomPanel, cns); 

        //JButton button = new JButton("BUTTON");
        //headPanel.add(button);

        maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
        maFrame.pack();
        maFrame.setVisible(true); //making the frame visible
    }

    //defining the main method
    public static void main(String[] args) 
    {
        Runnable runnable = new Runnable()
        {
            public void run()
            {
                new GridBagLayoutTest(); //instantiating the class
            }
        };
        SwingUtilities.invokeLater(runnable);       
    }
}
like image 74
nIcE cOw Avatar answered Sep 29 '22 18:09

nIcE cOw


Consider using JGoodies Forms layout. it is highly flexible and easy to use. Any sort of layouting of widgets are possible, vertical stacking , horizontal stacking etc.

http://www.jgoodies.com/freeware/forms/index.html

enter image description here

                   /* B1   Space  B2    Space B3    Space  B4 */
     String col1 = "10dlu, 3dlu, 10dlu, 3dlu 10dlu, 3dlu, 10ldu";
                   /*Width of button */ 
     String row1 = "5dlu";

     FormLayout layout = new FormLayout( col1, row1);

     JPanel panel = new JPanel(layout); 

     panel.setBorder(Borders.DIALOG_BORDER);

     CellConstraints cc = new CellConstraints();

     /* X - stands for column position :: Y - stand for row */  
     panel.add(new JButton("B1"), cc.xy(1, 1));

     panel.add(new JButton("B2"), cc.xy(2, 1));
     .
     .
     . 
     there is one more cc.xyh(1,1,2) where 'h' stands for vertical span or width.   

Yes, you read more about JGoodies forms layout, 'DLU' means dialog units, rather than me explaining all these, kindly visit JGoodies site, download the library and documentation. Trust be, this layout manager is much better, in terms of usability , maintainability and readability.

like image 20
alkber Avatar answered Sep 29 '22 19:09

alkber