d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Gridlayout - How To Use It Properly
Add Reply New Topic New Poll
Trade Moderator
Posts: 26,725
Joined: Dec 20 2005
Gold: 82,500.00
Trader: Trusted
Apr 29 2019 06:44am
So basiclly I have this task where I need to create a Sign up screen where I check also for Username and password validation.
If the Username is taken or has less then 4 chars I need to add an "x" near that textfield.
So without adding the functions themself I have this:

Code

pnlTop=new JPanel(new GridLayout(0,2));
// Username
lblUserName = new JLabel("Username:");
txtUserName = new JTextField(10);

// Password
lblPassword = new JLabel("Password:");
txtPassword = new JPasswordField(5);

// Retype Password
lblRetype = new JLabel("Repeat Password:");
txtRetype = new JPasswordField(5);

// ID
lblID = new JLabel("ID:");
txtID = new JTextField(5);

// Permission
lblPermission = new JLabel("Permission");
txtPermission = new JTextField(5);

//Empty Labels
lblX1 = new JLabel("");
lblX2 = new JLabel("");
lblX3 = new JLabel("");
lblX4 = new JLabel("");



// Top Panel - adding elements
pnlTop.add(lblUserName);
pnlTop.add(lblX1);
pnlTop.add(txtUserName);


pnlTop.add(lblPassword);
pnlTop.add(lblX2);
pnlTop.add(txtPassword);

pnlTop.add(lblRetype);
pnlTop.add(lblX2);
pnlTop.add(txtRetype);


pnlTop.add(lblID);
pnlTop.add(lblX3);
pnlTop.add(txtID);


pnlTop.add(lblPermission);
pnlTop.add(lblX4);
pnlTop.add(txtPermission);

// Bottom Panel -- Adding buttons
pnlBottom = new JPanel(new FlowLayout());
btnCancel = new JButton("Cancel");
btnSignUp = new JButton("Sign UP");

pnlBottom.add(btnCancel);
btnCancel.addActionListener(this);
pnlBottom.add(btnSignUp);
btnSignUp.addActionListener(this);


// adding focus listeners
txtID.addFocusListener(this);
txtPassword.addFocusListener(this);
txtUserName.addFocusListener(this);
txtRetype.addFocusListener(this);



// General Panel - adding top and bottom panels
pnlGeneral = new JPanel(new BorderLayout());
pnlGeneral.add(pnlTop,BorderLayout.NORTH);
pnlGeneral.add(pnlBottom,BorderLayout.SOUTH);

// Setting size and location of screen
setContentPane(pnlGeneral);
setLocation(600, 400);
setVisible(true);
pack();


But the problem is, when I add these "empty" panels, the grid layout breaks.
Any suggestions? :)
Member
Posts: 13,958
Joined: Dec 9 2007
Gold: 320.00
Trader: Trusted
Apr 29 2019 10:46am
You have a few problems.

1. GridLayout(0,2) specifies 0 rows and 2 columns (see its constructor). You have (I assume) 5 rows and 3 columns where each row is (label + input + x), spanning across 3 columns. Each component that you add is added horisontally, from left to right by default. See the docs: https://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html
2. You're adding the same component lblX2 twice and you're missing some components.
3. The constructor to JTextField and JPasswordField is number of "columns". Set those to 1.

You should also refrain from using any unnecessary comments and keep blocks that are "the same" together. It's easier to spot mistakes in the beginning. You should also try to learn and use design pattern as soon possible, it helps. You might think it's too hard to get started with, but I ensure you, it's way harder to "just get it working" by putting everything in the same file. I recommend learning the pattern MVC (model/view/controller) to divide your user interface (view) and backend code (model) and use a controller to "mediate" the information between the view and model. It makes the application easier to debug as well :)

Working example (missing the listeners, you'll have to readd them plus you have to to delete duplicate declarations (declarations should always be kept at the top of every function/class))

Screenshot: https://i.imgur.com/5MgdZ3T.png

Code
import javax.swing.*;
import java.awt.*;

public class Main {

public static void main(String[] args) {

JLabel lblUserName = new JLabel("Username:");
JLabel lblPassword = new JLabel("Password:");
JLabel lblRetype = new JLabel("Repeat Password:");
JLabel lblID = new JLabel("ID:");
JLabel lblPermission = new JLabel("Permission");

JTextField txtUserName = new JTextField(1);
JPasswordField txtPassword = new JPasswordField(1);
JPasswordField txtRetype = new JPasswordField(1);
JTextField txtID = new JTextField(1);
JTextField txtPermission = new JTextField(1);

JLabel lblX1 = new JLabel(" ");
JLabel lblX2 = new JLabel(" ");
JLabel lblX3 = new JLabel(" ");
JLabel lblX4 = new JLabel(" ");
JLabel lblX5 = new JLabel(" ");

// General Panel - adding top and bottom panels
JPanel pnlTop = new JPanel(new GridLayout(5, 3));
JPanel pnlGeneral = new JPanel(new BorderLayout());

pnlTop.add(lblUserName);
pnlTop.add(txtUserName);
pnlTop.add(lblX1);

pnlTop.add(lblPassword);
pnlTop.add(txtPassword);
pnlTop.add(lblX2);

pnlTop.add(lblRetype);
pnlTop.add(txtRetype);
pnlTop.add(lblX3);


pnlTop.add(lblID);
pnlTop.add(txtID);
pnlTop.add(lblX4);


pnlTop.add(lblPermission);
pnlTop.add(txtPermission);
pnlTop.add(lblX5);

// Bottom Panel -- Adding buttons
JPanel pnlBottom = new JPanel(new FlowLayout());
JButton btnCancel = new JButton("Cancel");
JButton btnSignUp = new JButton("Sign UP");

pnlBottom.add(btnCancel);
pnlBottom.add(btnSignUp);


pnlGeneral.add(pnlTop, BorderLayout.NORTH);
pnlGeneral.add(pnlBottom, BorderLayout.SOUTH);

// Setting size and location of screen
JFrame frame = new JFrame();
frame.setContentPane(pnlGeneral);
frame.setLocation(600, 400);
frame.setVisible(true);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

}
Trade Moderator
Posts: 26,725
Joined: Dec 20 2005
Gold: 82,500.00
Trader: Trusted
Apr 29 2019 11:29am
This is part of an homework assignment and we need to have all these comments :)
The problem I had is that I used the same component twice ( lblX2 ), once I changed that, everything good.


Thanks for the response buddy :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll