How to insert a vertical JSeperator in a page between two categories of data using designgridlayout?

201 Views Asked by At

For example, i want to insert data in to two categories in a page.

USER         |             PASSWORDS

user1        |             ******

user2        |             *******

user3        |             *********

I have inserted the piping symbol to represent the vertical seperator. The vertical seperator should be continous though. Please help.

DesignGridLayout layout= new DesignGridLayout(Panel);

JSeparator sep = new JSeparator(JSeparator.VERTICAL);

layout.row().grid(userlabel).add(passwordlabel);
1

There are 1 best solutions below

0
On

You might do like this....

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;


public class PanelAdd extends JFrame {

    JPanel panelLabels, panelPasswords  ;

    JLabel [] userLabels ;
    JPasswordField [] passwordFields;

    public PanelAdd() {

        panelLabels = new JPanel();
        panelPasswords = new JPanel();  

        GridLayout panelsLayout = new GridLayout(0, 1, 0, 5);
        GridLayout mainLayout = new GridLayout(1, 2);

        panelLabels.setLayout(panelsLayout);
        panelPasswords.setLayout(panelsLayout);

        setLayout(mainLayout);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        setSize(350, 150);
    }

    public static void main(String [] args) {

        PanelAdd add = new PanelAdd();
        add.addControls();
        add.setVisible(true);

    }

    private void addControls() {

        userLabels = new JLabel[3];
        passwordFields = new JPasswordField[3];

        panelLabels.add(new JLabel("Users"));
        panelPasswords.add(new JLabel("Passwords"));

        for ( int i = 0 ; i < 3 ; i++) {
            userLabels[i] = new JLabel("User "+i);
            passwordFields[i] = new JPasswordField();

            panelLabels.add(userLabels[i]);
            panelPasswords.add(passwordFields[i]);
        }

        add(panelLabels);
        JSeparator sep = new JSeparator(JSeparator.VERTICAL);
        add(sep);
        add(panelPasswords);

    }

}

The GridLayout objects enable you to specify a rectangular grid in which to place the components. Each cell in the grid is the same height as the other cells, and each width is the same width as the other cells. Components are stretched both vertically and horizontally to fill the cell.