Newer
Older
POS_for_GUI / src / panels / MainPanel.java
student on 4 Apr 2022 3 KB [add]featuring to generate window

package panels;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import frames.MainFrame;
import resources.Customer;

//-------------------------------------------------------------------------
// overview of customers
public class MainPanel extends JPanel {
		
	// model
	private HashMap<Integer, Customer> customerTable;

	// table
	private JTable panelTable;
	private DefaultTableModel panelTableModel;
	private String[] columnNames = { "name", "total", "points" };

	// buttons
	private JButton addButton;
	private JButton modifyButton;
	private JButton removeButton;

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// constructor
	// -------------------------------------------------------------------------
	//
	public MainPanel() {

		super(new BorderLayout());

		// initialize model
		customerTable = new HashMap<Integer, Customer>();

		// initialize table
		panelTableModel = new DefaultTableModel(columnNames, 0);
		panelTable = new JTable(panelTableModel);
		// deny to edit cell
		panelTable.setDefaultEditor(Object.class, null);

		// add buttons
		addButton = new JButton("Add");
		modifyButton = new JButton("Modify");
		removeButton = new JButton("Remove");

		// add listener
		addButton.addActionListener(new AddActionHandler());
		removeButton.addActionListener(new RemoveActionHandler());

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(addButton);
		buttonPanel.add(modifyButton);
		buttonPanel.add(removeButton);
		add(buttonPanel, BorderLayout.SOUTH);

		// add scroll menu
		JScrollPane listScrollPane = new JScrollPane(panelTable);
		add(listScrollPane, BorderLayout.NORTH);

	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// private
	// -------------------------------------------------------------------------
	//
	private void transitionToHistory(MainFrame mainFrame) {
		mainFrame.showHistoryPanel(this);
	}
	
	// -------------------------------------------------------------------------
	//
	private Object[] generateCustomerRow(String name) {
		Customer customer = new Customer(name + panelTableModel.getRowCount());
		Object[] data = { customer.getName(), customer.getPOS().getTotal(), customer.getPOS().getPoints() };

		return data;
	}

	// -------------------------------------------------------------------------
	// -------------------------------------------------------------------------
	// interface
	// -------------------------------------------------------------------------
	// add button's event
	private class AddActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			panelTableModel.addRow(generateCustomerRow("name"));
		}
	}

	// -------------------------------------------------------------------------
	// remove button's event
	private class RemoveActionHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			if (panelTable.getSelectedRow() < 0)
				return;

			if (1 < panelTable.getRowCount())
				panelTableModel.removeRow(panelTable.getSelectedRow());
		}
	}
}