How to add components line by line?
I want to add components in JPanel line by line.
I have tried several layouts including GridLayout, GridBagLayout and BorderLayout. But 'new line' arrangement of components is not happening..
Here is my code:
Code:
class Login extends JFrame implements ActionListener
{
JButton login,register;
JPanel panel;
JLabel label1,label2;
public JTextField text1,text2;
RegisterAction regAction = null;
public Login()
{
label1 = new JLabel();
regAction = new RegisterAction();
label1.setText("Username:");
text1 = new JTextField(15);
label2 = new JLabel();
label2.setText("Password:");
text2 = new JPasswordField(15);
login=new JButton("Login");
register = new JButton("Register");
panel=new JPanel(new GridBagLayout());
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(login);
panel.add(register);
add(panel,BorderLayout.CENTER);
login.addActionListener(this);
register.addActionListener(regAction);
setTitle("File Jamming Synthesizer");
}
This one is of JFrame, I have tried out Jpanel too..In both cases anot working..
Re: How to add components line by line?
Look up the BoxLayout, but you'll want to read the whole tutorial and API on it as it has some tricky parts. For that matter, you'll want to read the whole layout manager tutorial as well.
Re: How to add components line by line?
Even with GridLayout you can add component line by line, in each row.