Java Code Examples for javax.swing.JTable#getCellEditor()

The following examples show how to use javax.swing.JTable#getCellEditor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent ae) {
    JTable jt = (JTable) ae.getSource();

    if (jt != null) {
        if (jt.isEditing()) {
            TableCellEditor tce = jt.getCellEditor();

            if (PropUtils.isLoggable(BaseTable.class)) {
                PropUtils.log(BaseTable.class, "Cancelling edit due to keyboard event"); //NOI18N
            }

            if (tce != null) {
                jt.getCellEditor().cancelCellEditing();
            }
        } else {
            //If we're in a dialog, try to close it
            trySendEscToDialog(jt);
        }
    }
}
 
Example 2
Source File: PropertySheetTable.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent e) {
  JTable table = (JTable)e.getSource();
  if (!table.hasFocus()) {
    CellEditor cellEditor = table.getCellEditor();
    if (cellEditor != null && !cellEditor.stopCellEditing()) { return; }
    table.requestFocus();
    return;
  }
  ListSelectionModel rsm = table.getSelectionModel();
  int anchorRow = rsm.getAnchorSelectionIndex();
  table.editCellAt(anchorRow, PropertySheetTableModel.VALUE_COLUMN);
  Component editorComp = table.getEditorComponent();
  if (editorComp != null) {
    editorComp.requestFocus();
  }
}
 
Example 3
Source File: AnySelectionTableUI.java    From wandora with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void mousePressed(MouseEvent e) {
    super.mousePressed(e);
    
    if(!SwingUtilities.isLeftMouseButton(e)) {
        return;
    }

    JTable t = getTable();
    Point p = e.getPoint();
    int row = t.rowAtPoint(p);
    int column = t.columnAtPoint(p);
    int rowCount = t.getRowCount();
    int columnCount = t.getColumnCount();

    if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
        return;
    }

    TableCellEditor tce = t.getCellEditor();
    if((tce==null) || (tce.shouldSelectCell(e))) {
        t.requestFocus();
        updateTableSelectionModel(row, column, e.isControlDown(), e.isShiftDown(), false);
        t.repaint();
    }
}
 
Example 4
Source File: MemoryMapModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
void update() {
	JTable table = provider.getTable();
	TableCellEditor cellEditor = table.getCellEditor();
	if (cellEditor != null) {
		cellEditor.cancelCellEditing();
	}
	populateMap();
}
 
Example 5
Source File: BasePanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public final Map<String,String> getData() {
    Map<String,String> retVal = new HashMap<String,String>(getDataComponents().size());
    for (Component c : getDataComponents()) {
        // fill in the blanks...
        String compName = c.getName();
        if (compName != null) {
            // construct the key
            String key = compName;
            if (c instanceof JComboBox) {
                final JComboBox jcb = (JComboBox) c;
                retVal.put(key, (String) jcb.getSelectedItem());
            } else if (c instanceof JTextComponent) {
                final JTextComponent jtc = (JTextComponent) c;
                retVal.put(key, jtc.getText());
            } else if (c instanceof AbstractButton) {
                AbstractButton ab = (AbstractButton) c;
                retVal.put(key, Boolean.toString(ab.isSelected()));
            } else if (c instanceof JTable) {
                JTable table = (JTable) c;
                TableCellEditor tce = table.getCellEditor();
                if (null != tce) {
                    tce.stopCellEditing();
                }
                Object tm = table.getModel();
                if (tm instanceof DataTableModel) {
                    DataTableModel model = (DataTableModel) tm;
                    retVal.putAll(model.getData());
                }
            }
        }
    }
    return retVal;
}
 
Example 6
Source File: BasePanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public final Map<String,String> getData() {
    Map<String,String> retVal = new HashMap<String,String>(getDataComponents().size());
    for (Component c : getDataComponents()) {
        // fill in the blanks...
        String compName = c.getName();
        if (compName != null) {
            // construct the key
            String key = compName;
            if (c instanceof JComboBox) {
                final JComboBox jcb = (JComboBox) c;
                retVal.put(key, (String) jcb.getSelectedItem());
            } else if (c instanceof JTextComponent) {
                final JTextComponent jtc = (JTextComponent) c;
                retVal.put(key, jtc.getText());
            } else if (c instanceof AbstractButton) {
                AbstractButton ab = (AbstractButton) c;
                retVal.put(key, Boolean.toString(ab.isSelected()));
            } else if (c instanceof JTable) {
                JTable table = (JTable) c;
                TableCellEditor tce = table.getCellEditor();
                if (null != tce) {
                    tce.stopCellEditing();
                }
                Object tm = table.getModel();
                if (tm instanceof DataTableModel) {
                    DataTableModel model = (DataTableModel) tm;
                    retVal.putAll(model.getData());
                }
            }
        }
    }
    return retVal;
}
 
Example 7
Source File: AnySelectionTableUI.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void mouseDragged(MouseEvent e) {           
    super.mouseDragged(e);

    if(!SwingUtilities.isLeftMouseButton(e)) {
        return;
    }

    
    JTable t = getTable();
    Point p = e.getPoint();
    int row = t.rowAtPoint(p);
    int column = t.columnAtPoint(p);
    int rowCount = t.getRowCount();
    int columnCount = t.getColumnCount();

    if(column < 0 || row < 0 || column >= columnCount || row >= rowCount ) {
        return;
    }

    TableCellEditor tce = t.getCellEditor();
    if(tce==null) {
        t.requestFocus();
        updateTableSelectionModel(row, column, e.isControlDown(), !e.isShiftDown(), true);
        t.repaint();
    }
}
 
Example 8
Source File: TableSorter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    JTableHeader h = (JTableHeader) e.getSource();
    JTable table = h.getTable();
    int selectedRow = table.getSelectedRow();
    TableModel model = table.getModel();
    //remember selection to keep after sorting
    Object selectedAction=null;
    int objectColumn=-1;
    if(selectedRow>-1) {
        for(int i=0; i<table.getColumnCount(); i++) {
            //first find colum with appropriate object
            if(model.getValueAt(selectedRow, i) instanceof ActionHolder) {
                //remember this object
                selectedAction=model.getValueAt(selectedRow, i);
                objectColumn=i;
                //stop edition as we click somewhere ouside of editor
                TableCellEditor editor=table.getCellEditor();
                if(editor!=null) {
                    editor.stopCellEditing();
                }
                break;
            }
        }
    }
    TableColumnModel columnModel = h.getColumnModel();
    int viewColumn = columnModel.getColumnIndexAtX(e.getX());
    int column = columnModel.getColumn(viewColumn).getModelIndex();
    if (column != -1) {
        int status = getSortingStatus(column);
        if (!e.isControlDown()) {
            cancelSorting();
        }
        // Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or 
        // {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed. 
        status = status + (e.isShiftDown() ? -1 : 1);
        status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
        setSortingStatus(column, status);
        //reselect the same object
        if(selectedAction!=null)setSelectedRow(table, selectedAction, objectColumn);
    }
}
 
Example 9
Source File: TableRendererTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testEditors() {
    setUpModel();
    JTable t = ot.treeTable.getTable();
    ot.revalidate();

    javax.swing.JFrame f = new javax.swing.JFrame();
    f.setLayout(new BorderLayout());
    f.add(ot, BorderLayout.CENTER);
    f.setSize(600, 500);
    f.setVisible(true);
    //while (f.isVisible()) {
        try {
            Thread.sleep(333);
        } catch (InterruptedException ex) {
            Exceptions.printStackTrace(ex);
        }
    //}

    System.out.println("table rows = "+t.getRowCount());
    TableCellEditor tce = t.getCellEditor(0, 0);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 0)));
    //assertTrue(t+"is not editable.", t.isCellEditable(0, 0));
    Component c = tce.getTableCellEditorComponent(t, null, true, 0, 0);
    //System.err.println("c = "+c);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 0:DN", ((EditorComponent) c).getText());

    tce = t.getCellEditor(0, 1);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 1)));
    assertTrue(t+"is not editable.", t.isCellEditable(0, 1));
    c = tce.getTableCellEditorComponent(t, null, true, 0, 1);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 0:col1", ((EditorComponent) c).getText());

    tce = t.getCellEditor(0, 2);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 0, 2)));
    assertTrue(t+"is not editable.", t.isCellEditable(0, 2));
    c = tce.getTableCellEditorComponent(t, null, true, 0, 2);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 0:col2", ((EditorComponent) c).getText());

    tce = t.getCellEditor(1, 0);
    assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 0)));
    assertFalse(t+"is editable.", t.isCellEditable(1, 0));
    c = tce.getTableCellEditorComponent(t, null, true, 1, 0);
    assertFalse("Editor component = "+c, c instanceof EditorComponent);

    tce = t.getCellEditor(1, 2);
    assertFalse(tce+"is editable.", tce.isCellEditable(getMouseClickAt(t, 1, 2)));
    assertFalse(t+"is editable.", t.isCellEditable(1, 2));
    c = tce.getTableCellEditorComponent(t, null, true, 1, 2);
    assertFalse("Editor component = "+c, c instanceof EditorComponent);

    tce = t.getCellEditor(3, 1);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 3, 1)));
    assertTrue(t+"is not editable.", t.isCellEditable(3, 1));
    c = tce.getTableCellEditorComponent(t, null, true, 3, 1);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 3:col1", ((EditorComponent) c).getText());

    tce = t.getCellEditor(6, 0);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 6, 0)));
    assertTrue(t+"is not editable.", t.isCellEditable(6, 0));
    c = tce.getTableCellEditorComponent(t, null, true, 6, 0);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 6:DN", ((EditorComponent) c).getText());

    tce = t.getCellEditor(9, 2);
    assertTrue(tce+"is not editable.", tce.isCellEditable(getMouseClickAt(t, 9, 2)));
    assertTrue(t+"is not editable.", t.isCellEditable(9, 2));
    c = tce.getTableCellEditorComponent(t, null, true, 9, 2);
    assertTrue("Editor component = "+c, c instanceof EditorComponent);
    assertEquals("Editor of 9:col2", ((EditorComponent) c).getText());
}
 
Example 10
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
/**
 * stops the tmodel cell editing
 *
 * @param table the target tmodel
 */
public static void cancelEditing(JTable table) {
    if (table.getCellEditor() != null) {
        table.getCellEditor().cancelCellEditing();
    }
}
 
Example 11
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
public static void stopEditing(JTable table) {
    if (null != table.getCellEditor()) {
        table.getCellEditor().stopCellEditing();
    }
}