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

The following examples show how to use javax.swing.JTable#isCellEditable() . 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: FlatTableCellBorder.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether at least one selected cell is editable.
 */
protected boolean isSelectionEditable( JTable table ) {
	if( table.getRowSelectionAllowed() ) {
		int columnCount = table.getColumnCount();
		int[] selectedRows = table.getSelectedRows();
		for( int selectedRow : selectedRows ) {
			for( int column = 0; column < columnCount; column++ ) {
				if( table.isCellEditable( selectedRow, column ) )
					return true;
			}
		}
	}

	if( table.getColumnSelectionAllowed() ) {
		int rowCount = table.getRowCount();
		int[] selectedColumns = table.getSelectedColumns();
		for( int selectedColumn : selectedColumns ) {
			for( int row = 0; row < rowCount; row++ ) {
				if( table.isCellEditable( row, selectedColumn ) )
					return true;
			}
		}
	}

	return false;
}
 
Example 2
Source File: JTableJavaElement.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private void validate(int viewRow, int viewCol) {
	JTable table = (JTable) getComponent();
	try {
		int row = table.convertRowIndexToModel(viewRow);
		int col = table.convertColumnIndexToModel(viewCol);
		TableModel model = table.getModel();
		if (row >= 0 && row < model.getRowCount() && col >= 0 && col < model.getColumnCount()) {
			if (table.isCellEditable(viewRow, viewCol)) {
				return;
			} else {
				throw new NoSuchElementException(
						"The cell is not editable on JTable: (" + viewRow + ", " + viewCol + ")", null);
			}
		}
	} catch (IndexOutOfBoundsException e) {
	}
	throw new NoSuchElementException("Invalid row/col for JTable: (" + viewRow + ", " + viewCol + ")", null);
}
 
Example 3
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
/**
 * Reads clipboard data and converts it into supported format and fills the
 * tmodel cells
 *
 * @param table the target tmodel
 */
private static void pasteFromClipboard(JTable table) {
    int startRow = table.getSelectedRows()[0];
    int startCol = table.getSelectedColumns()[0];
    String pasteString;
    try {
        pasteString = (String) (CLIPBOARD.getContents(CLIPBOARD).getTransferData(DataFlavor.stringFlavor));
    } catch (UnsupportedFlavorException | IOException ex) {
        Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
        return;
    }
    String[] lines = pasteString.split(LINE_BREAK);
    for (int i = 0; i < lines.length; i++) {
        String[] cells = lines[i].split(CELL_BREAK);
        if (table.getRowCount() <= startRow + i) {
            ((DefaultTableModel) table.getModel()).addRow(nullRow);
        }
        for (int j = 0; j < cells.length; j++) {
            if (table.getColumnCount() > startCol + j) {
                if (table.isCellEditable(startRow + i, startCol + j)) {
                    table.setValueAt(cells[j], startRow + i, startCol + j);
                }
            }
        }
    }
}
 
Example 4
Source File: DomainResults.java    From yeti with MIT License 6 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column) {
    if (isSelected) {
        setForeground(table.getSelectionForeground());
        setBackground(table.getSelectionBackground());
    } else {
        setForeground(table.getForeground());
        setBackground(table.getBackground());
    }
    setFont(table.getFont());
    if (hasFocus) {
        setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        if (table.isCellEditable(row, column)) {
            setForeground(UIManager.getColor("Table.focusCellForeground"));
            setBackground(UIManager.getColor("Table.focusCellBackground"));
        }
    } else {
        setBorder(new EmptyBorder(1, 2, 1, 2));
    }
    setText((value == null) ? "" : value.toString());
    return this;
}
 
Example 5
Source File: ArrayTable.java    From osp with GNU General Public License v3.0 6 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
  super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
  boolean editable = table.isCellEditable(row, column);
  setEnabled(editable);
  ArrayTable arrayTable = (ArrayTable) table;
  DecimalFormat cellFormat = arrayTable.formatDictionary.get(column); // does column have a special format?
  if(cellFormat==null) {
    cellFormat = arrayTable.defaultFormat; // use default format
  }
  if(value==null) {
    setText("");                           //$NON-NLS-1$
  } else if(cellFormat==null) {            // default format not set
    setText(value.toString());
  } else {
    try {
      setText(cellFormat.format(value));
    } catch(IllegalArgumentException ex) { // convert to string if value cannot be formatted
      setText(value.toString());
    }
  }
  setBorder(new CellBorder(new Color(224, 224, 224)));
  return this;
}
 
Example 6
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * clear selection by setting empty values
 *
 * @param table to be cleared
 */
private static void ClearSelection(JTable table) {
    int[] srow = table.getSelectedRows();
    int[] scol = table.getSelectedColumns();
    int lastSrow = srow.length;
    int lastScol = scol.length;
    for (int i = 0; i < lastSrow; i++) {
        for (int j = 0; j < lastScol; j++) {
            if (table.isCellEditable(srow[i], scol[j])) {
                table.setValueAt("", srow[i], scol[j]);
            }
        }
    }
}
 
Example 7
Source File: JtableUtils.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the cell values of selected cells of the <code>tmodel</code> and
 * uploads into clipboard in supported format
 *
 * @param isCut CUT flag,<code>true</code> for CUT and <code>false</code>
 * for COPY
 * @param table the source for the action
 * @see #escape(java.lang.Object)
 */
private static void copyToClipboard(boolean isCut, JTable table) {
    try {
        int numCols = table.getSelectedColumnCount();
        int numRows = table.getSelectedRowCount();
        int[] rowsSelected = table.getSelectedRows();
        int[] colsSelected = table.getSelectedColumns();
        if (numRows != rowsSelected[rowsSelected.length - 1] - rowsSelected[0] + 1 || numRows != rowsSelected.length
                || numCols != colsSelected[colsSelected.length - 1] - colsSelected[0] + 1 || numCols != colsSelected.length) {
            JOptionPane.showMessageDialog(null, "Invalid Selection", "Invalid Selection", JOptionPane.ERROR_MESSAGE);
            return;
        }
        StringBuilder excelStr = new StringBuilder();
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                excelStr.append(escape(table.getValueAt(rowsSelected[i], colsSelected[j])));
                if (isCut) {
                    if (table.isCellEditable(rowsSelected[i], colsSelected[j])) {
                        table.setValueAt("", rowsSelected[i], colsSelected[j]);
                    }
                }
                if (j < numCols - 1) {
                    excelStr.append(CELL_BREAK);
                }
            }
            if (i < numRows - 1) {
                excelStr.append(LINE_BREAK);
            }
        }
        if (!excelStr.toString().isEmpty()) {
            StringSelection sel = new StringSelection(excelStr.toString());
            CLIPBOARD.setContents(sel, sel);
        }

    } catch (HeadlessException ex) {
        Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
}
 
Example 8
Source File: YesNoRenderer.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {
	if (isSelected) {
		super.setForeground(table.getSelectionForeground());
		super.setBackground(table.getSelectionBackground());
	} else {
		super.setForeground((unselectedForeground != null) ? unselectedForeground : table.getForeground());
		super.setBackground((unselectedBackground != null) ? unselectedBackground : table.getBackground());
	}

	setFont(table.getFont());

	if (hasFocus) {
		setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));

		if (table.isCellEditable(row, column)) {
			super.setForeground(UIManager.getColor("Table.focusCellForeground"));
			super.setBackground(UIManager.getColor("Table.focusCellBackground"));
		}
	} else {
		setBorder(noFocusBorder);
	}
	setValue(value);
	Color back = getBackground();
	boolean colorMatch = (back != null) && (back.equals(table.getBackground())) && table.isOpaque();
	setOpaque(!colorMatch);
	return this;
}
 
Example 9
Source File: MosaicExpressionsPanel.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
                                               int row, int column) {
    final boolean enabled = table.isEnabled();
    setText((String) value);

    if (isSelected) {
        super.setForeground(table.getSelectionForeground());
        super.setBackground(table.getSelectionBackground());
    } else if (!enabled) {
        super.setForeground(UIManager.getColor("TextField.inactiveForeground"));
        super.setBackground(table.getBackground());
    } else {
        super.setForeground(table.getForeground());
        super.setBackground(table.getBackground());
    }

    setFont(table.getFont());

    if (hasFocus) {
        setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
        if (table.isCellEditable(row, column)) {
            super.setForeground(UIManager.getColor("Table.focusCellForeground"));
            super.setBackground(UIManager.getColor("Table.focusCellBackground"));
        }
    } else {
        setBorder(noFocusBorder);
    }

    setValue(value);

    return this;
}
 
Example 10
Source File: EncryptBackActionCellRenderer.java    From pgptool with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
		int row, int column) {

	if (table == null) {
		return this;
	}

	lbl.setFont(table.getFont().deriveFont(Font.BOLD));

	Color fg = null;
	Color bg = null;

	JTable.DropLocation dropLocation = table.getDropLocation();
	if (dropLocation != null && !dropLocation.isInsertRow() && !dropLocation.isInsertColumn()
			&& dropLocation.getRow() == row && dropLocation.getColumn() == column) {
		fg = UIManager.getColor("Table.dropCellForeground");
		bg = UIManager.getColor("Table.dropCellBackground");
		isSelected = true;
	}

	if (isSelected) {
		super.setForeground(fg == null ? table.getSelectionForeground() : fg);
		super.setBackground(bg == null ? table.getSelectionBackground() : bg);
	} else {
		Color background = unselectedBackground != null ? unselectedBackground : table.getBackground();
		if (background == null || background instanceof javax.swing.plaf.UIResource) {
			Color alternateColor = UIManager.getColor("Table.alternateRowColor");
			if (alternateColor != null && row % 2 != 0) {
				background = alternateColor;
			}
		}
		super.setForeground(unselectedForeground != null ? unselectedForeground : table.getForeground());
		super.setBackground(background);
	}

	setFont(table.getFont());

	if (hasFocus) {
		Border border = null;
		if (isSelected) {
			border = UIManager.getBorder("Table.focusSelectedCellHighlightBorder");
		}
		if (border == null) {
			border = UIManager.getBorder("Table.focusCellHighlightBorder");
		}
		setBorder(border);

		if (!isSelected && table.isCellEditable(row, column)) {
			Color col;
			col = UIManager.getColor("Table.focusCellForeground");
			if (col != null) {
				super.setForeground(col);
			}
			col = UIManager.getColor("Table.focusCellBackground");
			if (col != null) {
				super.setBackground(col);
			}
		}
	} else {
		setBorder(getNoFocusBorder());
	}

	return this;
}