# 7
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.util.*;
import com.mqmonitor.mq.beans.MQMChannel;
public class TableSorterColumn extends JPanel {
private boolean DEBUG = false;
DefaultTableModel tablemodel = null;
String qmname;
public TableSorterColumn(String[] vec,Object[][] channelslist)
{
super(new BorderLayout());
tableSorterColumn(vec,channelslist);
}
public void tableSorterColumn(String[] vec,Object[][] channelslist) {
JFrame frame = new JFrame("ChannelsTableSorterColumn");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if(channelslist != null)
{
System.out.println("just before calling MyTableModel() constructor");
tablemodel = new MyTableModel(vec,channelslist);
//sorter = new TableSorter(tablemodel);
}
else
return;
final JTable table = new JTable(tablemodel)
{
public boolean isCellEditable(int row,int column)
{
return false;
}
};
table.addMouseListener( new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
//if ( SwingUtilities.isRightMouseButton(e) )
if (e.isPopupTrigger())
{
JTable source = (JTable)e.getSource();
final int row = source.rowAtPoint( e.getPoint() );
int column = source.columnAtPoint( e.getPoint() );
//final int sizeoftable = source.getRowCount() ;
System.out.println(row +" --> " +column);
source.changeSelection(row, column, false, false);
JMenu writemessage = null;
JPopupMenu popup = new JPopupMenu();
ActionListener menuListener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("Delete Channel"))
{
System.out.println("row number is :::: "+row);
tablemodel.removeRow(row);
}
}
};
JMenuItem item,deletech;
popup.add(deletech = new JMenuItem("Delete Channel") );
deletech.addActionListener(menuListener);
popup.add( item = new JMenuItem("Refresh Channel") );
item.addActionListener(menuListener);
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
});
table.getTableHeader().setToolTipText("Click to sort data");
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
class MyTableModel extends DefaultTableModel {
Object channelslist;
String[] vec;
String[] channelcolumnNames;
Object[][] data;
MyTableModel(String[] vec, Object[][] channelslist)
{
//super(vec,channelslist);
System.out.println("inside of MyTableModel() constructor");
this.channelslist = channelslist;
this.vec = vec;
this.channelcolumnNames = vec;
MQMChannel mqChannel = null;
int i = 0;
data = channelslist;
}
public String getColumnName(int col) {
return channelcolumnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
if (DEBUG) {
//System.out.println("New value of data:");
//printDebugData();
}
}
}
private static void createAndShowGUI() {
//Create and set up the window.
String[] columnNames = {"First Name","Last Name","Sport","# of Years","Vegetarian"};
Object[][] data = {
{"Mary", "Campione","Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml","Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath","Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour","Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne","Pool", new Integer(10), new Boolean(false)}
};
//Create and set up the content pane.
JFrame frame = new JFrame("TableSorterColumn");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
TableSorterColumn newContentPane = new TableSorterColumn(columnNames,data);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
This is my snippet, i am working on SSCCE. that was huge file, finally i could make this program, I am unable to display the table on Frame.
If we walk through the code, we can find bold line where i am unable to delete my desired row.