Reputation: 328
I have a JPanel with a JLabel, JTextField and another JPanel with a JLabel in it.
createDomainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
//createDomainPanel.setSize(600, 300);
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.gridx=0;
gbc.gridy=0;
createDomainPanel.add(new JLabel("Enter the name of the domain"), gbc);
gbc.gridx=0;
gbc.gridy=1;
createDomainPanel.add(domainName, gbc);
JPanel result = new JPanel(new FlowLayout());
result.add(successMessage);
gbc.anchor=GridBagConstraints.LAST_LINE_START;
gbc.gridx=0;
gbc.gridy=2;
createDomainPanel.add(result);
The last JLabel result prints a success message after a certain operation.
public void actionPerformed(ActionEvent e) {
SimpleDbConnect dbc = new SimpleDbConnect();
String name = "";
if (e.getSource()==domainName){
name=e.getActionCommand();
boolean success = dbc.addDomain(name);
if (success){
successMessage.setText("Domain "+ name + " added successfully");
}
}
}
However the problem is when I do get a success response, the success message instead of being at the bottom appears at the right of the first JLabel. I'm pretty new to Swings. Can anyone help me out?
Upvotes: 0
Views: 59
Reputation: 347184
You forget to pass the constraints when adding the result
s panel
createDomainPanel.add(result, gbc);
Upvotes: 2