Skip to content

Instantly share code, notes, and snippets.

@Cazzar
Created November 18, 2014 15:32
Show Gist options
  • Save Cazzar/c6ac2de3f128224caa7a to your computer and use it in GitHub Desktop.
Save Cazzar/c6ac2de3f128224caa7a to your computer and use it in GitHub Desktop.
package net.cazzar.itech2100.assign2.dialogues;
import assignment2.Property;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
/**
* @author Cayde
*/
public class NewPropertyDialogue extends JDialog {
private JLabel lblStreet = new JLabel("Street: ");
private JTextField txtStreet = new JTextField();
private JLabel lblSuburb = new JLabel("Suburb: ");
private JTextField txtSuburb = new JTextField();
private JLabel lblPostcode = new JLabel("Postcode: ");
private JTextField txtPostcode = new JTextField();
private JLabel lblState = new JLabel("State: ");
private JComboBox<String> cbState = new JComboBox<>();
private JButton btnOkay = new JButton("Okay");
private JButton btnCancel = new JButton("Cancel");
private boolean response = false;
public NewPropertyDialogue(Frame owner) {
super(owner, true);
setTitle("Create new Property");
initUI();
setupComponents();
pack();
setMinimumSize(getPreferredSize());
}
private void setupComponents() {
}
private void initUI() {
JPanel panel = new JPanel();
getContentPane().add(panel);
GroupLayout layout = new GroupLayout(panel);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
panel.setLayout(layout);
txtStreet.setPreferredSize(new Dimension(500, 20));
cbState.addItem("Victoria");
cbState.addItem("Queensland");
cbState.addItem("New South Wales");
cbState.addItem("Tasmania");
cbState.addItem("Western Australia");
cbState.addItem("South Australia");
cbState.addItem("Australian Capital Territory");
cbState.addItem("Northern Territory");
layout.setHorizontalGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(lblStreet)
.addComponent(lblSuburb)
.addComponent(lblPostcode)
.addComponent(lblState)
.addComponent(btnCancel)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
.addComponent(txtStreet)
.addComponent(txtSuburb)
.addComponent(txtPostcode)
.addComponent(cbState)
.addComponent(btnOkay)
)
);
layout.setVerticalGroup(
layout.createSequentialGroup().addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblStreet)
.addComponent(txtStreet)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblSuburb)
.addComponent(txtSuburb)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblPostcode)
.addComponent(txtPostcode)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(lblState)
.addComponent(cbState)
).addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(btnCancel) //I lay it out this due to ease.
.addComponent(btnOkay)
)
);
btnCancel.addActionListener(
new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
}
);
btnOkay.addActionListener(
new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (!validateData()) return;
response = true;
setVisible(false);
}
}
);
}
private boolean validateData() {
if (txtStreet.getText() == null || txtStreet.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Street can not be empty!", "Error!", JOptionPane.ERROR_MESSAGE);
return false;
} else if (txtSuburb.getText() == null || txtSuburb.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Suburb can not be empty!", "Error!", JOptionPane.ERROR_MESSAGE);
return false;
} else if (txtPostcode.getText() == null || txtPostcode.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Postcode can not be empty!", "Error!", JOptionPane.ERROR_MESSAGE);
return false;
}
return true;
}
public boolean getResponse() {
return response;
}
public Property getCreatedProperty() {
return new Property(String.format(
"%s %s %s %s", txtStreet.getText(), txtSuburb.getText(), txtPostcode.getText(),
cbState.getSelectedItem()
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment