Al final después de un descanso para despejar la mente, y leyendo y leyendo mucho, lo conseguí.
Si algúno sabe de algo que este mal, porfavor, decirlo
Código:
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;
public class AllTableExample {
private JPopupMenu popup;
private JTabbedPane tabbedPane;
private JTable allTable;
private AllTableModel allTableModel;
private List<TableData> data;
static int selected = -1;
private JFrame frame;
public AllTableExample() {
data = new ArrayList<TableData>();
data.add(new TableData("test1"));
data.add(new TableData("test2"));
data.add(new TableData("test3"));
data.add(new TableData("test4"));
data.add(new TableData("test5"));
allTableModel = new AllTableModel(data);
}
public void createUI() {
frame = new JFrame();
tabbedPane = new JTabbedPane();
popup = new JPopupMenu();
tabbedPane.add("All", getAllTablePanel());
JMenuItem delete = new JMenuItem("Delete");
popup.add(delete);
delete.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e) {
int del = allTable.getSelectedRow();
data.remove(del);
frame.repaint();
JOptionPane.showMessageDialog((Component) frame, del);
}
});
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Table Model Example.");
frame.pack();
frame.setVisible(true);
}
private void mostrarPopupMenu(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
private JPanel getAllTablePanel() {
JPanel panel = new JPanel();
allTable = new JTable(allTableModel);
JScrollPane scroll = new JScrollPane(allTable);
panel.add(scroll);
allTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if ( SwingUtilities.isLeftMouseButton( e ) )
{
selected = allTable.getSelectedRow();
}
else if ( SwingUtilities.isRightMouseButton( e ))
{
Point p = e.getPoint();
int rowNumber = allTable.rowAtPoint( p );
ListSelectionModel model = allTable.getSelectionModel();
model.setSelectionInterval( rowNumber, rowNumber );
selected = rowNumber;
frame.repaint();
mostrarPopupMenu(e);
}
}
});
return panel;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
new AllTableExample().createUI();
}
};
EventQueue.invokeLater(r);
}
}
class AllTableModel extends AbstractTableModel {
List<TableData> tableData = new ArrayList<TableData>();
Object[] columnNames = {"Name"};
public AllTableModel(List<TableData> data) {
tableData = data;
}
public List<TableData> getTableData() {
return tableData;
}
@Override
public String getColumnName(int column) {
return columnNames[column].toString();
}
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getRowCount() {
return tableData.size();
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableData data = tableData.get(rowIndex);
return data.getName();
}
}
class TableData {
private String name;
public TableData(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}