//Customer Class
public class Customer {
private String name;
private String address;
private double balance;
public Customer(String name, String address, double balance) {
super();
this.name = name;
this.address = address;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Customer [address=" + address + ", balance=" + balance
+ ", name=" + name + "]";
}
}
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class CustomerDialog extends JDialog {
private JLabel nameLabel = new JLabel("Name");
private JTextField nameTextField = new JTextField(20);
private JLabel addressLabel = new JLabel("Address");
private JTextField addressTextField = new JTextField(20);
private JLabel balanceLabel = new JLabel("Balance");
private JTextField balanceTextField = new JTextField(20);
private JButton okButton = new JButton("Ok");
private JButton cancelButton = new JButton("Cancel");
private Customer customer;
public CustomerDialog(Frame owner,boolean modal){
super(owner,modal);
this.setLayout(new MigLayout());
this.add(nameLabel);
this.add(nameTextField,"wrap");
this.add(addressLabel);
this.add(addressTextField,"wrap");
this.add(balanceLabel);
this.add(balanceTextField,"wrap paragraph");
this.add(okButton,"split 2,span 2,tag ok");
this.add(cancelButton,"tag cancel");
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String name = new TextField.getText();
String address = new TextField.getText();
String balanceAsString = balanceTextField.getText();
double balance = Double.parseDouble(balanceAsString);
customer = new Customer(name,address,balance);
CustomerDialog.this.setVisible(false);
}
});
}
public Customer getCustomer()
{
return customer;
}
}
import java.util.ArrayList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class CustomerTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private List<Customer>customers;
private ArrayList<Customer> customers1 = new ArrayList <Customer> ();
private CustomerTableModel tableModel = new CustomerTableModel(customers);
private JTable table = new JTable(tableModel);
private JScrollPane scrollpane = new JScrollPane(table);
public CustomerTableModel(List<Customer>customers) {
this.customers = customers;
}
public int getColumnCount() {
return 3;
}
public int getRowCount() {
return customers.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
Customer customer = customers.get(rowIndex);
Object result;
if(columnIndex == 0){
result = customer.getName();
}
else if(columnIndex == 1){
result = customer.getAddress();
}
else {
result = customer.getBalance();
}
return result;
}
}
import java.awt.List;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;
import net.miginfocom.swing.MigLayout;
public class MainWindow extends JFrame {
List<Customer> customer = new ArrayList<Customer>();
private JMenuBar mainMenuBar = new JMenuBar();
private JMenuBar menuBar = new JMenuBar();
private JMenu customerMenu = new JMenu("Customer");
private JMenuItem newCustomerItem = new JMenuItem("New..");
public MainWindow() {
this.setLayout(new MigLayout());
this.setJMenuBar(mainMenuBar);
mainMenuBar.add(customerMenu);
customerMenu.add(newCustomerItem);
}
/**
* @param args
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
}
catch (Exception ignored){
}
MainWindow window = new MainWindow();
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setSize(500,400);
window.setVisible(true);
// TODO Auto-generated method stub
}
}