I have a JTable
and I want to open a PopupMenu by a right-click inside a cell. I tried this by making my own AbstractCellEditor
, but it doesn't work (When I start my program and when the first method initialize the CellEditor
(it´s the last thing that this method do), CellEditor
is starting. But at the beginning of the next method the CellEditor
has already disapeared). Can somebody please help me? I´m trying to find the error for weeks.
This is the Code of CellEditor
:
public class CellEditor extends AbstractCellEditor implements TableCellEditor {
JTextField component;
JPopupMenu popmen;
CellEditor(){
component = new JTextField();
popmen = new JPopupMenu();
createPopmen(component);
component.addMouseListener(new MouseAdapter(){
public void mouseReleased(MouseEvent e) {
if ( e.isPopupTrigger() )
popmen.show( e.getComponent(), e.getX(), e.getY() );
}
});
}
public Component getTableCellEditorComponent1(JTable table, Object value,
boolean isSelected, int rowIndex, int vColIndex) {
if (isSelected) {
}
component.setText((String)value);
return component;
}
public Object getCellEditorValue() {
return component.getText();
}
@Override
public Component getTableCellEditorComponent(JTable arg0, Object arg1, boolean arg2,int arg3, int arg4) {
return null;
}
public void createPopmen(final JTextField text){
// creating Popmen
}
}
This is the part of the code, where something is happening with my table:
Vector<Vector> nu = new Vector<Vector>();
Vector<String> columnNames = new Vector<String>();
TableModel model;
model = new DefaultTableModel(nu, columnNames){
public Class getColumnClass() {
Class returnValue;
if ((3 >= 0) && (3 < getColumnCount())) {
System.out.println(getValueAt(0, 3));
returnValue = getValueAt(0, 3).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
public boolean isCellEditable(int row, int col) {
if(col == 0){
return false;
}
开发者_高级运维 else{
return true;
}
}
};
table = new JTable(model);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
int a = table.getSelectedRow();
if (!changes.contains(a) && a!=-1 && table.getSelectedColumn() != 0){
changes.add(a);
saved = false;
}
}
});
// SearchingBar
textfield.addKeyListener( new KeyListener(){
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
String searchedText =textfield.getText();
if (searchedText.length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(searchedText));
}
}
@Override
public void keyTyped(KeyEvent arg0) {
}
});
((DefaultTableModel) model).removeRow(0);
table.setModel(model);
table.setCellEditor(new CellEditor());
There should be no need to create a custom editor. You should be able to do something like:
JTable table = new JTable(...);
DefaultCellEditor dce = table.getDefaultEditor(Object.class);
Component editor = dce.getComponent();
editor.addMouseListener(...);
Also, read the section from the Swing tutorial on Sorting and Filtering for a working example of how to use filtering. You should not be using a KeyListener.
If you need more help then post your SSCCE which demonstrates the problem.
精彩评论