Here is the code...
import java.awt.*;
import javax.swing.*;
import java.awt.ActiveEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
public class Updater extends JFrame{
private JButton addButton;
private JButton removeButton;
private JButton removeAllButton;
private JTable table;
MyTableModel model;
public Updater(Object [][] obj , String [] header){
super("Runtime Updater");
setLayout(new FlowLayout());
// constructor of JTable model
model = new MyTableModel(obj, header);
// the table from that model
table = new JTable(model);
add(new JScrollPane(table));
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(false);
///add additional items
addButton = new JButton("Add");
addButton.setMnemonic(KeyEvent.VK_A);
removeButton = new JButton("Remove");
removeButton.setMnemonic(KeyEvent.VK_R);
//adding action to the addButton
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String user= JOptionPane.showInputDialog(null,"Enter User:");
String pass= JOptionPane.showInputDialog(null,"Enter Password:");
model.addItem(user, pass); //this is a method defined in the MyTableModel class, not a predefined one...
}
});
add(addButton);
// adding action to removeButton
removeButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int selectedRow = table.getSelectedRow();
JOptionPane.showMessageDialog(null,"The removed item : "+selectedRow);
model.removeRow(selectedRow);
}
});
add(removeButton);
}
public class MyTableModel extends AbstractTableModel{
ArrayList<Object []>al;
String [] header;
public MyTableModel(Object [][] obj, String [] header){
// save the header
this.header = header;
// and the rows
al = new ArrayList<Object[]>();
// copy the rows into the ArrayList
for(int i = 0; i < obj.length; ++i)
al.add(obj[i]);
}
public int getColumnCount() {
// TODO Auto-generated method stub
return header.length;
}
public int getRowCount() {
// TODO Auto-generated method stub
return al.size();
}
public Object getValueAt(int row, int col) {
// TODO Auto-generated method stub
return al.get(row)[col];
}
public String getColumnName(int index) {
return header[index];
}
void addItem(String user, String pass) {
// make it an array[2] as this is the way it is stored in the ArrayList
// (not best design but we want simplicity)
String[] str = new String[2];
str[0] = user;
str[1] = pass;
al.add(str);
// inform the GUI that I have change
fireTableDataChanged();
}
void removeRow(int row){
int [] remover = new int[1];
remover[0]=row;
al.remove(remover);
fireTableDataChanged();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String [] header ={"User","Password"};
Object [][] rowItems ={
{"Red","Alert"},{"Robin","Hood"},{"Maximus","general"},{"Galeo","Romeo"},
{"Euclid","Percy"},{"VamPercy","Nethd"}
};
Updater upd = new Updater(rowItems,header);
upd.setVisible(true);
upd.setSize(500,600);
upd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFrame.setDefaultLookAndFeelDecorated(false);
}
}
I can't figure it out?
Ignore the comments, those are there for my learning purpose.
This post has been edited by vbabey: 26 August 2012 - 04:07 AM

New Topic/Question
Reply




MultiQuote




|