Java 8 JTable - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 JTable class.

This code example shows how to use the following methods:getRowHeight, getSelectionBackground, getSelectionForeground, isCellEditable, getBackground
    } 
     
    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));

7
CodeRank
This code example shows how to use the following methods:getCellEditor, getEditingColumn, getEditingRow, getSelectedRow, isEditing
 
        if (action.equals(DELETE_COMMAND)) {
            if (tableModel.getRowCount() > 0) {
                // If a table cell is being edited, we must cancel the editing 
                // before deleting the row. 
                if (jmsPropertiesTable.isEditing()) {
                    TableCellEditor cellEditor = jmsPropertiesTable.getCellEditor(jmsPropertiesTable.getEditingRow(), jmsPropertiesTable
                            .getEditingColumn());
                    cellEditor.cancelCellEditing();
                } 
 
                int rowSelected = jmsPropertiesTable.getSelectedRow();
 
                if (rowSelected != -1) {
                    tableModel.removeRow(rowSelected);
                    tableModel.fireTableDataChanged();
 
                    // Disable the DELETE and SAVE buttons if no rows remaining 
                    // after delete. 
                    if (tableModel.getRowCount() == 0) {

7
CodeRank
This code example shows how to use the following methods:getColumnCount, getColumnModel, getModel, getTableHeader, sizeColumnsToFit
   */ 
  public static int calcHeaderWidth(JTable table, int col) {
    if (table == null) 
      return -1; 
     
    if (col < 0 || col > table.getColumnCount()) {
      System.out.println("invalid col " + col);
      return -1; 
    } 
     
    JTableHeader header = table.getTableHeader();
    TableCellRenderer defaultHeaderRenderer = null;
    if (header != null) defaultHeaderRenderer = header.getDefaultRenderer();
    TableColumnModel columns = table.getColumnModel();
    TableModel data = table.getModel();
    TableColumn column = columns.getColumn(col);
    int width = -1;
    TableCellRenderer h = column.getHeaderRenderer();
    if (h == null) h = defaultHeaderRenderer;
    if (h != null) {

7
CodeRank
This code example shows how to use the following methods:getSelectionBackground, getSelectionForeground, getBackground, getForeground
This code example shows how to use the following methods:getSelectionBackground, getSelectionForeground, isCellEditable, getBackground, getForeground
			boolean isSelected, boolean hasFocus, int row, int column) {
 
		// Set checkbox state based on value 
		if (value instanceof Boolean) {
			setSelected(((Boolean) value).booleanValue());
			setEnabled(table.isEnabled() && table.isCellEditable(row, column));
		} else { 
			return null; 
		} 
		// Set foreground/background based on whether row is selected 
		if (isSelected) {
			setBackground(table.getSelectionBackground());
			setForeground(table.getSelectionForeground());
		} else { 
			setForeground(table.getForeground());
			setBackground(table.getBackground());
		} 
		if (hasFocus) {
			setBorder(TableCellRendererDefaults.getFocusBorder());
		} else { 
			setBorder(TableCellRendererDefaults.getNoFocusBorder());
7
CodeRank
This code example shows how to use the following methods:getColumnCount, getColumnModel, getRowCount, getRowHeight, getValueAt
class JTableMouseButtonListener implements MouseListener {
     
    private JTable __table;
 
    private void __forwardEventToButton(MouseEvent e) {
      TableColumnModel columnModel = __table.getColumnModel();
      int column = columnModel.getColumnIndexAtX(e.getX());
      int row    = e.getY() / __table.getRowHeight();
     
      Component component;
       
 
      //System.out.println("row " + row + " col " + column); 
      if(row >= __table.getRowCount() || row < 0 ||
         column >= __table.getColumnCount() || column < 0)
        return; 
 
      Object value = __table.getValueAt(row, column);
      
      if(!(value instanceof Component))

7
CodeRank
This code example shows how to use the following methods:getCellEditor, getEditingColumn, getEditingRow, isEditing
    /** 
     * Stop any editing that is currently being done on the table. This will 
     * save any changes that have already been made. 
     */ 
    public static void stopTableEditing(JTable table) {
        if (table.isEditing()) {
            TableCellEditor cellEditor = table.getCellEditor(table.getEditingRow(), table.getEditingColumn());
            cellEditor.stopCellEditing();
        } 
    } 
     
    /** 
     * Get pasted text from clipboard 
     * @return String Pasted text 
     * @throws UnsupportedFlavorException 
     * @throws IOException 
     */ 
    public static String getPastedText() throws UnsupportedFlavorException, IOException {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable trans = clipboard.getContents(null);
6
CodeRank
This code example shows how to use the following methods:getCellEditor, getEditingColumn, getEditingRow, getSelectedRow, isEditing
        this.deleteRowButton = deleteRowButton;
        this.sender = aSender;
    } 
 
    public void actionPerformed(ActionEvent e) {
        if (grid.isEditing()) {
            TableCellEditor cellEditor = grid.getCellEditor(grid.getEditingRow(), grid.getEditingColumn());
            cellEditor.stopCellEditing();
        } 
        final int selectedRow = grid.getSelectedRow();
 
        if (tableModel.getRowCount() == 0 || selectedRow < 0) {
            return; 
        } 
 
        tableModel.addRow(tableModel.getRowData(selectedRow));
        tableModel.fireTableDataChanged();
 
        // Enable DELETE (which may already be enabled, but it won't hurt) 
        deleteRowButton.setEnabled(true);

7
CodeRank
This code example shows how to use the following methods:getRowCount, getSelectedRow, getSelectedRowCount, getSelectionModel, rowAtPoint
        } 
        return idx;
    } 
 
    protected int checkTableSelection(JTable table, Point p) {
        int row = table.rowAtPoint(p);
        if (row >= 0 && row < table.getRowCount() && table.getSelectedRowCount() < 2 && table.getSelectedRow() != row) {
            table.getSelectionModel().setSelectionInterval(row, row);
        } 
        return row;
    } 
 
    protected TreePath checkTreeSelection(JTree tree, Point p) {
        TreePath path = tree.getPathForLocation(p.x, p.y);
        if (path != null && tree.getSelectionCount() < 2 && !tree.isPathSelected(path)) {
            tree.setSelectionPath(path);
        } 
        return path;
    } 
 
6
CodeRank
This code example shows how to use the following methods:getTableHeader, setSelectionMode, revalidate
     * 
     * @return the main GUI panel 
     */ 
    private Component makeMainPanel() {
        initializeTableModel(); 
        table = new JTable(tableModel);
        table.getTableHeader().setDefaultRenderer(new HeaderAsPropertyRenderer());
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        return makeScrollPane(table);
    } 
 
    /** 
     * Create a panel containing the title label for the table. 
     * 
     * @return a panel containing the title label 
     */ 
    private Component makeLabelPanel() {
        JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        labelPanel.add(tableLabel);
        return labelPanel;

6
CodeRank
See Code Examples for Java 8 JTable Methods: