import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
public class MyFrame {
private JFrame jFrame;
private JTextField firstNameTextField;
private JTextField lastNameTextField;
private JTextField addressTextField;
private JTextField ageTextField;
private JTextField zipTextField;
private JTextField cityTextField;
private JList<String> jList;
private JRadioButton rbFemale;
private JRadioButton rbMale;
private JButton button1;
private JButton button2;
private JTable table;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private List<Person> personList;
/**
* Create the frame.
*/
public MyFrame() {
personList = new ArrayList<>();
initGui();
}
private void initGui() {
jFrame = new JFrame();
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setBounds(100, 100, 628, 579);
Container contentPane = jFrame.getContentPane();
contentPane.setLayout(new MigLayout("", "[grow,fill][grow,fill][grow,fill][grow,fill][grow,fill]","[grow,fill]10[grow,fill]10[grow,fill]10[grow,fill]10[grow,fill]10[grow,fill]10[grow,fill]10[150]10[grow,fill]"));
JLabel firstNameLabel = new JLabel("Firstname");
contentPane.add(firstNameLabel, "cell 0 0");
firstNameTextField = new JTextField();
contentPane.add(firstNameTextField, "cell 1 0, span 2, width :180:");
JLabel lastNameLabel = new JLabel("Lastname");
contentPane.add(lastNameLabel, "cell 0 1");
lastNameTextField = new JTextField();
contentPane.add(lastNameTextField, "cell 1 1, span 2, width :180:");
JLabel addressLabel = new JLabel("Adress");
contentPane.add(addressLabel, "cell 0 2");
addressTextField = new JTextField();
contentPane.add(addressTextField, "cell 1 2, span 2, width :180:");
JLabel ageLabel = new JLabel("Age");
contentPane.add(ageLabel, "cell 0 3");
ageTextField = new JTextField();
contentPane.add(ageTextField, "cell 1 3, width :120:");
JLabel zipLabel = new JLabel("ZIP / City");
contentPane.add(zipLabel, "cell 0 4");
zipTextField = new JTextField();
contentPane.add(zipTextField, "cell 1 4, split 2, span 2, width :60:");
cityTextField = new JTextField();
contentPane.add(cityTextField, "growx, width :180:");
contentPane.add(getList(), "span 2 2, cell 0 5");
rbFemale = new JRadioButton("Female");
contentPane.add(rbFemale, "cell 3 5, width :120:");
rbMale = new JRadioButton("Male");
contentPane.add(rbMale, "cell 3 6, width :120:");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(rbFemale);
radioGroup.add(rbMale);
button1 = new JButton("Select");
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
contentPane.add(button1, "cell 4 5, width :80:");
button2 = new JButton("Cancel");
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
contentPane.add(button2, "cell 4 6, width :80:");
contentPane.add(getTable(), "grow, span 5, cell 0 7");
button3 = new JButton("Read");
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
readAddress();
enableButtons();
}
});
contentPane.add(button3, "cell 0 8, width :120:" );
button4 = new JButton("Save");
button4.setSize(80, 23);
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
contentPane.add(button4, "cell 1 8, width :120:");
button5 = new JButton("Remove");
button5.setSize(80, 23);
button5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
contentPane.add(button5, "cell 2 8, width :120:");
button6 = new JButton("Close");
button6.setSize(80, 23);
button6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
contentPane.add(button6, "cell 3 8, width :120:");
jFrame.pack();
jFrame.setVisible(true);
}
private JScrollPane getTable() {
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setEnabled(true);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event) {
Integer theIndex = table.getSelectedRow();
if (theIndex >= 0) {
Person person = personList.get(theIndex);
displayAddress(person);
} else {
clearAddress();
}
enableButtons();
}
});
table.setModel(new DefaultTableModel(new Object[][] {},
new String[] { "Firstname", "Lastname", "Street", "ZIP",
"Town", "Sex", "Age" }) {
Class[] columnTypes = new Class[] { String.class, String.class,
String.class, Integer.class, String.class, String.class,
Integer.class };
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(51);
table.getColumnModel().getColumn(4).setPreferredWidth(100);
table.getColumnModel().getColumn(5).setPreferredWidth(50);
table.getColumnModel().getColumn(6).setPreferredWidth(35);
//table.setBounds(80, 256, 237, 122);
JScrollPane addressScrollBar = new JScrollPane(table,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return addressScrollBar;
}
private JScrollPane getList() {
jList = new JList<>();
// jList = new JList(Person.getAvailableHobbyList());
jList.setVisibleRowCount(4);
jList.setBackground(Color.WHITE);
jList.setBounds(80, 182, 131, 63);
jList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
JScrollPane hobbiesScrollBar = new JScrollPane(jList);
hobbiesScrollBar.setBounds(80, 182, 131, 63);
return hobbiesScrollBar;
}
private void displayAddress(Person person) {
}
private void clearAddress() {
}
private boolean readAddress() {
boolean result = false;
return result;
}
private void enableButtons() {
button2.setEnabled(!personList.isEmpty());
button4.setEnabled(!personList.isEmpty());
button5.setEnabled(table.getSelectedRow() >= 0);
}
}