JTable & Edit Button Click - Plz Help

Dear All

I am using JTable. I need suggestion regarding following. Please help me out.

I need suggesgtion that at background what should i use to know that a particular row is added,edit,delete or a certain operation is done.

All the following operation are done after clicking Edit Button.

1. I have add button to add row into jTable.

2. I have edit button to edit a particular row. And there is another column with combobox that shows that if "YES" is selected then only "YES" option value are updated. Is this correct.....?

3. I have delete button to remove a particular row. How do i know if 3 rows are deleted randomly out of 10 rows. (Shoud i use vector or something else)

4. What if Row # 3 is deleted and another Row is added at last. How do i know that which row is added as total rows are 10 in both cases (delete/add row).

5. How do i know that particular row is edited. Let column 1 has code = 1001 and that column value is now change to 1002. (I think i can get this from YES option as i defined in Q#2.) Is this correct.

Please help me what technique should i use to handle my all required events.

Answer me if you can help me out regarding any of above scneario.

Thanks in advance.

[1269 byte] By [AnandKaria] at [2007-9-30 21:33:58]
# 1

Hello,

The following might help you on how to resolve your problem

construct a Jtable having DefaultTableModel with data express using vector ,vector

Add a global variables to listen to the row or column

public int G_row,G_Col

myModel = new MyTableModel(columnNames, 5);

myModel.setDataVector(my_data2, columnNames);

table = new JTable(myModel);

table.addMouseListener(new MouseClickHandler());

private class MouseClickHandler extends MouseAdapter {

public void mouseClicked(MouseEvent event) {

no_mouseclick = event.getClickCount();

}

overidding your DefaulttableModel like

public boolean isCellEditable(int row, int col) {

boolean isedit false;

G_row = row;

G_Col=col;

// Add something to listen for the action request,

//may be double mouse click through mouselistener would enable a particular cell to true

private class MouseClickHandler extends MouseAdapter {

public void mouseClicked(MouseEvent event) {

no_mouseclick = event.getClickCount();

if no_mouseclick>=2

isedit= true;

}

In your Jtable add two button b1 for edit , b2 for delete

JPanel p1 = new JPanel();

p1.setLayout(new GridLayout(1, 3));

JButton b1 = new JButton("Edit row");

b1.setMnemonic(KeyEvent.VK_A);

b1.setToolTipText("Edit row");

JButton b2 = new JButton("Delete row");

b2.setMnemonic(KeyEvent.VK_D);

p1.add(b1);

p1.add(b2);

b1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

// peform your edting routine here

myModel.fireTableRowsUpdated(0, myModel.getRowCount());

//System.out.println(myModel.getRowCount());

}

});

b2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (table.getSelectedRow() > -1) {

int i = JOptionPane.showConfirmDialog(null,

"Confirm Remove Selected Row ?",

"CONFIRM DELETE",

JOptionPane.YES_NO_OPTION,

JOptionPane.INFORMATION_MESSAGE);

if (i == JOptionPane.YES_OPTION) {

int tblrow=table.getRowCount();

myModel.removeRow(table.getSelectedRow());

myModel.fireTableRowsDeleted(0,

tblrow);

// myModel.fireTableDataChanged();

}

} else {

JOptionPane.showMessageDialog(null,

"please select row to remove");

}

// myModel.fireTableDataChanged();

}

If your need more example, pls post again

Thank

8233 at 2007-7-7 3:05:17 > top of Java-index,Desktop,Core GUI APIs...