FileDocCategorySizeDatePackage
ArrayListTablePanel.javaAPI DocExample3531Fri Oct 12 19:54:38 BST 2001com.darwinsys.util

ArrayListTablePanel

public class ArrayListTablePanel extends JPanel

Fields Summary
protected ArrayList
list
The list of objects we are viewing
protected Class
objectClass
The kind of thing that is in the list.
protected ArrayListTableModel
model
The JTable's data (model)
protected JTable
table
The JTable itself
Constructors Summary
public ArrayListTablePanel(Class objClass, ArrayList al, ArrayListTableModel lm)
Construct new ArrayListTablePanel


		objectClass = objClass;
		list = al;
		model = lm;
		table = new JTable(model);

		setLayout(new BorderLayout());

		add(new JScrollPane(table), BorderLayout.CENTER);

		JPanel botPanel = new JPanel();
		JButton jb;
		botPanel.add(jb = new JButton("Add"));
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				int i = table.getSelectedRow();
				if (i<0) i = list.size();
				Object newObj = null;
				try {
					newObj = objectClass.newInstance();
				} catch (Exception ex) {
					JOptionPane.showMessageDialog(null,
						"Object creation FAILED\n " + ex, "Error",
						JOptionPane.ERROR_MESSAGE);
					return;
				}
				list.add(i, newObj);
				table.tableChanged(new 
					TableModelEvent(model, i, i, 
					TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
			}
		});

		botPanel.add(jb = new JButton("Move Up"));
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				int i = table.getSelectedRow();
				if (i == -1 || i == 0)
					return;
				Object obj = list.get(i);
				list.remove(i);
				list.add(i-1, obj);
				table.tableChanged(new 
					TableModelEvent(model, i-1, i-1, 
					TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
				model.invalidateCache();
			}
		});

		botPanel.add(jb = new JButton("Move Down"));
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				int i = table.getSelectedRow();
				if (i == -1 || i == list.size()-1)
					return;
				Object obj = list.get(i);
				list.remove(i);
				list.add(i+1, obj);
				table.tableChanged(new 
					TableModelEvent(model, i, i, 
					TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT));
				// table.setSelectedRowInterval(i+1, i+1);
				model.invalidateCache();
			}
		});

		botPanel.add(jb = new JButton("Remove"));
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				int i = table.getSelectedRow();
				if (i<0)
					return;	// nothing selected
				list.remove(i);
				table.tableChanged(new 
					TableModelEvent(model, i, i, 
					TableModelEvent.ALL_COLUMNS, TableModelEvent.DELETE));
				model.invalidateCache();
			}
		});

		add(botPanel, BorderLayout.SOUTH);
	
Methods Summary
public javax.swing.JTablegetTable()
Return a reference to the JTable.

		return table;