package my.org;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;
public class App extends JFrame {
private JTable table;
private DataTable dataTable = new DataTable();
private JButton createClientBtn = new JButton("Create client");
private JButton editClientBtn = new JButton("Edit client");
private JFrame editWindow;
private SomeData editData;
private static class SomeData implements Cloneable {
static String[] COLUMNS = { "name", "surname" };
static int COLUMNS_COUNT = 2;
private String name;
private String surname;
String getValueAt(int c) {
return (c == 0) ? name : surname;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
private List<SomeData> dataList = new ArrayList<>();
public App() throws HeadlessException {
JPanel mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(300, 300));
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
table = new JTable(dataTable);
table.setPreferredScrollableViewportSize(new Dimension(300, 200));
mainPanel.add(table);
mainPanel.add(createClientBtn);
mainPanel.add(editClientBtn);
add(mainPanel);
createClientBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editData = new SomeData();
editWindow = new EditWindow(editData, data -> {
dataList.add(data);
dataTable.fireTableDataChanged();
});
showWin(editWindow);
}
});
editClientBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow == -1)
return;
editData = dataList.get(selectedRow);
editWindow = new EditWindow(editData, data -> {
dataTable.fireTableDataChanged();
});
showWin(editWindow);
}
});
}
public static void main(String[] args) {
JFrame win = new App();
showWin(win);
}
private static void showWin(JFrame win) {
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.pack();
win.setVisible(true);
}
private class DataTable extends AbstractTableModel {
@Override
public String getColumnName(int c) {
return SomeData.COLUMNS[c];
}
@Override
public int getRowCount() {
return dataList.size();
}
@Override
public int getColumnCount() {
return SomeData.COLUMNS_COUNT;
}
@Override
public Object getValueAt(int r, int c) {
return dataList.get(r).getValueAt(c);
}
}
private static class EditWindow extends JFrame {
private JButton saveBtn = new JButton("Save");
private JButton cancelBntn = new JButton("Cancel");
private SomeData data;
private JTextField nameTxt;
private JTextField surnameTxt;
private final SomeData backupData;
public SomeData getData() {
return data;
}
public EditWindow(final SomeData someData, final Consumer<SomeData> onSaveCallback) throws HeadlessException {
super();
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setPreferredSize(new Dimension(100, 200));
this.backupData = someData;
this.data = someData;
nameTxt = new JTextField(data.name);
surnameTxt = new JTextField(data.surname);
mainPanel.add(nameTxt);
mainPanel.add(surnameTxt);
mainPanel.add(saveBtn);
mainPanel.add(cancelBntn);
add(mainPanel);
saveBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
data.name = nameTxt.getText();
data.surname = surnameTxt.getText();
onSaveCallback.accept(data);
setVisible(false);
dispose();
}
});
cancelBntn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
data = (SomeData) backupData.clone();
} catch (CloneNotSupportedException e1) {
e1.printStackTrace();
}
setVisible(false);
dispose();
}
});
}
}
}